You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
8.1 KiB

  1. /*********************************************/
  2. /* string.cpp */
  3. /* some useful functions to handle strings */
  4. /*********************************************/
  5. #include "fctsys.h"
  6. #include <time.h>
  7. #include "common.h"
  8. #include "macros.h"
  9. /*********************************************************************/
  10. int ReadDelimitedText( char* dest, char* source, int NbMaxChar )
  11. /*********************************************************************/
  12. /* read a double-quote delimited text from source and put it in in dest,
  13. * read NbMaxChar bytes max
  14. * return the char count read from source
  15. */
  16. {
  17. int ii, jj, flag = 0;
  18. for( ii = 0, jj = 0; ii < NbMaxChar - 1; jj++, source++ )
  19. {
  20. if( *source == 0 )
  21. break; /* E.O.L. */
  22. if( *source == '"' ) /* delimiteur trouve */
  23. {
  24. if( flag )
  25. break; /* Fin de texte delimite */
  26. flag = 1; /* Marque 1er delimiteur trouve */
  27. }
  28. else if( flag )
  29. {
  30. *dest = *source; dest++; ii++;
  31. }
  32. }
  33. *dest = 0; /* Null termined */
  34. return jj;
  35. }
  36. /********************************/
  37. char* StrPurge( char* text )
  38. /********************************/
  39. /* Remove training spaces in text
  40. * return a pointer on the first non space char in text
  41. */
  42. {
  43. char* ptspace;
  44. if( text == NULL )
  45. return NULL;
  46. while( (*text <= ' ') && *text )
  47. text++;
  48. ptspace = text + strlen( text ) - 1;
  49. while( (*ptspace <= ' ') && *ptspace && (ptspace >= text) )
  50. {
  51. *ptspace = 0; ptspace--;
  52. }
  53. return text;
  54. }
  55. /*****************************************************************/
  56. char* GetLine( FILE* File, char* Line, int* LineNum, int SizeLine )
  57. /*****************************************************************/
  58. /* Read lines from File
  59. * Skip void lines and comments (starting by #)
  60. * return the first non void line.
  61. * increments *LineNum for ecah line
  62. */
  63. {
  64. do {
  65. if( fgets( Line, SizeLine, File ) == NULL )
  66. return NULL;
  67. if( LineNum )
  68. *LineNum += 1;
  69. } while( Line[0] == '#' || Line[0] == '\n' || Line[0] == '\r'
  70. || Line[0] == 0 );
  71. strtok( Line, "\n\r" );
  72. return Line;
  73. }
  74. /*******************************/
  75. char* DateAndTime( char* aBuffer )
  76. /*******************************/
  77. /* return in aBuffer the date and time
  78. * time is the local time.
  79. */
  80. {
  81. wxString datetime;
  82. datetime = DateAndTime();
  83. strcpy( aBuffer, CONV_TO_UTF8( datetime ) );
  84. return aBuffer;
  85. }
  86. /*******************************/
  87. wxString DateAndTime()
  88. /*******************************/
  89. /* return the date and time in a wxString
  90. * note: does the same thing than strftime()
  91. * time is the local time.
  92. */
  93. {
  94. wxString Line;
  95. wxDateTime datetime = wxDateTime::Now();
  96. datetime.SetCountry( wxDateTime::Country_Default );
  97. Line = datetime.Format( wxDefaultDateTimeFormat, wxDateTime::Local );
  98. return Line;
  99. }
  100. /************************************************************/
  101. int StrLenNumCmp( const wxChar* str1, const wxChar* str2, int NbMax )
  102. /************************************************************/
  103. /*
  104. * sort() function
  105. * Same as strncmp() but numbers in strings
  106. * are compared according to the value, not the ascii value of each digit
  107. */
  108. {
  109. int i;
  110. int nb1 = 0, nb2 = 0;
  111. if( (str1 == NULL) || (str2 == NULL) )
  112. return 0;
  113. for( i = 0; i < NbMax; i++ )
  114. {
  115. if( isdigit( *str1 ) && isdigit( *str2 ) ) /* digit found */
  116. {
  117. nb1 = 0; nb2 = 0;
  118. while( isdigit( *str1 ) )
  119. {
  120. nb1 = nb1 * 10 + *str1 - '0'; str1++;
  121. }
  122. while( isdigit( *str2 ) )
  123. {
  124. nb2 = nb2 * 10 + *str2 - '0'; str2++;
  125. }
  126. if( nb1 < nb2 )
  127. return -1;
  128. if( nb1 > nb2 )
  129. return 1;
  130. }
  131. if( *str1 < *str2 )
  132. return -1;
  133. if( *str1 > *str2 )
  134. return 1;
  135. if( (*str1 == 0 ) && ( *str2 == 0 ) )
  136. return 0;
  137. str1++; str2++;
  138. }
  139. return 0;
  140. }
  141. /***********************************************/
  142. int StrNumICmp( const wxChar* str1, const wxChar* str2 )
  143. /***********************************************/
  144. /*
  145. * sort() function
  146. * Same as stricmp() but numbers in strings
  147. * are compared according to the value, not the ascii value of each digit
  148. */
  149. {
  150. return StrLenNumICmp( str1, str2, 32735 );
  151. }
  152. /**************************************************************/
  153. int StrLenNumICmp( const wxChar* str1, const wxChar* str2, int NbMax )
  154. /**************************************************************/
  155. /*
  156. * sort() function
  157. * Same as strnicmp() but numbers in strings
  158. * are compared according to the value, not the ascii value of each digit
  159. */
  160. {
  161. int i;
  162. int nb1 = 0, nb2 = 0;
  163. if( (str1 == NULL) || (str2 == NULL) )
  164. return 0;
  165. for( i = 0; i < NbMax; i++ )
  166. {
  167. if( isdigit( *str1 ) && isdigit( *str2 ) ) /* find number */
  168. {
  169. nb1 = 0; nb2 = 0;
  170. while( isdigit( *str1 ) )
  171. {
  172. nb1 = nb1 * 10 + *str1 - '0'; str1++;
  173. }
  174. while( isdigit( *str2 ) )
  175. {
  176. nb2 = nb2 * 10 + *str2 - '0'; str2++;
  177. }
  178. if( nb1 < nb2 )
  179. return -1;
  180. if( nb1 > nb2 )
  181. return 1;
  182. }
  183. if( toupper( *str1 ) < toupper( *str2 ) )
  184. return -1;
  185. if( toupper( *str1 ) > toupper( *str2 ) )
  186. return 1;
  187. if( (*str1 == 0 ) && ( *str2 == 0 ) )
  188. return 0;
  189. str1++; str2++;
  190. }
  191. return 0;
  192. }
  193. /***********************************************************************/
  194. bool WildCompareString( const wxString& pattern, const wxString& string_to_tst,
  195. bool case_sensitive )
  196. /***********************************************************************/
  197. /* compare a string to a pattern
  198. * ( usual chars * and ? allowed).
  199. * if case_sensitive == true, comparison is case sensitive
  200. * return true if match else false
  201. */
  202. {
  203. const wxChar* cp = NULL, * mp = NULL;
  204. const wxChar* wild, * string;
  205. wxString _pattern, _string_to_tst;
  206. if( case_sensitive )
  207. {
  208. wild = pattern.GetData();
  209. string = string_to_tst.GetData();
  210. }
  211. else
  212. {
  213. _pattern = pattern;
  214. _pattern.MakeUpper();
  215. _string_to_tst = string_to_tst;
  216. _string_to_tst.MakeUpper();
  217. wild = _pattern.GetData();
  218. string = _string_to_tst.GetData();
  219. }
  220. while( (*string) && (*wild != '*') )
  221. {
  222. if( (*wild != *string) && (*wild != '?') )
  223. return FALSE;
  224. wild++; string++;
  225. }
  226. while( *string )
  227. {
  228. if( *wild == '*' )
  229. {
  230. if( !*++wild )
  231. return 1;
  232. mp = wild;
  233. cp = string + 1;
  234. }
  235. else if( (*wild == *string) || (*wild == '?') )
  236. {
  237. wild++;
  238. string++;
  239. }
  240. else
  241. {
  242. wild = mp;
  243. string = cp++;
  244. }
  245. }
  246. while( *wild == '*' )
  247. {
  248. wild++;
  249. }
  250. return !*wild;
  251. }
  252. /***************************/
  253. char* to_point( char* Text )
  254. /**************************/
  255. /* convertit les , en . dans une chaine. utilise pour compenser
  256. * l'internalisation de la fct printf
  257. * qui genere les flottants avec une virgule au lieu du point
  258. * Obsolete: use SetLocaleTo_C_standard insteed
  259. */
  260. {
  261. char* line = Text;
  262. if( Text == NULL )
  263. return NULL;
  264. for( ; *Text != 0; Text++ )
  265. {
  266. if( *Text == ',' )
  267. *Text = '.';
  268. }
  269. return line;
  270. }
  271. /********************************/
  272. char* strupper( char* Text )
  273. /********************************/
  274. /* Change les caracteres 'a' ... 'z' en 'A' ... 'Z'. dans la chaine Text.
  275. * Retourne Text
  276. */
  277. {
  278. char* code = Text;
  279. if( Text )
  280. {
  281. while( *code )
  282. {
  283. if( (*code >= 'a') && (*code <= 'z') )
  284. *code += 'A' - 'a';
  285. code++;
  286. }
  287. }
  288. return Text;
  289. }