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.

248 lines
8.3 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 KiCad Developers, see change_log.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file kicad_string.h
  25. * @see common.h, string.cpp
  26. */
  27. #ifndef KICAD_STRING_H_
  28. #define KICAD_STRING_H_
  29. #include "config.h"
  30. #include <wx/string.h>
  31. #include <wx/filename.h>
  32. /**
  33. * These Escape/Unescape routines use HTML-entity-reference-style encoding to handle
  34. * characters which are:
  35. * (a) not legal in filenames
  36. * (b) used as control characters in LIB_IDs
  37. * (c) used to delineate hierarchical paths
  38. */
  39. wxString EscapeString( const wxString& aSource );
  40. wxString UnescapeString( const wxString& aSource );
  41. /**
  42. * Function ReadDelimitedText
  43. * copies bytes from @a aSource delimited string segment to @a aDest buffer.
  44. * The extracted string will be null terminated even if truncation is necessary
  45. * because aDestSize was not large enough.
  46. *
  47. * @param aDest is the destination byte buffer.
  48. * @param aSource is the source bytes as a C string.
  49. * @param aDestSize is the size of the destination byte buffer.
  50. * @return int - the number of bytes read from source, which may be more than
  51. * the number copied, due to escaping of double quotes and the escape byte itself.
  52. * @deprecated should use the one which fetches a wxString, below.
  53. */
  54. int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
  55. /**
  56. * Function ReadDelimitedText
  57. * copies bytes from @a aSource delimited string segment to @a aDest wxString.
  58. *
  59. * @param aDest is the destination wxString
  60. * @param aSource is the source C string holding utf8 encoded bytes.
  61. * @return int - the number of bytes read from source, which may be more than
  62. * the number copied, due to escaping of double quotes and the escape byte itself.
  63. */
  64. int ReadDelimitedText( wxString* aDest, const char* aSource );
  65. /**
  66. * Function EscapedUTF8
  67. * returns an 8 bit UTF8 string given aString in unicode form.
  68. * Any double quoted or back slashes are prefixed with a '\\' byte and the form
  69. * of this UTF8 byte string is compatible with function ReadDelimitedText().
  70. *
  71. * @param aString is the input string to convert.
  72. * @return std::string - the escaped input text, without the wrapping double quotes.
  73. */
  74. std::string EscapedUTF8( const wxString& aString );
  75. /**
  76. * Return a new wxString escaped for embedding in HTML.
  77. */
  78. wxString EscapedHTML( const wxString& aString );
  79. /**
  80. * Function GetLine
  81. * reads one line line from \a aFile.
  82. * @return A pointer the first useful line read by eliminating blank lines and comments.
  83. */
  84. char* GetLine( FILE* aFile, char* Line, int* LineNum = NULL, int SizeLine = 255 );
  85. /**
  86. * Function StrPurge
  87. * removes leading and training spaces, tabs and end of line chars in \a text
  88. * return a pointer on the first n char in text
  89. */
  90. char* StrPurge( char* text );
  91. /**
  92. * Function DateAndTime
  93. * @return a string giving the current date and time.
  94. */
  95. wxString DateAndTime();
  96. /**
  97. * Function StrNumCmp
  98. * is a routine compatible with qsort() to sort by alphabetical order.
  99. *
  100. * This function is equivalent to strncmp() or strncasecmp() if \a aIgnoreCase is true
  101. * except that strings containing numbers are compared by their integer value not
  102. * by their ASCII code.
  103. *
  104. * @param aString1 A wxString reference to the reference string.
  105. * @param aString2 A wxString reference to the comparison string.
  106. * @param aLength The number of characters to compare. Set to -1 to compare
  107. * the entire string.
  108. * @param aIgnoreCase Use true to make the comparison case insensitive.
  109. * @return An integer value of -1 if \a aString1 is less than \a aString2, 0 if
  110. * \a aString1 is equal to \a aString2, or 1 if \a aString1 is greater
  111. * than \a aString2.
  112. */
  113. int StrNumCmp( const wxString& aString1, const wxString& aString2, int aLength = INT_MAX,
  114. bool aIgnoreCase = false );
  115. /**
  116. * Function WildCompareString
  117. * compares a string against wild card (* and ?) pattern using the usual rules.
  118. * @return true if pattern matched otherwise false.
  119. */
  120. bool WildCompareString( const wxString& pattern,
  121. const wxString& string_to_tst,
  122. bool case_sensitive = true );
  123. /**
  124. * Function ValueStringCompare
  125. * acts just like the strcmp function but handles numbers and modifiers within the
  126. * string text correctly for sorting. eg. 1mF > 55uF
  127. * return -1 if first string is less than the second
  128. * return 0 if the strings are equal
  129. * return 1 if the first string is greater than the second
  130. */
  131. int ValueStringCompare( wxString strFWord, wxString strSWord );
  132. /**
  133. * Function SplitString
  134. * breaks a string into three parts.
  135. * The alphabetic preamble
  136. * The numeric part
  137. * Any alphabetic ending
  138. * For example C10A is split to C 10 A
  139. */
  140. int SplitString( wxString strToSplit,
  141. wxString* strBeginning,
  142. wxString* strDigits,
  143. wxString* strEnd );
  144. /**
  145. * Gets the trailing int, if any, from a string.
  146. * @param aStr the string to check
  147. * @return the trailing int or 0 if none found
  148. */
  149. int GetTrailingInt( const wxString& aStr );
  150. /**
  151. * Function GetIllegalFileNameWxChars
  152. * @return a wString object containing the illegal file name characters for all platforms.
  153. */
  154. wxString GetIllegalFileNameWxChars();
  155. /**
  156. * Function ReplaceIllegalFileNameChars
  157. * checks \a aName for illegal file name characters.
  158. *
  159. * The Windows (DOS) file system forbidden characters already include the forbidden file
  160. * name characters for both Posix and OSX systems. The characters \/?*|"\<\> are illegal
  161. * and are replaced with %xx where xx the hexadecimal equivalent of the replaced character.
  162. * This replacement may not be as elegant as using an underscore ('_') or hyphen ('-') but
  163. * it guarentees that there will be no naming conflicts when fixing footprint library names.
  164. * however, if aReplaceChar is given, it will replace the illegal chars
  165. *
  166. * @param aName is a point to a std::string object containing the footprint name to verify.
  167. * @param aReplaceChar (if not 0) is the replacement char.
  168. * @return true if any characters have been replaced in \a aName.
  169. */
  170. bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar = 0 );
  171. bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
  172. #ifndef HAVE_STRTOKR
  173. // common/strtok_r.c optionally:
  174. extern "C" char* strtok_r( char* str, const char* delim, char** nextp );
  175. #endif
  176. /**
  177. * A helper for sorting strings from the rear. Useful for things like 3d model names
  178. * where they tend to be largely repititous at the front.
  179. */
  180. struct rsort_wxString
  181. {
  182. bool operator() (const wxString& strA, const wxString& strB ) const
  183. {
  184. wxString::const_reverse_iterator sA = strA.rbegin();
  185. wxString::const_reverse_iterator eA = strA.rend();
  186. wxString::const_reverse_iterator sB = strB.rbegin();
  187. wxString::const_reverse_iterator eB = strB.rend();
  188. if( strA.empty() )
  189. {
  190. if( strB.empty() )
  191. return false;
  192. // note: this rule implies that a null string is first in the sort order
  193. return true;
  194. }
  195. if( strB.empty() )
  196. return false;
  197. while( sA != eA && sB != eB )
  198. {
  199. if( (*sA) == (*sB) )
  200. {
  201. ++sA;
  202. ++sB;
  203. continue;
  204. }
  205. if( (*sA) < (*sB) )
  206. return true;
  207. else
  208. return false;
  209. }
  210. if( sB == eB )
  211. return false;
  212. return true;
  213. }
  214. };
  215. #endif // KICAD_STRING_H_