diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2014-06-20 21:31:41 -0700 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2014-06-20 21:31:41 -0700 |
commit | df9b8f8ae7a540988c20fdffaaff2442c349873a (patch) | |
tree | ff9be69934b6896c8d7e91412b63dd9cd9b37341 | |
parent | fcc66ce9f5d6db995536beab5539fd47e490bf14 (diff) |
Moving tokenize, percentEncode and percentDecode around. Fixing a few warnings. Workarounding c-ares and pthreads compilation issue with unicode.
-rw-r--r-- | includes/BString.h | 3 | ||||
-rw-r--r-- | includes/CurlTask.h | 4 | ||||
-rw-r--r-- | includes/Http.h | 3 | ||||
-rw-r--r-- | src/BString.cc | 22 | ||||
-rw-r--r-- | src/CurlTask.cc | 81 | ||||
-rw-r--r-- | src/Http.cc | 68 | ||||
-rw-r--r-- | src/SimpleMustache.cc | 6 | ||||
-rw-r--r-- | win32/project/Balau.vcxproj | 7 | ||||
-rw-r--r-- | win32/project/c-ares.vcxproj | 2 | ||||
-rw-r--r-- | win32/project/pthreads.vcxproj | 2 |
10 files changed, 106 insertions, 92 deletions
diff --git a/includes/BString.h b/includes/BString.h index 0a5fceb..dc9fd5d 100644 --- a/includes/BString.h +++ b/includes/BString.h @@ -134,6 +134,9 @@ class String : private std::string { bool isEmpty() { return std::string::empty(); } + std::vector<String> tokenize(const String & delimiters = "&", bool trimEmpty = true); + + using std::string::npos; using std::string::size_type; }; diff --git a/includes/CurlTask.h b/includes/CurlTask.h index f1aba65..c5c085c 100644 --- a/includes/CurlTask.h +++ b/includes/CurlTask.h @@ -12,10 +12,6 @@ class CurlTask : public StacklessTask { virtual ~CurlTask(); friend class TaskMan; - static String percentEncode(const String & src); - static String percentDecode(const String & src); - static std::vector<String> tokenize(const String & str, const String & delimiters = "&", bool trimEmpty = true); - virtual void prepareRequest() { } protected: diff --git a/includes/Http.h b/includes/Http.h index f0651f7..58edd23 100644 --- a/includes/Http.h +++ b/includes/Http.h @@ -15,8 +15,9 @@ namespace Balau { namespace Http { const char * getStatusMsg(int httpStatus); - const char * getContentType(const String & extension); +String percentEncode(const String & src); +String percentDecode(const String & src); typedef std::map<String, String> StringMap; typedef std::multimap<String, String> StringMultiMap; diff --git a/src/BString.cc b/src/BString.cc index 4ba3601..5f4b868 100644 --- a/src/BString.cc +++ b/src/BString.cc @@ -154,3 +154,25 @@ Balau::String::List Balau::String::split(char c) { free(d); return r; } + +std::vector<Balau::String> Balau::String::tokenize(const String & delimiters, bool trimEmpty) { + std::vector<String> tokens; + size_t pos, lastPos = 0; + for (;;) { + pos = find_first_of(delimiters, lastPos); + if (pos == String::npos) { + pos = strlen(); + + if ((pos != lastPos) || !trimEmpty) + tokens.push_back(extract(lastPos, pos)); + + return tokens; + } + else { + if ((pos != lastPos) || !trimEmpty) + tokens.push_back(extract(lastPos, pos)); + } + + lastPos = pos + 1; + } +} diff --git a/src/CurlTask.cc b/src/CurlTask.cc index 2a792f9..2c37514 100644 --- a/src/CurlTask.cc +++ b/src/CurlTask.cc @@ -38,87 +38,6 @@ int Balau::CurlTask::debugFunctionStatic(CURL * easy, curl_infotype info, char * return curlTask->debugFunction(info, str, str_len); } -Balau::String Balau::CurlTask::percentEncode(const String & src) { - const size_t size = src.strlen(); - String ret; - ret.reserve(size); - - static char toHex[] = "0123456789ABCDEF"; - - for (size_t i = 0; i < size; i++) { - char c = src[i]; - if (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) { - ret += c; - } else { - ret += '%'; - ret += toHex[c >> 4]; - ret += toHex[c & 15]; - } - } - - return ret; -} - -Balau::String Balau::CurlTask::percentDecode(const String & src) { - const size_t size = src.strlen(); - String ret; - ret.reserve(size); - - for (size_t i = 0; i < size; i++) { - char c = src[i]; - if (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) { - ret += c; - } else if ((c == '%') && ((i + 2) < size)) { - char h1 = src[i + 1]; - char h2 = src[i + 2]; - if ((h1 >= '0') && (h1 <= '9')) { - c = h1 - '0'; - } else if ((h1 >= 'A') && (h1 <= 'F')) { - c = h1 - 'A' + 10; - } else { - // invalid - return ret; - } - c <<= 4; - if ((h2 >= '0') && (h2 <= '9')) { - c |= h2 - '0'; - } else if ((h2 >= 'A') && (h2 <= 'F')) { - c |= h2 - 'A' + 10; - } else { - // invalid - return ret; - } - i += 2; - } else { - // invalid - return ret; - } - } - - return ret; -} - -std::vector<Balau::String> Balau::CurlTask::tokenize(const String & str, const String & delimiters, bool trimEmpty) { - std::vector<String> tokens; - size_t pos, lastPos = 0; - for (;;) { - pos = str.find_first_of(delimiters, lastPos); - if (pos == String::npos) { - pos = str.strlen(); - - if ((pos != lastPos) || !trimEmpty) - tokens.push_back(str.extract(lastPos, pos)); - - return tokens; - } else { - if ((pos != lastPos) || !trimEmpty) - tokens.push_back(str.extract(lastPos, pos)); - } - - lastPos = pos + 1; - } -} - Balau::DownloadTask::DownloadTask(const Balau::String & url) { curl_easy_setopt(m_curlHandle, CURLOPT_URL, url.to_charp()); m_name.set("DownloadTask(%s)", url.to_charp()); diff --git a/src/Http.cc b/src/Http.cc index 10c3430..c645836 100644 --- a/src/Http.cc +++ b/src/Http.cc @@ -72,3 +72,71 @@ static const std::map<const Balau::String, const char *> s_mimeMap { const char * Balau::Http::getContentType(const String & extension) { return getMap<const Balau::String>(s_mimeMap, extension, "application/octet-stream"); } + + +Balau::String Balau::Http::percentEncode(const String & src) { + const size_t size = src.strlen(); + String ret; + ret.reserve(size); + + static char toHex[] = "0123456789ABCDEF"; + + for (size_t i = 0; i < size; i++) { + char c = src[i]; + if (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) { + ret += c; + } + else { + ret += '%'; + ret += toHex[c >> 4]; + ret += toHex[c & 15]; + } + } + + return ret; +} + +Balau::String Balau::Http::percentDecode(const String & src) { + const size_t size = src.strlen(); + String ret; + ret.reserve(size); + + for (size_t i = 0; i < size; i++) { + char c = src[i]; + if (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) { + ret += c; + } + else if ((c == '%') && ((i + 2) < size)) { + char h1 = src[i + 1]; + char h2 = src[i + 2]; + if ((h1 >= '0') && (h1 <= '9')) { + c = h1 - '0'; + } + else if ((h1 >= 'A') && (h1 <= 'F')) { + c = h1 - 'A' + 10; + } + else { + // invalid + return ret; + } + c <<= 4; + if ((h2 >= '0') && (h2 <= '9')) { + c |= h2 - '0'; + } + else if ((h2 >= 'A') && (h2 <= 'F')) { + c |= h2 - 'A' + 10; + } + else { + // invalid + return ret; + } + i += 2; + } + else { + // invalid + return ret; + } + } + + return ret; +} diff --git a/src/SimpleMustache.cc b/src/SimpleMustache.cc index 557a183..bad2ab0 100644 --- a/src/SimpleMustache.cc +++ b/src/SimpleMustache.cc @@ -65,7 +65,7 @@ Balau::SimpleMustache::Context & Balau::SimpleMustache::Context::Proxy::operator m_idx = ctxLst.size() + m_idx + 1; if (m_idx <= 0) m_idx = 1; - if (ctxLst.size() < m_idx) + if (ctxLst.size() < static_cast<size_t>(m_idx)) ctxLst.resize(m_idx); SubContext & subCtx = ctxLst[m_idx - 1]; SubContext::iterator s = subCtx.find(key); @@ -447,7 +447,7 @@ Balau::SimpleMustache::Fragments::const_iterator Balau::SimpleMustache::render_r Balau::String Balau::SimpleMustache::escape(const String & s) { int size = 0; - for (int i = 0; i < s.strlen(); i++) { + for (unsigned i = 0; i < s.strlen(); i++) { switch (s[i]) { case '&': size += 5; @@ -476,7 +476,7 @@ Balau::String Balau::SimpleMustache::escape(const String & s) { char * t = (char *) malloc(size + 1); char * p = t; - for (int i = 0; i < s.strlen(); i++) { + for (unsigned i = 0; i < s.strlen(); i++) { switch (s[i]) { case '&': *p++ = '&'; diff --git a/win32/project/Balau.vcxproj b/win32/project/Balau.vcxproj index e08f09f..07d6277 100644 --- a/win32/project/Balau.vcxproj +++ b/win32/project/Balau.vcxproj @@ -256,7 +256,12 @@ <ClCompile Include="..\..\src\TaskMan.cc" />
<ClCompile Include="..\..\src\Threads.cc" />
<ClCompile Include="..\..\src\ZHandle.cc" />
- <ClCompile Include="..\regex\msvc-regex.c" />
+ <ClCompile Include="..\regex\msvc-regex.c">
+ <DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4290;4800;4018;4090;4047;4101</DisableSpecificWarnings>
+ <DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4290;4800;4018;4090;4047;4101</DisableSpecificWarnings>
+ <DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4290;4800;4018;4090;4047;4101</DisableSpecificWarnings>
+ <DisableSpecificWarnings Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4290;4800;4018;4090;4047;4101</DisableSpecificWarnings>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\includes\Async.h" />
diff --git a/win32/project/c-ares.vcxproj b/win32/project/c-ares.vcxproj index 342516e..2276ca5 100644 --- a/win32/project/c-ares.vcxproj +++ b/win32/project/c-ares.vcxproj @@ -27,7 +27,7 @@ <ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
- <CharacterSet>Unicode</CharacterSet>
+ <CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
diff --git a/win32/project/pthreads.vcxproj b/win32/project/pthreads.vcxproj index 7ea7c3d..7820ce8 100644 --- a/win32/project/pthreads.vcxproj +++ b/win32/project/pthreads.vcxproj @@ -27,7 +27,7 @@ <ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
- <CharacterSet>Unicode</CharacterSet>
+ <CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
|