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.

432 lines
10 KiB

  1. /*********************************************/
  2. /* string.cpp */
  3. /* some useful functions to handle strings */
  4. /*********************************************/
  5. #include "fctsys.h"
  6. #include "macros.h"
  7. #include "kicad_string.h"
  8. /* read a double-quote delimited text from source and put it in in dest,
  9. * read NbMaxChar bytes max
  10. * return the char count read from source
  11. */
  12. int ReadDelimitedText( char* dest, char* source, int NbMaxChar )
  13. {
  14. int ii, jj, flag = 0;
  15. for( ii = 0, jj = 0; ii < NbMaxChar - 1; jj++, source++ )
  16. {
  17. if( *source == 0 )
  18. break; /* E.O.L. */
  19. if( *source == '"' ) /* delimiter is " */
  20. {
  21. if( flag )
  22. break; /* End of delimited text */
  23. flag = 1; /* First delimiter found. */
  24. }
  25. else if( flag )
  26. {
  27. *dest = *source; dest++; ii++;
  28. }
  29. }
  30. *dest = 0; /* Null terminated */
  31. return jj;
  32. }
  33. /* Remove training spaces in text
  34. * return a pointer on the first non space char in text
  35. */
  36. char* StrPurge( char* text )
  37. {
  38. char* ptspace;
  39. if( text == NULL )
  40. return NULL;
  41. while( ( *text <= ' ' ) && *text )
  42. text++;
  43. ptspace = text + strlen( text ) - 1;
  44. while( ( *ptspace <= ' ' ) && *ptspace && ( ptspace >= text ) )
  45. {
  46. *ptspace = 0; ptspace--;
  47. }
  48. return text;
  49. }
  50. /* Read lines from File
  51. * Skip void lines and comments (starting by #)
  52. * return the first non void line.
  53. * increments *LineNum for each line
  54. */
  55. char* GetLine( FILE* File, char* Line, int* LineNum, int SizeLine )
  56. {
  57. do {
  58. if( fgets( Line, SizeLine, File ) == NULL )
  59. return NULL;
  60. if( LineNum )
  61. *LineNum += 1;
  62. } while( Line[0] == '#' || Line[0] == '\n' || Line[0] == '\r'
  63. || Line[0] == 0 );
  64. strtok( Line, "\n\r" );
  65. return Line;
  66. }
  67. /* return in aBuffer the date and time
  68. * time is the local time.
  69. */
  70. char* DateAndTime( char* aBuffer )
  71. {
  72. wxString datetime;
  73. datetime = DateAndTime();
  74. strcpy( aBuffer, CONV_TO_UTF8( datetime ) );
  75. return aBuffer;
  76. }
  77. /* return the date and time in a wxString
  78. * note: does the same thing than strftime()
  79. * time is the local time.
  80. */
  81. wxString DateAndTime()
  82. {
  83. wxString Line;
  84. wxDateTime datetime = wxDateTime::Now();
  85. datetime.SetCountry( wxDateTime::Country_Default );
  86. Line = datetime.Format( wxDefaultDateTimeFormat, wxDateTime::Local );
  87. return Line;
  88. }
  89. /*
  90. * sort() function
  91. * Same as strncmp() but numbers in strings
  92. * are compared according to the value, not the ascii value of each digit
  93. */
  94. int StrLenNumCmp( const wxChar* str1, const wxChar* str2, int NbMax )
  95. {
  96. int i;
  97. int nb1 = 0, nb2 = 0;
  98. if( ( str1 == NULL ) || ( str2 == NULL ) )
  99. return 0;
  100. for( i = 0; i < NbMax; i++ )
  101. {
  102. if( isdigit( *str1 ) && isdigit( *str2 ) ) /* digit found */
  103. {
  104. nb1 = 0; nb2 = 0;
  105. while( isdigit( *str1 ) )
  106. {
  107. nb1 = nb1 * 10 + *str1 - '0'; str1++;
  108. }
  109. while( isdigit( *str2 ) )
  110. {
  111. nb2 = nb2 * 10 + *str2 - '0'; str2++;
  112. }
  113. if( nb1 < nb2 )
  114. return -1;
  115. if( nb1 > nb2 )
  116. return 1;
  117. }
  118. if( *str1 < *str2 )
  119. return -1;
  120. if( *str1 > *str2 )
  121. return 1;
  122. if( ( *str1 == 0 ) && ( *str2 == 0 ) )
  123. return 0;
  124. str1++; str2++;
  125. }
  126. return 0;
  127. }
  128. /*
  129. * sort() function
  130. * Same as stricmp() but numbers in strings
  131. * are compared according to the value, not the ascii value of each digit
  132. */
  133. int StrNumICmp( const wxChar* str1, const wxChar* str2 )
  134. {
  135. return StrLenNumICmp( str1, str2, 32735 );
  136. }
  137. /*
  138. * sort() function
  139. * Same as strnicmp() but numbers in strings
  140. * are compared according to the value, not the ascii value of each digit
  141. */
  142. int StrLenNumICmp( const wxChar* str1, const wxChar* str2, int NbMax )
  143. {
  144. int i;
  145. int nb1 = 0, nb2 = 0;
  146. if( ( str1 == NULL ) || ( str2 == NULL ) )
  147. return 0;
  148. for( i = 0; i < NbMax; i++ )
  149. {
  150. if( isdigit( *str1 ) && isdigit( *str2 ) ) /* find number */
  151. {
  152. nb1 = 0; nb2 = 0;
  153. while( isdigit( *str1 ) )
  154. {
  155. nb1 = nb1 * 10 + *str1 - '0'; str1++;
  156. }
  157. while( isdigit( *str2 ) )
  158. {
  159. nb2 = nb2 * 10 + *str2 - '0'; str2++;
  160. }
  161. if( nb1 < nb2 )
  162. return -1;
  163. if( nb1 > nb2 )
  164. return 1;
  165. }
  166. if( toupper( *str1 ) < toupper( *str2 ) )
  167. return -1;
  168. if( toupper( *str1 ) > toupper( *str2 ) )
  169. return 1;
  170. if( (*str1 == 0 ) && ( *str2 == 0 ) )
  171. return 0;
  172. str1++; str2++;
  173. }
  174. return 0;
  175. }
  176. /* compare a string to a pattern
  177. * ( usual chars * and ? allowed).
  178. * if case_sensitive == true, comparison is case sensitive
  179. * return true if match else false
  180. */
  181. bool WildCompareString( const wxString& pattern, const wxString& string_to_tst,
  182. bool case_sensitive )
  183. {
  184. const wxChar* cp = NULL, * mp = NULL;
  185. const wxChar* wild, * string;
  186. wxString _pattern, _string_to_tst;
  187. if( case_sensitive )
  188. {
  189. wild = pattern.GetData();
  190. string = string_to_tst.GetData();
  191. }
  192. else
  193. {
  194. _pattern = pattern;
  195. _pattern.MakeUpper();
  196. _string_to_tst = string_to_tst;
  197. _string_to_tst.MakeUpper();
  198. wild = _pattern.GetData();
  199. string = _string_to_tst.GetData();
  200. }
  201. while( ( *string ) && ( *wild != '*' ) )
  202. {
  203. if( ( *wild != *string ) && ( *wild != '?' ) )
  204. return FALSE;
  205. wild++; string++;
  206. }
  207. while( *string )
  208. {
  209. if( *wild == '*' )
  210. {
  211. if( !*++wild )
  212. return 1;
  213. mp = wild;
  214. cp = string + 1;
  215. }
  216. else if( ( *wild == *string ) || ( *wild == '?' ) )
  217. {
  218. wild++;
  219. string++;
  220. }
  221. else
  222. {
  223. wild = mp;
  224. string = cp++;
  225. }
  226. }
  227. while( *wild == '*' )
  228. {
  229. wild++;
  230. }
  231. return !*wild;
  232. }
  233. /* Converts a string used to compensate for internalization of printf().
  234. * Generated floats with a comma instead of point.
  235. * Obsolete: use SetLocaleTo_C_standard instead
  236. */
  237. char* to_point( char* Text )
  238. {
  239. char* line = Text;
  240. if( Text == NULL )
  241. return NULL;
  242. for( ; *Text != 0; Text++ )
  243. {
  244. if( *Text == ',' )
  245. *Text = '.';
  246. }
  247. return line;
  248. }
  249. /* Convert string to upper case.
  250. * Returns pointer to the converted string.
  251. */
  252. char* strupper( char* Text )
  253. {
  254. char* code = Text;
  255. if( Text )
  256. {
  257. while( *code )
  258. {
  259. if( ( *code >= 'a' ) && ( *code <= 'z' ) )
  260. *code += 'A' - 'a';
  261. code++;
  262. }
  263. }
  264. return Text;
  265. }
  266. int RefDesStringCompare( const wxString& strFWord, const wxString& strSWord )
  267. {
  268. // The different sections of the first string
  269. wxString strFWordBeg, strFWordMid, strFWordEnd;
  270. // The different sections of the second string
  271. wxString strSWordBeg, strSWordMid, strSWordEnd;
  272. int isEqual = 0; // The numerical results of a string compare
  273. int iReturn = 0; // The variable that is being returned
  274. long lFirstDigit = 0; /* The converted middle section of the first
  275. *string */
  276. long lSecondDigit = 0; /* The converted middle section of the second
  277. *string */
  278. // Split the two strings into separate parts
  279. SplitString( strFWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
  280. SplitString( strSWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
  281. // Compare the Beginning section of the strings
  282. isEqual = strFWordBeg.CmpNoCase( strSWordBeg );
  283. if( isEqual > 0 )
  284. iReturn = 1;
  285. else if( isEqual < 0 )
  286. iReturn = -1;
  287. else
  288. {
  289. // If the first sections are equal compare their digits
  290. strFWordMid.ToLong( &lFirstDigit );
  291. strSWordMid.ToLong( &lSecondDigit );
  292. if( lFirstDigit > lSecondDigit )
  293. iReturn = 1;
  294. else if( lFirstDigit < lSecondDigit )
  295. iReturn = -1;
  296. else
  297. {
  298. // If the first two sections are equal compare the endings
  299. isEqual = strFWordEnd.CmpNoCase( strSWordEnd );
  300. if( isEqual > 0 )
  301. iReturn = 1;
  302. else if( isEqual < 0 )
  303. iReturn = -1;
  304. else
  305. iReturn = 0;
  306. }
  307. }
  308. return iReturn;
  309. }
  310. int SplitString( wxString strToSplit,
  311. wxString* strBeginning,
  312. wxString* strDigits,
  313. wxString* strEnd )
  314. {
  315. // Clear all the return strings
  316. strBeginning->Empty();
  317. strDigits->Empty();
  318. strEnd->Empty();
  319. // There no need to do anything if the string is empty
  320. if( strToSplit.length() == 0 )
  321. return 0;
  322. // Starting at the end of the string look for the first digit
  323. int ii;
  324. for( ii = (strToSplit.length() - 1); ii >= 0; ii-- )
  325. {
  326. if( isdigit( strToSplit[ii] ) )
  327. break;
  328. }
  329. // If there were no digits then just set the single string
  330. if( ii < 0 )
  331. *strBeginning = strToSplit;
  332. else
  333. {
  334. // Since there is at least one digit this is the trailing string
  335. *strEnd = strToSplit.substr( ii + 1 );
  336. // Go to the end of the digits
  337. int position = ii + 1;
  338. for( ; ii >= 0; ii-- )
  339. {
  340. if( !isdigit( strToSplit[ii] ) )
  341. break;
  342. }
  343. // If all that was left was digits, then just set the digits string
  344. if( ii < 0 )
  345. *strDigits = strToSplit.substr( 0, position );
  346. /* We were only looking for the last set of digits everything else is
  347. *part of the preamble */
  348. else
  349. {
  350. *strDigits = strToSplit.substr( ii + 1, position - ii - 1 );
  351. *strBeginning = strToSplit.substr( 0, ii + 1 );
  352. }
  353. }
  354. return 0;
  355. }