Lhogho  0.0.028
 All Data Structures Files Functions Variables Typedefs Macros Pages
string_functions.c File Reference

Go to the source code of this file.

Functions

size_t m_strlen (const TCHAR *str)
 calculates string length More...
 
int m_strcmp (const TCHAR *str1, const TCHAR *str2)
 compares two strings More...
 
TCHARm_strcpy (TCHAR *dest, const TCHAR *src)
 copy string More...
 
BOOL m_isspace (TCHAR chr)
 test if char is space More...
 
RESULT m_strndup (TCHAR **dest_ptr, const TCHAR *src, size_t num_chars)
 duplicate string, but no more than num_chars symbols More...
 
RESULT m_strdup (TCHAR **dest_ptr, const TCHAR *src)
 duplicate string. Allocate memory for destination More...
 
TCHARm_fgets (FILE *file, TCHAR *buffer, size_t buffer_size, size_t *out_size_ptr)
 Read a line from file. More...
 
void m_fputs (FILE *file, const TCHAR *buffer)
 prints a line to file More...
 
void m_fputs_ascii (FILE *file, const char *buffer)
 prints a line to file, convert it to UNICODE if needed More...
 
void m_fputc (FILE *file, TCHAR tchar)
 prints a symbol to file More...
 

Function Documentation

size_t m_strlen ( const TCHAR str)
Parameters
strthe string
Returns
length of the string in symbols

Calculates string length in symbols. Works with char and w_char

Definition at line 34 of file string_functions.c.

35 {
36  const TCHAR * start = str;
37  while (*str) ++str;
38  return str - start;
39 }
int m_strcmp ( const TCHAR str1,
const TCHAR str2 
)
Parameters
str1first string
str2second string
Returns
comparison result similar to standart strcmp result

Comare two strings. Return negative value if first is less than second positive value if first is great than second and zero if strings are equal

Definition at line 54 of file string_functions.c.

55 {
56  while (*str1 && *str1 == *str2)
57  {
58  ++str1;
59  ++str2;
60  }
61  return *str1 - *str2;
62 }
TCHAR* m_strcpy ( TCHAR dest,
const TCHAR src 
)
Parameters
destdestination string
srcsource string
Returns
pointer to destination

Copy string value to given pointer. User must supply enought memory

Definition at line 75 of file string_functions.c.

76 {
77  TCHAR * mem = dest;
78  while (*src)
79  {
80  *dest++ = *src++;
81  }
82  *dest = 0;
83  return mem;
84 }
BOOL m_isspace ( TCHAR  chr)
Parameters
chrchar to test
Returns
result of the test

Test if symbol is space symbol (Space, new line or tab)

Definition at line 95 of file string_functions.c.

96 {
97  return (chr == TEXT(' ') || chr == TEXT('\t') ||
98  chr == TEXT('\r') || chr == TEXT('\n'));
99 }
RESULT m_strndup ( TCHAR **  dest_ptr,
const TCHAR src,
size_t  num_chars 
)
Parameters
dest_ptrpointer to destination string
srcsource string
num_charsnumber of characters to copy.
Returns
RESULT value. Error on memory problems

Copy string value to given pointer, byt no more than num_chars symbols. Allocates memory for destination. Not copy the 0 byte if source is longer than wanted length

Definition at line 114 of file string_functions.c.

115 {
116  *dest_ptr = (TCHAR *)malloc((num_chars + 1) * sizeof(TCHAR));
117  if (!*dest_ptr)
118  {
119  return ERR_MEMORY;
120  }
121  memcpy(*dest_ptr, src, num_chars * sizeof(TCHAR));
122  (*dest_ptr)[num_chars] = 0;
123  return SUCCESS_FULL;
124 }
RESULT m_strdup ( TCHAR **  dest_ptr,
const TCHAR src 
)
Parameters
dest_ptrpointer to destination string
srcsource string
Returns
RESULT value. Error on memory problems

Duplicate string. Allocate memory for destination. If memory problems occured return error.

Definition at line 137 of file string_functions.c.

138 {
139  size_t len = m_strlen(src);
140  return m_strndup(dest_ptr, src, len);
141 }
TCHAR* m_fgets ( FILE *  file,
TCHAR buffer,
size_t  buffer_size,
size_t *  out_size_ptr 
)
Parameters
filefile to read from
bufferbuffer to read in
buffer_sizesize of space in the buffer
out_size_ptrsize of line readed
Returns
pointer to readed string or NULL if error

Read characters from given open file until new line symbol is reached or input buffer is full. Returns the count of really readed symbols in parameter and pointer to the buffer. If error occured during read process returns NULL

Definition at line 159 of file string_functions.c.

160 {
161  TCHAR c = 0;
162  size_t pos = 0;
163  if (!file || !buffer || !buffer_size || !out_size_ptr)
164  {
165  LOG_ERROR("Invalid args supplied to m_fgets");
166  if (out_size_ptr)
167  {
168  *out_size_ptr = 0;
169  }
170  return NULL;
171  }
172 
173  while (pos < buffer_size - 1)
174  {
175  if (!fread(&c, sizeof(TCHAR), 1, file))
176  {
177  break;
178  }
179  buffer[pos++] = c;
180 
181  if (c == TEXT('\r') || c == TEXT('\n'))
182  {
183  TCHAR c1;
184  size_t readed;
185  if ((readed = fread(&c1, sizeof(TCHAR), 1, file)) &&
186  (c1 == TEXT('\r') || c1 == TEXT('\n')) && c1 != c)
187  {
188  buffer[pos++] = c1;
189  }
190  else
191  {
192  if (readed)
193  {
194  fseek(file, -1l, SEEK_CUR);
195  }
196  }
197  break;
198  }
199  }
200 
201  buffer[pos] = 0;
202  *out_size_ptr = pos;
203  return pos ? buffer : NULL;
204 }
void m_fputs ( FILE *  file,
const TCHAR buffer 
)
Parameters
filefile to write in
bufferstring to write

Writes a line to the file. Buffer must be a valid nul-terminated string

Definition at line 216 of file string_functions.c.

217 {
218  size_t len = m_strlen(buffer);
219  fwrite(buffer, sizeof(TCHAR), len, file);
220 }
void m_fputs_ascii ( FILE *  file,
const char *  buffer 
)
Parameters
filefile to write in
bufferstring to write

Writes a line to the file. If program works in unicode mode the string is converted from ASCII to unicode.

Definition at line 232 of file string_functions.c.

233 {
234  char * buf = (char *)buffer;
235  size_t len = strlen(buffer) * sizeof(TCHAR);
236 
237 #if defined(UNICODE)
238  buf = (char*)malloc(len);
239  while (*buffer) *buf++ = *buffer++, *buf++ = 0;
240 #endif
241  fwrite(buf, sizeof(TCHAR), len, file);
242 
243 #if defined(UNICODE)
244  free(buf);
245 #endif
246 }
void m_fputc ( FILE *  file,
TCHAR  tchar 
)
Parameters
filefile to write in
tcharsymbol to write

Writes a symbol to file.

Definition at line 258 of file string_functions.c.

259 {
260 #if defined (UNICODE)
261  putwc(tchar, file);
262 #else
263  putc(tchar, file);
264 #endif
265 }

[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Wed Jul 10 2013