Lhogho  0.0.028
 All Data Structures Files Functions Variables Typedefs Macros Pages
string_functions.c
Go to the documentation of this file.
1 //===================================================
2 // Project:TestSystem Author: Peter Armianov
3 //===================================================
4 //
5 // STRING_FUNCTIONS.C
6 //
7 // This file contains definitions of helper funcions
8 // working with TCHAR type
9 //
10 // Revision history:
11 // 2007-05-21 - file created
12 // 2007-06-25 - several bug fixes
13 // 2007-06-30 - several bug fixes
14 // 2007-07-12 - additional comments in Doxygen style
15 //===================================================
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <memory.h>
21 
22 #include "error.h"
23 #include "TestMaker.h"
24 
25 
26 //===========================================================
33 //===========================================================
34 size_t m_strlen(const TCHAR * str)
35 {
36  const TCHAR * start = str;
37  while (*str) ++str;
38  return str - start;
39 }
40 
41 
42 //===========================================================
53 //===========================================================
54 int m_strcmp(const TCHAR * str1, const TCHAR * str2)
55 {
56  while (*str1 && *str1 == *str2)
57  {
58  ++str1;
59  ++str2;
60  }
61  return *str1 - *str2;
62 }
63 
64 
65 //===========================================================
74 //===========================================================
75 TCHAR * m_strcpy(TCHAR * dest, const TCHAR * src)
76 {
77  TCHAR * mem = dest;
78  while (*src)
79  {
80  *dest++ = *src++;
81  }
82  *dest = 0;
83  return mem;
84 }
85 
86 
87 //===========================================================
94 //===========================================================
96 {
97  return (chr == TEXT(' ') || chr == TEXT('\t') ||
98  chr == TEXT('\r') || chr == TEXT('\n'));
99 }
100 
101 
102 //===========================================================
113 //===========================================================
114 RESULT m_strndup(TCHAR ** dest_ptr, const TCHAR * src, size_t num_chars)
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 }
125 
126 
127 //===========================================================
136 //===========================================================
137 RESULT m_strdup(TCHAR ** dest_ptr, const TCHAR * src)
138 {
139  size_t len = m_strlen(src);
140  return m_strndup(dest_ptr, src, len);
141 }
142 
143 
144 //===========================================================
157 //===========================================================
158 TCHAR *
159 m_fgets(FILE* file, TCHAR * buffer, size_t buffer_size, size_t * out_size_ptr)
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 }
205 
206 
207 //===========================================================
214 //===========================================================
215 void
216 m_fputs(FILE* file, const TCHAR * buffer)
217 {
218  size_t len = m_strlen(buffer);
219  fwrite(buffer, sizeof(TCHAR), len, file);
220 }
221 
222 //===========================================================
230 //===========================================================
231 void
232 m_fputs_ascii(FILE* file, const char * buffer)
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 }
247 
248 
249 //===========================================================
256 //===========================================================
257 void
258 m_fputc(FILE* file, TCHAR tchar)
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