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.

293 lines
9.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004, 2019 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 <string>
  31. #include <vector>
  32. #include <wx/string.h>
  33. #include <wx/filename.h>
  34. /**
  35. * Converts curly quotes and em/en dashes to straight quotes and dashes.
  36. * @param aString
  37. * @return true if any characters required conversion.
  38. */
  39. bool ConvertSmartQuotesAndDashes( wxString* aString );
  40. /**
  41. * Escape/Unescape routines to safely encode reserved-characters in various
  42. * contexts.
  43. */
  44. enum ESCAPE_CONTEXT
  45. {
  46. CTX_NETNAME,
  47. CTX_LIBID,
  48. CTX_QUOTED_STR,
  49. CTX_LINE,
  50. CTX_FILENAME
  51. };
  52. wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext );
  53. wxString UnescapeString( const wxString& aSource );
  54. /**
  55. * Remove markup (such as overbar or subscript) that we can't render to menu items.
  56. */
  57. wxString PrettyPrintForMenu( const wxString& aString );
  58. /**
  59. * Copy bytes from @a aSource delimited string segment to @a aDest buffer.
  60. *
  61. * The extracted string will be null terminated even if truncation is necessary
  62. * because aDestSize was not large enough.
  63. *
  64. * @param aDest is the destination byte buffer.
  65. * @param aSource is the source bytes as a C string.
  66. * @param aDestSize is the size of the destination byte buffer.
  67. * @return int - the number of bytes read from source, which may be more than
  68. * the number copied, due to escaping of double quotes and the escape byte itself.
  69. * @deprecated should use the one which fetches a wxString, below.
  70. */
  71. int ReadDelimitedText( char* aDest, const char* aSource, int aDestSize );
  72. /**
  73. * Copy bytes from @a aSource delimited string segment to @a aDest wxString.
  74. *
  75. * @param aDest is the destination wxString
  76. * @param aSource is the source C string holding utf8 encoded bytes.
  77. * @return int - the number of bytes read from source, which may be more than
  78. * the number copied, due to escaping of double quotes and the escape byte itself.
  79. */
  80. int ReadDelimitedText( wxString* aDest, const char* aSource );
  81. /**
  82. * Function EscapedUTF8
  83. * returns an 8 bit UTF8 string given aString in unicode form.
  84. * Any double quoted or back slashes are prefixed with a '\\' byte and the form
  85. * of this UTF8 byte string is compatible with function ReadDelimitedText().
  86. *
  87. * @param aString is the input string to convert.
  88. * @return std::string - the escaped input text, without the wrapping double quotes.
  89. */
  90. std::string EscapedUTF8( wxString aString );
  91. /**
  92. * Return a new wxString escaped for embedding in HTML.
  93. */
  94. wxString EscapedHTML( const wxString& aString );
  95. /**
  96. * Read one line line from \a aFile.
  97. *
  98. * @return a pointer the first useful line read by eliminating blank lines and comments.
  99. */
  100. char* GetLine( FILE* aFile, char* Line, int* LineNum = NULL, int SizeLine = 255 );
  101. /**
  102. * Remove leading and training spaces, tabs and end of line chars in \a text
  103. *
  104. * @return a pointer on the first n char in text
  105. */
  106. char* StrPurge( char* text );
  107. /**
  108. * @return a string giving the current date and time.
  109. */
  110. wxString DateAndTime();
  111. /**
  112. * Compare two strings with alphanumerical content.
  113. *
  114. * This function is equivalent to strncmp() or strncasecmp() if \a aIgnoreCase is true
  115. * except that strings containing numbers are compared by their integer value not
  116. * by their ASCII code. In other words U10 would be greater than U2.
  117. *
  118. * @param aString1 A wxString reference to the reference string.
  119. * @param aString2 A wxString reference to the comparison string.
  120. * @param aIgnoreCase Use true to make the comparison case insensitive.
  121. * @return An integer value of -1 if \a aString1 is less than \a aString2, 0 if
  122. * \a aString1 is equal to \a aString2, or 1 if \a aString1 is greater
  123. * than \a aString2.
  124. */
  125. int StrNumCmp( const wxString& aString1, const wxString& aString2, bool aIgnoreCase = false );
  126. /**
  127. * Compare a string against wild card (* and ?) pattern using the usual rules.
  128. *
  129. * @return true if pattern matched otherwise false.
  130. */
  131. bool WildCompareString( const wxString& pattern,
  132. const wxString& string_to_tst,
  133. bool case_sensitive = true );
  134. /**
  135. * Compare strings like the strcmp function but handle numbers and modifiers within the
  136. * string text correctly for sorting. eg. 1mF > 55uF
  137. *
  138. * @return -1 if first string is less than the second, 0 if the strings are equal, or
  139. * 1 if the first string is greater than the second.
  140. */
  141. int ValueStringCompare( wxString strFWord, wxString strSWord );
  142. /**
  143. * Breaks a string into three parts: he alphabetic preamble, the numeric part, and any
  144. * alphabetic ending.
  145. *
  146. * For example C10A is split to C 10 A
  147. */
  148. int SplitString( wxString strToSplit,
  149. wxString* strBeginning,
  150. wxString* strDigits,
  151. wxString* strEnd );
  152. /**
  153. * Gets the trailing int, if any, from a string.
  154. *
  155. * @param aStr the string to check
  156. * @return the trailing int or 0 if none found
  157. */
  158. int GetTrailingInt( const wxString& aStr );
  159. /**
  160. * @return a wxString object containing the illegal file name characters for all platforms.
  161. */
  162. wxString GetIllegalFileNameWxChars();
  163. /**
  164. * Checks \a aName for illegal file name characters.
  165. *
  166. * The Windows (DOS) file system forbidden characters already include the forbidden file
  167. * name characters for both Posix and OSX systems. The characters \/?*|"\<\> are illegal
  168. * and are replaced with %xx where xx the hexadecimal equivalent of the replaced character.
  169. * This replacement may not be as elegant as using an underscore ('_') or hyphen ('-') but
  170. * it guarantees that there will be no naming conflicts when fixing footprint library names.
  171. * however, if aReplaceChar is given, it will replace the illegal chars
  172. *
  173. * @param aName is a point to a std::string object containing the footprint name to verify.
  174. * @param aReplaceChar (if not 0) is the replacement char.
  175. * @return true if any characters have been replaced in \a aName.
  176. */
  177. bool ReplaceIllegalFileNameChars( std::string* aName, int aReplaceChar = 0 );
  178. bool ReplaceIllegalFileNameChars( wxString& aName, int aReplaceChar = 0 );
  179. #ifndef HAVE_STRTOKR
  180. // common/strtok_r.c optionally:
  181. extern "C" char* strtok_r( char* str, const char* delim, char** nextp );
  182. #endif
  183. /**
  184. * A helper for sorting strings from the rear. Useful for things like 3d model names
  185. * where they tend to be largely repetitious at the front.
  186. */
  187. struct rsort_wxString
  188. {
  189. bool operator() (const wxString& strA, const wxString& strB ) const
  190. {
  191. wxString::const_reverse_iterator sA = strA.rbegin();
  192. wxString::const_reverse_iterator eA = strA.rend();
  193. wxString::const_reverse_iterator sB = strB.rbegin();
  194. wxString::const_reverse_iterator eB = strB.rend();
  195. if( strA.empty() )
  196. {
  197. if( strB.empty() )
  198. return false;
  199. // note: this rule implies that a null string is first in the sort order
  200. return true;
  201. }
  202. if( strB.empty() )
  203. return false;
  204. while( sA != eA && sB != eB )
  205. {
  206. if( (*sA) == (*sB) )
  207. {
  208. ++sA;
  209. ++sB;
  210. continue;
  211. }
  212. if( (*sA) < (*sB) )
  213. return true;
  214. else
  215. return false;
  216. }
  217. if( sB == eB )
  218. return false;
  219. return true;
  220. }
  221. };
  222. /**
  223. * Splits the input string into a vector of output strings
  224. *
  225. * @param aStr - Input string with 0 or more delimiters
  226. * @param aDelim - The string of delimiter. Multiple characters here denote alternate delimiters
  227. *
  228. * @note Multiple delimiters are considered to be separate records with empty strings
  229. * @return a vector of strings
  230. */
  231. static inline std::vector<std::string> split( const std::string& aStr, const std::string& aDelim )
  232. {
  233. size_t pos = 0;
  234. size_t last_pos = 0;
  235. size_t len;
  236. std::vector<std::string> tokens;
  237. while( pos < aStr.size() )
  238. {
  239. pos = aStr.find_first_of( aDelim, last_pos );
  240. if( pos == std::string::npos )
  241. pos = aStr.size();
  242. len = pos - last_pos;
  243. tokens.push_back( aStr.substr( last_pos, len ) );
  244. last_pos = pos + 1;
  245. }
  246. return tokens;
  247. }
  248. #endif // KICAD_STRING_H_