pathparse.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef _PATHPARSE_H
  2. #define _PATHPARSE_H
  3. #include <bfc/ptrlist.h>
  4. #include <bfc/string/bfcstring.h>
  5. #include <bfc/string/StringW.h>
  6. /**
  7. PathParser is a class that parses a DOS/Windows (example: C:\DOS)
  8. style path as well as a UNIX style path (example: /usr/local/bin/).
  9. @short Path parser for Windows and UNIX style paths.
  10. @author Nullsoft
  11. @ver 1.0
  12. */
  13. class PathParser
  14. {
  15. public:
  16. /**
  17. When PathParser is instantiated, the contructor takes the path to
  18. parse and the separators to use to parse the path. It will then
  19. parse the path using the separators and make the parsed elements
  20. available. If no separators are given \ and / are used.
  21. @param str The path to parse.
  22. @param separators String that contains the separators to use. Single character separators only.
  23. No delimiters between separators in the string.
  24. */
  25. PathParser(const char *_str, const char *sep = "\\/", int uniquestrs = 0);
  26. void setString(const char *string, const char *separators = "\\/");
  27. /**
  28. Gets the number of path elements found in the path.
  29. @ret The number of path elements found.
  30. */
  31. int getNumStrings();
  32. /**
  33. Gets the number of path elements found in the path.
  34. @ret The number of path elements found.
  35. */
  36. char *enumString(int i);
  37. char *enumStringSafe(int i, char *def_val="");
  38. /**
  39. Gets the last path element from the parsed path.
  40. @ret The last path element from the parsed path.
  41. */
  42. char *getLastString() { return enumString(getNumStrings()-1); }
  43. protected:
  44. /**
  45. Override available for pre-processing of the
  46. string before it's split. This is done at the start
  47. of the process() call.
  48. @param str A reference to the string to pre-process.
  49. */
  50. virtual void preProcess(String &str) { }
  51. /**
  52. Override available for post-processing of the pieces
  53. of the command line that are obtained after it's
  54. been split.
  55. @param str The command line piece to post-process.
  56. */
  57. virtual void postProcess(char *str) { }
  58. private:
  59. void process();
  60. int processed;
  61. String str;
  62. String separators;
  63. PtrList<char> strings;
  64. int uniques;
  65. };
  66. class PathParserW
  67. {
  68. public:
  69. /**
  70. When PathParser is instantiated, the contructor takes the path to
  71. parse and the separators to use to parse the path. It will then
  72. parse the path using the separators and make the parsed elements
  73. available. If no separators are given \ and / are used.
  74. @param str The path to parse.
  75. @param separators String that contains the separators to use. Single character separators only.
  76. No delimiters between separators in the string.
  77. */
  78. PathParserW(const wchar_t *_str, const wchar_t *sep = L"\\/", int uniquestrs = 0);
  79. void setString(const wchar_t *string, const wchar_t *separators = L"\\/");
  80. /**
  81. Gets the number of path elements found in the path.
  82. @ret The number of path elements found.
  83. */
  84. int getNumStrings();
  85. /**
  86. Gets the number of path elements found in the path.
  87. @ret The number of path elements found.
  88. */
  89. wchar_t *enumString(int i);
  90. wchar_t *enumStringSafe(int i, wchar_t *def_val=L"");
  91. /**
  92. Gets the last path element from the parsed path.
  93. @ret The last path element from the parsed path.
  94. */
  95. wchar_t *getLastString() { return enumString(getNumStrings()-1); }
  96. protected:
  97. /**
  98. Override available for pre-processing of the
  99. string before it's split. This is done at the start
  100. of the process() call.
  101. @param str A reference to the string to pre-process.
  102. */
  103. virtual void preProcess(StringW &str) { }
  104. /**
  105. Override available for post-processing of the pieces
  106. of the command line that are obtained after it's
  107. been split.
  108. @param str The command line piece to post-process.
  109. */
  110. virtual void postProcess(wchar_t *str) { }
  111. private:
  112. void process();
  113. int processed;
  114. StringW str;
  115. StringW separators;
  116. PtrList<wchar_t> strings;
  117. int uniques;
  118. };
  119. #endif