dynamiC  0.1
dynamic_string.c
Go to the documentation of this file.
1 
15 #include "dynamic_string.h"
16 
17 #define FLOAT_DIGITS 100000.
18 
28 {
29  dyn_ushort len = 0;
30 
31  while (*str++)
32  ++len;
33 
34  return len;
35 }
36 
54 void dyn_strcat(dyn_str destination, dyn_const_str source)
55 {
56  dyn_strcpy(&destination[dyn_strlen(destination)], source);
57 }
58 
76 void dyn_strcat2(dyn_str destination, dyn_const_str source)
77 {
78  destination = (dyn_str) realloc(destination, dyn_strlen(destination)+dyn_strlen(source)+1);
79  dyn_strcat(destination, source);
80 }
81 
94 void dyn_strcpy (dyn_str destination, dyn_const_str source)
95 {
96  while(*source)
97  *destination++=*source++;
98 
99  *destination = '\0';
100 }
101 
115 {
116  if (!i) return 1;
117 
118  dyn_ushort len = 0;
119 
120  if (i < 0) {
121  i *= -1;
122  ++len;
123  }
124 
125  while (i)
126  i/=10, ++len;
127 
128  return len;
129 }
130 
141 {
142  char const digit[] = "0123456789";
143 
144  if (i<0) {
145  *str++ = '-';
146  i *= -1;
147  }
148 
149  str += dyn_itoa_len(i);
150 
151  *str = '\0';
152  do {
153  *--str = digit[i%10];
154  i /= 10;
155  } while(i);
156 }
157 
163 {
164  dyn_ushort len = 1;
165 
166  dyn_int a = (dyn_int) f;
167  dyn_int b = (dyn_int) ((f - a) * FLOAT_DIGITS);
168 
169  len += dyn_itoa_len(a);
170  len += dyn_itoa_len(b);
171 
172  return len;
173 }
174 
186 {
187  dyn_int a = (dyn_int) f;
188  dyn_int b = (dyn_int) ((f - a) * FLOAT_DIGITS);
189 
190  dyn_itoa(str, a);
191 
192  dyn_ushort len = dyn_strlen(str);
193 
194  str[len] = '.';
195  dyn_itoa(&str[len+1], b < 0 ? -b : b);
196 }
197 
213 {
214  while (*a == *b++) {
215  if (*a++ == 0)
216  return 0;
217  }
218  return (*a - *(b - 1));
219 }
dyn_ushort dyn_itoa_len(dyn_int i)
Calculates the number of required characters for integer to string (decimal) conversion, minus increases the value by one.
const char * dyn_const_str
Standard dynamic C const string.
Definition: dynamic_types.h:39
Definition of C string manipulation functions.
int32_t dyn_int
32bit signed integer, standard Integer type.
Definition: dynamic_types.h:49
void dyn_strcat2(dyn_str destination, dyn_const_str source)
Concatenate strings, required memory is automatically allocated.
dyn_ushort dyn_strlen(dyn_const_str str)
Returns the length of an string.
void dyn_strcpy(dyn_str destination, dyn_const_str source)
Copy string.
void dyn_ftoa(dyn_str str, const dyn_float f)
Float to ASCII-string conversion (decimal).
dyn_int i
basic integer
Definition: dynamic_types.h:59
dyn_ushort dyn_ftoa_len(const dyn_float f)
Calculates the number of required characters for float to string (decimal) conversion, minus increases the value by one.
char dyn_char
Basic 8bit signed integer.
Definition: dynamic_types.h:30
uint16_t dyn_ushort
16bit unsigned integer
Definition: dynamic_types.h:43
dyn_str str
pointer to character-array
Definition: dynamic_types.h:61
dyn_float f
float value
Definition: dynamic_types.h:60
void dyn_itoa(dyn_str str, dyn_int i)
Integer to ASCII-string conversion.
void dyn_strcat(dyn_str destination, dyn_const_str source)
Concatenate strings.
dyn_char dyn_strcmp(dyn_const_str a, dyn_const_str b)
Compares the string a to the string b.
float dyn_float
basic float definition (32 bit)
Definition: dynamic_types.h:55
char * dyn_str
Standard dynamic C string.
Definition: dynamic_types.h:36
dyn_char b
boolean value
Definition: dynamic_types.h:58