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.

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