PageRenderTime 98ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mongo/util/text.h

https://github.com/RedBeard0531/mongo
C Header | 118 lines | 46 code | 25 blank | 47 comment | 2 complexity | b887806f9500081829625394334fc1b4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // text.h
  2. /*
  3. * Copyright 2010 10gen Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License, version 3,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * As a special exception, the copyright holders give permission to link the
  18. * code of portions of this program with the OpenSSL library under certain
  19. * conditions as described in each individual source file and distribute
  20. * linked combinations including the program with the OpenSSL library. You
  21. * must comply with the GNU Affero General Public License in all respects
  22. * for all of the code used other than as permitted herein. If you modify
  23. * file(s) with this exception, you may extend this exception to your
  24. * version of the file(s), but you are not obligated to do so. If you do not
  25. * wish to do so, delete this exception statement from your version. If you
  26. * delete this exception statement from all source files in the program,
  27. * then also delete it in the license file.
  28. */
  29. #pragma once
  30. #include <vector>
  31. #include <string>
  32. #include "mongo/base/disallow_copying.h"
  33. namespace mongo {
  34. class StringSplitter {
  35. public:
  36. /** @param big the std::string to be split
  37. @param splitter the delimiter
  38. */
  39. StringSplitter( const char * big , const char * splitter )
  40. : _big( big ) , _splitter( splitter ) {
  41. }
  42. /** @return true if more to be taken via next() */
  43. bool more() const { return _big[0] != 0; }
  44. /** get next split std::string fragment */
  45. std::string next();
  46. void split( std::vector<std::string>& l );
  47. std::vector<std::string> split();
  48. static std::vector<std::string> split( const std::string& big , const std::string& splitter );
  49. static std::string join( const std::vector<std::string>& l , const std::string& split );
  50. private:
  51. const char * _big;
  52. const char * _splitter;
  53. };
  54. /* This doesn't defend against ALL bad UTF8, but it will guarantee that the
  55. * std::string can be converted to sequence of codepoints. However, it doesn't
  56. * guarantee that the codepoints are valid.
  57. */
  58. bool isValidUTF8(const char *s);
  59. bool isValidUTF8(const std::string& s);
  60. // expect that n contains a base ten number and nothing else after it
  61. // NOTE win version hasn't been tested directly
  62. long long parseLL( const char *n );
  63. #if defined(_WIN32)
  64. std::string toUtf8String(const std::wstring& wide);
  65. std::wstring toWideString(const char *s);
  66. bool writeUtf8ToWindowsConsole( const char* utf8String, unsigned int utf8StringSize );
  67. /* like toWideString but UNICODE macro sensitive */
  68. # if !defined(_UNICODE)
  69. #error temp error
  70. inline std::string toNativeString(const char *s) { return s; }
  71. # else
  72. inline std::wstring toNativeString(const char *s) { return toWideString(s); }
  73. # endif
  74. class WindowsCommandLine {
  75. MONGO_DISALLOW_COPYING(WindowsCommandLine);
  76. char** _argv;
  77. char** _envp;
  78. public:
  79. WindowsCommandLine(int argc, wchar_t* argvW[], wchar_t* envpW[]);
  80. ~WindowsCommandLine();
  81. char** argv(void) const { return _argv; };
  82. char** envp(void) const { return _envp; };
  83. };
  84. #endif // #if defined(_WIN32)
  85. /**
  86. * Construct a Windows command line string, UTF-8 encoded, from a vector of
  87. * UTF-8 arguments, "argv".
  88. *
  89. * See "Parsing C++ Command-Line Arguments (C++)"
  90. * http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
  91. */
  92. std::string constructUtf8WindowsCommandLine(const std::vector<std::string>& argv);
  93. } // namespace mongo