diff options
Diffstat (limited to 'baseline/source/ammunition/ammunition_libc.c')
-rw-r--r-- | baseline/source/ammunition/ammunition_libc.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/baseline/source/ammunition/ammunition_libc.c b/baseline/source/ammunition/ammunition_libc.c new file mode 100644 index 0000000..4cce6e9 --- /dev/null +++ b/baseline/source/ammunition/ammunition_libc.c | |||
@@ -0,0 +1,166 @@ | |||
1 | /* | ||
2 | Include section | ||
3 | */ | ||
4 | |||
5 | #include "ammunition_string.h" | ||
6 | #include "ammunition_stdio.h" | ||
7 | #include "ammunition_stdlib.h" | ||
8 | |||
9 | |||
10 | /* | ||
11 | Standard library functions | ||
12 | */ | ||
13 | |||
14 | char ammunition_isdigit( unsigned char c ) | ||
15 | { | ||
16 | if ( ( c >= '0' ) & ( c <= '9' ) ) | ||
17 | return 1; | ||
18 | else | ||
19 | return 0; | ||
20 | } | ||
21 | |||
22 | int ammunition_isspace( int c ) | ||
23 | { | ||
24 | return ( c == ' ' ) | ( c == '\t' ) | ( c == '\n' ) | ( c == '\r' ); | ||
25 | } | ||
26 | |||
27 | void *ammunition_memcpy( void *dest, const void *src, size_x size ) | ||
28 | { | ||
29 | size_x i; | ||
30 | _Pragma( "loopbound min 2 max 6" ) | ||
31 | for ( i = 0; i < size; i++ ) | ||
32 | ( ( unsigned char * )dest )[i] = ( ( unsigned char * )src )[i]; | ||
33 | |||
34 | return dest; | ||
35 | } | ||
36 | |||
37 | |||
38 | void *ammunition_memset( void *s, int c, size_x n ) | ||
39 | { | ||
40 | size_x i; | ||
41 | _Pragma( "loopbound min 0 max 4" ) | ||
42 | for ( i = 0; i < n; i++ ) | ||
43 | ( ( unsigned char * )s )[i] = ( unsigned char )c; | ||
44 | |||
45 | return s; | ||
46 | } | ||
47 | |||
48 | |||
49 | int ammunition_memcmp ( const void *mem1, const void *mem2, size_x size ) | ||
50 | { | ||
51 | const unsigned char *p1 = (const unsigned char *) mem1, | ||
52 | *p2 = (const unsigned char *) mem2; | ||
53 | _Pragma( "loopbound min 0 max 4" ) | ||
54 | while ( size-- ) | ||
55 | if ( *p1 != *p2 ) | ||
56 | return ( *p1 - *p2 ); | ||
57 | else | ||
58 | p1++, p2++; | ||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | |||
63 | /* The following function is an analog of standard C function | ||
64 | `memmove'. The function returns the first operand. */ | ||
65 | |||
66 | void *ammunition_memmove ( void *s1, const void *s2, size_x n ) | ||
67 | { | ||
68 | int i; | ||
69 | |||
70 | if ( ( ( char * ) s1 < ( char * ) s2 && ( char * ) s1 + n <= ( char * ) s2 ) | ||
71 | || ( ( char * ) s2 < ( char * ) s1 | ||
72 | && ( char * ) s2 + n <= ( char * ) s1 ) ) | ||
73 | return ( void * ) ammunition_memcpy ( s1, s2, n ); | ||
74 | if ( ( char * ) s1 < ( char * ) s2 && ( char * ) s1 + n > ( char * ) s2 ) { | ||
75 | _Pragma( "loopbound min 0 max 4" ) | ||
76 | for ( i = 0; ( size_x ) i < n; i++ ) | ||
77 | ( ( char * ) s1 ) [i] = ( ( char * ) s2 ) [i]; | ||
78 | } else { | ||
79 | _Pragma( "loopbound min 0 max 4" ) | ||
80 | for ( i = n - 1; i >= 0; i-- ) | ||
81 | ( ( char * ) s1 )[i] = ( ( char * ) s2 ) [i]; | ||
82 | } | ||
83 | return s1; | ||
84 | } | ||
85 | |||
86 | int ammunition_strcmp ( const char *str1, const char *str2 ) | ||
87 | { | ||
88 | _Pragma( "loopbound min 1 max 4008" ) | ||
89 | while ( *str1 && ( *str1 == *str2 ) ) | ||
90 | str1++, str2++; | ||
91 | return *( const unsigned char * )str1 - *( const unsigned char * )str2; | ||
92 | } | ||
93 | |||
94 | int ammunition_atoi ( const char *str ) | ||
95 | { | ||
96 | int result = 0; | ||
97 | int sign = ( str[0] == '-' ? -1 : 1 ); | ||
98 | |||
99 | int readingPos = 0; | ||
100 | if ( str[0] == '-' || str[0] == '+' ) | ||
101 | readingPos++; | ||
102 | _Pragma( "loopbound min 1 max 1" ) | ||
103 | do { | ||
104 | result *= 10; | ||
105 | result += str[readingPos++] - 48; | ||
106 | } while ( str[readingPos] != 0 ); | ||
107 | |||
108 | return sign * result; | ||
109 | } | ||
110 | |||
111 | |||
112 | int ammunition_sprintf_d( char *s, int number ) | ||
113 | { | ||
114 | /* How many decimal digits do we need? */ | ||
115 | char digits = 0; | ||
116 | unsigned char writePos = 0; | ||
117 | long long copyOfNumber = number; | ||
118 | _Pragma( "loopbound min 1 max 10" ) | ||
119 | do { | ||
120 | digits++; | ||
121 | copyOfNumber /= 10; | ||
122 | } while ( copyOfNumber != 0 ); | ||
123 | |||
124 | writePos = digits; | ||
125 | if ( number < 0 ) { | ||
126 | writePos++; | ||
127 | s[0] = '-'; | ||
128 | } | ||
129 | s[writePos] = 0; | ||
130 | |||
131 | copyOfNumber = number; | ||
132 | _Pragma( "loopbound min 1 max 10" ) | ||
133 | do { | ||
134 | s[--writePos] = 48 + ( ( copyOfNumber >= 0 ? | ||
135 | copyOfNumber : -copyOfNumber ) % 10 ); | ||
136 | copyOfNumber /= 10; | ||
137 | } while ( copyOfNumber != 0 ); | ||
138 | |||
139 | return digits + ( number < 0 ? 1 : 0 ); | ||
140 | } | ||
141 | |||
142 | |||
143 | int ammunition_sprintf_u( char *s, unsigned int number ) | ||
144 | { | ||
145 | /* How many decimal digits do we need? */ | ||
146 | char digits = 0; | ||
147 | unsigned char writePos = 0; | ||
148 | unsigned long copyOfNumber = number; | ||
149 | _Pragma( "loopbound min 1 max 10" ) | ||
150 | do { | ||
151 | digits++; | ||
152 | copyOfNumber /= 10; | ||
153 | } while ( copyOfNumber != 0 ); | ||
154 | |||
155 | writePos = digits; | ||
156 | s[writePos] = 0; | ||
157 | |||
158 | copyOfNumber = number; | ||
159 | _Pragma( "loopbound min 1 max 10" ) | ||
160 | do { | ||
161 | s[--writePos] = 48 + ( copyOfNumber % 10 ); | ||
162 | copyOfNumber /= 10; | ||
163 | } while ( copyOfNumber != 0 ); | ||
164 | |||
165 | return digits; | ||
166 | } | ||