http://git-wip-us.apache.org/repos/asf/kylin/blob/3c2329dc/odbc/Common/REST.cpp ---------------------------------------------------------------------- diff --git a/odbc/Common/REST.cpp b/odbc/Common/REST.cpp index 15921aa..bc3a6b8 100644 --- a/odbc/Common/REST.cpp +++ b/odbc/Common/REST.cpp @@ -15,8 +15,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - + + #include <cpprest/http_client.h> #include <cpprest/filestream.h> #include <cpprest/json.h> @@ -45,9 +45,10 @@ using namespace concurrency::streams; using namespace web; using namespace web::json; -void printLog ( const char* msg ) { - time_t now = time ( 0 ); - struct tm tstruct; +void printLog ( const char* msg ) +{ + time_t now = time ( 0 ); + struct tm tstruct; char buffer[100]; tstruct = *localtime ( &now ); strftime ( buffer, 100, "%Y-%m-%d.%X", &tstruct ); @@ -63,17 +64,21 @@ void printLog ( const char* msg ) { /// <param name="results"></param> /// <param name="column"></param> /// <returns></returns> -int ScanForLength ( std::vector<SQLRowContent*> results, int column ) { +int ScanForLength ( std::vector <SQLRowContent*> results, int column ) +{ int max = 0; - - for ( auto p = results.begin(); p < results.end(); p++ ) { + + for ( auto p = results . begin (); p < results . end (); p++ ) + { SQLRowContent* result = *p; - int length = result->contents[column].size(); - + int length = result -> contents[column] . size (); + if ( length > max ) - { max = length; } + { + max = length; + } } - + return max; } @@ -84,299 +89,355 @@ int ScanForLength ( std::vector<SQLRowContent*> results, int column ) { /// <param name="results"></param> /// <param name="column"></param> /// <returns></returns> -int ScanForScale ( std::vector<SQLRowContent*> results, int column ) { +int ScanForScale ( std::vector <SQLRowContent*> results, int column ) +{ int max = 0; - - for ( auto p = results.begin(); p < results.end(); p++ ) { + + for ( auto p = results . begin (); p < results . end (); p++ ) + { SQLRowContent* result = *p; - int length = result->contents[column].size(); - int dotLocation = result->contents[column].find ( L"." ); - - if ( dotLocation != string::npos ) { + int length = result -> contents[column] . size (); + int dotLocation = result -> contents[column] . find ( L"." ); + + if ( dotLocation != string::npos ) + { int scale = length - 1 - dotLocation; - + if ( scale > max ) - { max = scale; } + { + max = scale; + } } } - + return max; } -void overwrite ( SQLResponse* res ) { - for ( int i = 0; i < ( int ) res->columnMetas.size(); ++i ) { - SelectedColumnMeta* meta = res->columnMetas[i]; - ODBCTypes t = ( ODBCTypes ) meta->columnType; +void overwrite ( SQLResponse* res ) +{ + for ( int i = 0; i < ( int ) res -> columnMetas . size (); ++i ) + { + SelectedColumnMeta* meta = res -> columnMetas[i]; + ODBCTypes t = ( ODBCTypes ) meta -> columnType; int scale = 0; int length = 0; - - switch ( t ) { - case ODBCTypes::ODBC_Numeric: - case ODBCTypes::ODBC_Decimal: - case ODBCTypes::ODBC_Double: - case ODBCTypes::ODBC_Real: - case ODBCTypes::ODBC_Float: - scale = ScanForScale ( res->results, i ); - meta->scale = scale; - meta->scale = 4; + + switch ( t ) + { + case ODBCTypes::ODBC_Numeric : + case ODBCTypes::ODBC_Decimal : + case ODBCTypes::ODBC_Double : + case ODBCTypes::ODBC_Real : + case ODBCTypes::ODBC_Float : + scale = ScanForScale ( res -> results, i ); + meta -> scale = scale; + meta -> scale = 4; break; - - case ODBCTypes::ODBC_Char: - case ODBCTypes::ODBC_VarChar: - case ODBCTypes::ODBC_LongVarChar: - case ODBCTypes::ODBC_WChar: - case ODBCTypes::ODBC_WVarChar: - case ODBCTypes::ODBC_WLongVarChar: - case ODBCTypes::ODBC_DateTime: - case ODBCTypes::ODBC_Type_Date: - case ODBCTypes::ODBC_Type_Time: - case ODBCTypes::ODBC_Type_Timestamp: - length = ScanForLength ( res->results, i ); - meta->displaySize = length; - meta->precision = length; + + case ODBCTypes::ODBC_Char : + case ODBCTypes::ODBC_VarChar : + case ODBCTypes::ODBC_LongVarChar : + case ODBCTypes::ODBC_WChar : + case ODBCTypes::ODBC_WVarChar : + case ODBCTypes::ODBC_WLongVarChar : + case ODBCTypes::ODBC_DateTime : + case ODBCTypes::ODBC_Type_Date : + case ODBCTypes::ODBC_Type_Time : + case ODBCTypes::ODBC_Type_Timestamp : + length = ScanForLength ( res -> results, i ); + meta -> displaySize = length; + meta -> precision = length; break; - - default: + + default : break; } } } -std::wstring completeServerStr ( char* serverStr, long port ) { +std::wstring completeServerStr ( char* serverStr, long port ) +{ //concat the whole server string char completeServerAddr[256]; char portSuffix[10]; sprintf ( portSuffix, ":%d", port ); - + if ( strstr ( serverStr, "https://" ) == serverStr || - strstr ( serverStr, "http://" ) == serverStr ) { + strstr ( serverStr, "http://" ) == serverStr ) + { sprintf ( completeServerAddr, "%s", serverStr ); } - - else { + + else + { // by default use https sprintf ( completeServerAddr, "https://%s", serverStr ); } - - if ( strstr ( serverStr, portSuffix ) == NULL ) { + + if ( strstr ( serverStr, portSuffix ) == NULL ) + { strcat ( completeServerAddr, portSuffix ); } - + return string2wstring ( std::string ( completeServerAddr ) ); } -http_request makeRequest ( const char* username, const char* passwd, const wchar_t* uriStr , http::method method ) { +http_request makeRequest ( const char* username, const char* passwd, const wchar_t* uriStr, http::method method ) +{ http_request request; char s[128]; sprintf ( s, "%s:%s", username, passwd ); std::string b64 = base64_encode ( ( unsigned char const* ) s, strlen ( s ) ); - request.set_method ( method ); - request.set_request_uri ( uri ( uri::encode_uri ( uriStr ) ) ); - request.headers().add ( header_names::authorization, string2wstring ( "Basic " + b64 ) ); - request.headers().add ( header_names::content_type, "application/json" ); + request . set_method ( method ); + request . set_request_uri ( uri ( uri::encode_uri ( uriStr ) ) ); + request . headers () . add ( header_names::authorization, string2wstring ( "Basic " + b64 ) ); + request . headers () . add ( header_names::content_type, "application/json" ); return request; } -bool restAuthenticate ( char* serverAddr, long port, char* username, char* passwd ) { - wstring serverAddrW = completeServerStr ( serverAddr, port ) ; +bool restAuthenticate ( char* serverAddr, long port, char* username, char* passwd ) +{ + wstring serverAddrW = completeServerStr ( serverAddr, port ); http_client_config config; - config.set_timeout ( utility::seconds ( 300 ) ); + config . set_timeout ( utility::seconds ( 300 ) ); http_client session ( serverAddrW, config ); //can get project list only when correct username/password is given http_request request = makeRequest ( username, passwd, L"/kylin/api/projects", methods::GET ); - http_response response = session.request ( request ).get(); - - if ( response.status_code() == status_codes::OK ) - { return true; } - + http_response response = session . request ( request ) . get (); + + if ( response . status_code () == status_codes::OK ) + { + return true; + } + else - { return false; } + { + return false; + } } -void restListProjects ( char* serverAddr, long port, char* username, char* passwd , std::vector<string>& holder ) { - wstring serverAddrW = completeServerStr ( serverAddr, port ); +void restListProjects ( char* serverAddr, long port, char* username, char* passwd, std::vector <string>& holder ) +{ + wstring serverAddrW = completeServerStr ( serverAddr, port ); http_client_config config; - config.set_timeout ( utility::seconds ( 300 ) ); + config . set_timeout ( utility::seconds ( 300 ) ); http_client session ( serverAddrW, config ); http_request request = makeRequest ( username, passwd, L"/kylin/api/projects", methods::GET ); - http_response response = session.request ( request ).get(); - - if ( response.status_code() == status_codes::OK ) { - web::json::value projects = response.extract_json().get(); - - for ( auto iter = projects.as_array().begin(); iter != projects.as_array().end(); ++iter ) { - holder.push_back ( wstring2string ( ( *iter ) [U ( "name" )].as_string() ) ); + http_response response = session . request ( request ) . get (); + + if ( response . status_code () == status_codes::OK ) + { + web::json::value projects = response . extract_json () . get (); + + for ( auto iter = projects . as_array () . begin (); iter != projects . as_array () . end (); ++iter ) + { + holder . push_back ( wstring2string ( ( *iter )[U ( "name" )] . as_string () ) ); + } + + if ( holder . size () == 0 ) + { + throw exception ( "There is no project available in this server" ); } - - if ( holder.size() == 0 ) - { throw exception ( "There is no project available in this server" ); } } - - else if ( response.status_code() == status_codes::InternalError ) { - std::unique_ptr<ErrorMessage> em = ErrorMessageFromJSON ( response.extract_json().get() ); - string errorMsg = wstring2string ( em->msg ); - throw exception ( errorMsg.c_str() ); + + else if ( response . status_code () == status_codes::InternalError ) + { + std::unique_ptr <ErrorMessage> em = ErrorMessageFromJSON ( response . extract_json () . get () ); + string errorMsg = wstring2string ( em -> msg ); + throw exception ( errorMsg . c_str () ); } - - else { - throw exception ( "REST request(listproject) Invalid Response status code : " + response.status_code() ); + + else + { + throw exception ( "REST request(listproject) Invalid Response status code : " + response . status_code () ); } } -std::unique_ptr<MetadataResponse> restGetMeta ( char* serverAddr, long port , char* username, char* passwd, - char* project ) { - wstring serverAddrW = completeServerStr ( serverAddr, port ) ; +std::unique_ptr <MetadataResponse> restGetMeta ( char* serverAddr, long port, char* username, char* passwd, + char* project ) +{ + wstring serverAddrW = completeServerStr ( serverAddr, port ); http_client_config config; - config.set_timeout ( utility::seconds ( 300 ) ); + config . set_timeout ( utility::seconds ( 300 ) ); http_client session ( serverAddrW, config ); std::wstringstream wss; wss << L"/kylin/api/tables_and_columns" << L"?project=" << project; - http_request request = makeRequest ( username, passwd, wss.str().c_str(), methods::GET ); - http_response response = session.request ( request ).get(); - - if ( response.status_code() == status_codes::OK ) { - return MetadataResponseFromJSON ( response.extract_json().get() ); + http_request request = makeRequest ( username, passwd, wss . str () . c_str (), methods::GET ); + http_response response = session . request ( request ) . get (); + + if ( response . status_code () == status_codes::OK ) + { + return MetadataResponseFromJSON ( response . extract_json () . get () ); } - - else if ( response.status_code() == status_codes::Unauthorized ) { + + else if ( response . status_code () == status_codes::Unauthorized ) + { throw exception ( "Username/Password Unauthorized." ); } - - else if ( response.status_code() == status_codes::InternalError ) { - std::unique_ptr<ErrorMessage> em = ErrorMessageFromJSON ( response.extract_json().get() ); - string errorMsg = wstring2string ( em->msg ); - throw exception ( errorMsg.c_str() ); + + else if ( response . status_code () == status_codes::InternalError ) + { + std::unique_ptr <ErrorMessage> em = ErrorMessageFromJSON ( response . extract_json () . get () ); + string errorMsg = wstring2string ( em -> msg ); + throw exception ( errorMsg . c_str () ); } - - else { - throw exception ( "REST request(getmeta) Invalid Response status code : " + response.status_code() ); + + else + { + throw exception ( "REST request(getmeta) Invalid Response status code : " + response . status_code () ); } } -wstring cookQuery ( wchar_t* p ) { +wstring cookQuery ( wchar_t* p ) +{ wchar_t* q = new wchar_t[wcslen ( p ) + 1]; wcscpy ( q, p ); - - for ( int i = 0 ; i < ( int ) wcslen ( q ); i++ ) { - if ( q[i] == '\r' || q[i] == '\n' || q[i] == '\t' ) { + + for ( int i = 0; i < ( int ) wcslen ( q ); i++ ) + { + if ( q[i] == '\r' || q[i] == '\n' || q[i] == '\t' ) + { q[i] = ' '; } } - + wstring ret ( q ); delete[] q; size_t pos = 0; - - for ( size_t pos = 0;; pos += 2 ) { - pos = ret.find ( L"\"", pos ); - - if ( pos == wstring::npos ) { break; } - - ret.insert ( pos, L"\\" ); + + for ( size_t pos = 0;; pos += 2 ) + { + pos = ret . find ( L"\"", pos ); + + if ( pos == wstring::npos ) + { + break; + } + + ret . insert ( pos, L"\\" ); } - + return ret; } -wstring getBodyString ( http_response& response ) { - bool isGzipped = response.headers().has ( L"Content-Encoding" ); - - if ( isGzipped ) { +wstring getBodyString ( http_response& response ) +{ + bool isGzipped = response . headers () . has ( L"Content-Encoding" ); + + if ( isGzipped ) + { isGzipped = false; - http_headers::iterator iterator = response.headers().find ( L"Content-Encoding" ); - - if ( iterator != response.headers().end() ) { - wstring contentEncoding = iterator->second; - - if ( contentEncoding.find ( L"gzip" ) != std::string::npos ) - { isGzipped = true; } + http_headers::iterator iterator = response . headers () . find ( L"Content-Encoding" ); + + if ( iterator != response . headers () . end () ) + { + wstring contentEncoding = iterator -> second; + + if ( contentEncoding . find ( L"gzip" ) != std::string::npos ) + { + isGzipped = true; + } } } - - container_buffer<std::string> bodyBuffer; - response.body().read_to_end ( bodyBuffer ).get(); - const std::string& raw = bodyBuffer.collection(); + + container_buffer <std::string> bodyBuffer; + response . body () . read_to_end ( bodyBuffer ) . get (); + const std::string& raw = bodyBuffer . collection (); std::string uncompressed; - - if ( isGzipped ) { + + if ( isGzipped ) + { bool decompressStatus = gzipInflate ( raw, uncompressed ); - + if ( !decompressStatus ) - { throw exception ( "gzip decompress failed" ); } + { + throw exception ( "gzip decompress failed" ); + } } - - else { + + else + { uncompressed = raw; } - + //convert the string from utf8 to wchar - int size_needed = ::MultiByteToWideChar ( CP_UTF8, 0, ( char* ) uncompressed.c_str(), uncompressed.size(), NULL, 0 ); + int size_needed = ::MultiByteToWideChar ( CP_UTF8, 0, ( char* ) uncompressed . c_str (), uncompressed . size (), NULL, 0 ); std::wstring ret ( size_needed, 0 ); - ::MultiByteToWideChar ( CP_UTF8, 0, ( char* ) uncompressed.c_str(), uncompressed.size(), &ret[0], size_needed ); + ::MultiByteToWideChar ( CP_UTF8, 0, ( char* ) uncompressed . c_str (), uncompressed . size (), &ret[0], size_needed ); return ret; } -std::unique_ptr<SQLResponse> restQuery ( wchar_t* rawSql, char* serverAddr, long port, char* username, - char* passwd, - char* project ) { +std::unique_ptr <SQLResponse> restQuery ( wchar_t* rawSql, char* serverAddr, long port, char* username, + char* passwd, + char* project ) +{ //using local cache to intercept probing queries - std::unique_ptr<SQLResponse> cachedQueryRes = loadCache ( rawSql ); - + std::unique_ptr <SQLResponse> cachedQueryRes = loadCache ( rawSql ); + if ( cachedQueryRes != NULL ) - { return cachedQueryRes; } - + { + return cachedQueryRes; + } + //real requesting - wstring serverAddrW = completeServerStr ( serverAddr, port ) ; + wstring serverAddrW = completeServerStr ( serverAddr, port ); http_client_config config; - config.set_timeout ( utility::seconds ( 36000 ) ); + config . set_timeout ( utility::seconds ( 36000 ) ); http_client session ( serverAddrW, config ); http_request request = makeRequest ( username, passwd, L"/kylin/api/query", methods::POST ); wstring sql = cookQuery ( rawSql ); std::wstringstream wss; - wss << L"{ \"acceptPartial\": false, \"project\" : \"" << project << L"\", " << " \"sql\" : \"" << sql << L"\" }" ; - request.set_body ( wss.str(), L"application/json" ); - request.headers().add ( header_names::accept_encoding, "gzip,deflate" ); + wss << L"{ \"acceptPartial\": false, \"project\" : \"" << project << L"\", " << " \"sql\" : \"" << sql << L"\" }" ; + request . set_body ( wss . str (), L"application/json" ); + request . headers () . add ( header_names::accept_encoding, "gzip,deflate" ); http::status_code status; http_response response; - - try { - response = session.request ( request ).get(); - status = response.status_code(); + + try + { + response = session . request ( request ) . get (); + status = response . status_code (); } - - catch ( std::exception& e ) { + + catch ( std::exception& e ) + { std::stringstream ss; - ss << "An exception is throw Error message: " << e.what(); - throw exception ( ss.str().c_str() ); + ss << "An exception is throw Error message: " << e . what (); + throw exception ( ss . str () . c_str () ); } - + wstring ret = getBodyString ( response ); - - if ( status == status_codes::OK ) { + + if ( status == status_codes::OK ) + { //convert to json web::json::value actualRes = web::json::value::parse ( ret ); - std::unique_ptr<SQLResponse> r = SQLResponseFromJSON ( actualRes ); - - if ( r->isException == true ) { - string expMsg = wstring2string ( r->exceptionMessage ); - throw exception ( expMsg.c_str() ); + std::unique_ptr <SQLResponse> r = SQLResponseFromJSON ( actualRes ); + + if ( r -> isException == true ) + { + string expMsg = wstring2string ( r -> exceptionMessage ); + throw exception ( expMsg . c_str () ); } - - overwrite ( r.get() ); + + overwrite ( r . get () ); return r; } - - else if ( status == status_codes::InternalError ) { - std::unique_ptr<ErrorMessage> em = ErrorMessageFromJSON ( web::json::value::parse ( ret ) ); - string expMsg = wstring2string ( em->msg ); - throw exception ( expMsg.c_str() ); + + else if ( status == status_codes::InternalError ) + { + std::unique_ptr <ErrorMessage> em = ErrorMessageFromJSON ( web::json::value::parse ( ret ) ); + string expMsg = wstring2string ( em -> msg ); + throw exception ( expMsg . c_str () ); } - - else { + + else + { throw exception ( "Unknown exception in rest query with return code " + status ); } - + return NULL; } +
http://git-wip-us.apache.org/repos/asf/kylin/blob/3c2329dc/odbc/Common/REST.h ---------------------------------------------------------------------- diff --git a/odbc/Common/REST.h b/odbc/Common/REST.h index e20d745..bb405ef 100644 --- a/odbc/Common/REST.h +++ b/odbc/Common/REST.h @@ -15,8 +15,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - + + #pragma once #include "vld.h" @@ -25,11 +25,12 @@ //REST bool restAuthenticate ( char* serverAddr, long port, char* username, char* passwd ); -void restListProjects ( char* serverAddr, long port, char* username, char* passwd, std::vector<string>& holder ); +void restListProjects ( char* serverAddr, long port, char* username, char* passwd, std::vector <string>& holder ); + +std::unique_ptr <SQLResponse> restQuery ( wchar_t* rawSql, char* serverAddr, long port, char* username, + char* passwd, + char* project ); -std::unique_ptr<SQLResponse> restQuery ( wchar_t* rawSql, char* serverAddr, long port, char* username, - char* passwd, - char* project ); +std::unique_ptr <MetadataResponse> restGetMeta ( char* serverAddr, long port, char* username, char* passwd, + char* project ); -std::unique_ptr<MetadataResponse> restGetMeta ( char* serverAddr, long port, char* username, char* passwd, - char* project ); http://git-wip-us.apache.org/repos/asf/kylin/blob/3c2329dc/odbc/Common/StringUtils.cpp ---------------------------------------------------------------------- diff --git a/odbc/Common/StringUtils.cpp b/odbc/Common/StringUtils.cpp index dd33773..151b6d4 100644 --- a/odbc/Common/StringUtils.cpp +++ b/odbc/Common/StringUtils.cpp @@ -15,8 +15,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - + + #include <iostream> #include <stdlib.h> #include <string> @@ -31,131 +31,167 @@ using namespace std; -std::unique_ptr<char[]> str_base64_encode ( char* raw ) { +std::unique_ptr <char[]> str_base64_encode ( char* raw ) +{ trimwhitespace ( raw ); string encStr = base64_encode ( ( const unsigned char* ) raw, strlen ( raw ) ); - std::unique_ptr<char[]> temp ( new char[encStr.length() + 1] ); - strcpy ( temp.get(), encStr.c_str() ); + std::unique_ptr <char[]> temp ( new char[encStr . length () + 1] ); + strcpy ( temp . get (), encStr . c_str () ); return temp; } -std::unique_ptr<char[]> str_base64_decode ( char* enc ) { +std::unique_ptr <char[]> str_base64_decode ( char* enc ) +{ string s ( enc ); string decStr = base64_decode ( s ); - std::unique_ptr<char[]> temp ( new char[decStr.length() + 1] ); - strcpy ( temp.get(), decStr.c_str() ); + std::unique_ptr <char[]> temp ( new char[decStr . length () + 1] ); + strcpy ( temp . get (), decStr . c_str () ); return temp; } -void trimwhitespace ( char* str ) { +void trimwhitespace ( char* str ) +{ if ( str == NULL || strlen ( str ) == 0 ) - { return; } - + { + return; + } + char* start = str; char* end; - + // Trim leading space - while ( isspace ( *start ) ) { start++; } - - if ( *start == 0 ) { // All spaces? + while ( isspace ( *start ) ) + { + start++; + } + + if ( *start == 0 ) + { // All spaces? str[0] = '\0'; return; } - + // Trim trailing space end = start + strlen ( start ) - 1; - - while ( end > start && isspace ( *end ) ) { end--; } - + + while ( end > start && isspace ( *end ) ) + { + end--; + } + // Write new null terminator * ( end + 1 ) = 0; memmove ( str, start, end - start + 2 ); } -void copyTrimmed ( char** dest, char* src ) { +void copyTrimmed ( char** dest, char* src ) +{ // check if previous value exists - if ( *dest ) { + if ( *dest ) + { delete[] ( *dest ); *dest = NULL; } - + *dest = new char[strlen ( src ) + 1]; strcpy ( *dest, src ); trimwhitespace ( *dest ); } -std::unique_ptr<wchar_t[]> char2wchar ( char* orig ) { +std::unique_ptr <wchar_t[]> char2wchar ( char* orig ) +{ if ( orig == NULL ) - { return NULL; } - + { + return NULL; + } + size_t newsize = strlen ( orig ) + 1; - std::unique_ptr<wchar_t[]> wcstring ( new wchar_t[newsize] ); + std::unique_ptr <wchar_t[]> wcstring ( new wchar_t[newsize] ); size_t convertedChars = 0; - mbstowcs_s ( &convertedChars, wcstring.get(), newsize, orig, _TRUNCATE ); + mbstowcs_s ( &convertedChars, wcstring . get (), newsize, orig, _TRUNCATE ); return wcstring; } -std::unique_ptr<wchar_t[]> char2wchar ( const char* orig ) { +std::unique_ptr <wchar_t[]> char2wchar ( const char* orig ) +{ if ( orig == NULL ) - { return NULL; } - + { + return NULL; + } + size_t newsize = strlen ( orig ) + 1; - std::unique_ptr<wchar_t[]> wcstring ( new wchar_t[newsize] ); + std::unique_ptr <wchar_t[]> wcstring ( new wchar_t[newsize] ); size_t convertedChars = 0; - mbstowcs_s ( &convertedChars, wcstring.get(), newsize, orig, _TRUNCATE ); + mbstowcs_s ( &convertedChars, wcstring . get (), newsize, orig, _TRUNCATE ); return wcstring; } //specifying the destination -void char2wchar ( char* orig, wchar_t* dest, int destBufferLength ) { +void char2wchar ( char* orig, wchar_t* dest, int destBufferLength ) +{ if ( orig == NULL ) - { return; } - - if ( destBufferLength > 0 ) { - if ( destBufferLength <= ( int ) strlen ( orig ) ) { + { + return; + } + + if ( destBufferLength > 0 ) + { + if ( destBufferLength <= ( int ) strlen ( orig ) ) + { throw - 1; } } - + size_t newsize = strlen ( orig ) + 1; size_t convertedChars = 0; mbstowcs_s ( &convertedChars, dest, newsize, orig, _TRUNCATE ); } -std::unique_ptr<char[]> wchar2char ( wchar_t* orig ) { +std::unique_ptr <char[]> wchar2char ( wchar_t* orig ) +{ if ( orig == NULL ) - { return NULL; } - + { + return NULL; + } + size_t origsize = wcslen ( orig ) + 1; size_t convertedChars = 0; const size_t newsize = origsize; - std::unique_ptr<char[]> nstring ( new char[newsize] ); - wcstombs_s ( &convertedChars, nstring.get(), newsize, orig, _TRUNCATE ); + std::unique_ptr <char[]> nstring ( new char[newsize] ); + wcstombs_s ( &convertedChars, nstring . get (), newsize, orig, _TRUNCATE ); return nstring; } -std::unique_ptr<char[]> wchar2char ( const wchar_t* orig ) { +std::unique_ptr <char[]> wchar2char ( const wchar_t* orig ) +{ if ( orig == NULL ) - { return NULL; } - + { + return NULL; + } + size_t origsize = wcslen ( orig ) + 1; size_t convertedChars = 0; const size_t newsize = origsize; - std::unique_ptr<char[]> nstring ( new char[newsize] ); - wcstombs_s ( &convertedChars, nstring.get(), newsize, orig, _TRUNCATE ); + std::unique_ptr <char[]> nstring ( new char[newsize] ); + wcstombs_s ( &convertedChars, nstring . get (), newsize, orig, _TRUNCATE ); return nstring; } -void wchar2char ( wchar_t* orig, char* dest, int destBufferLength ) { +void wchar2char ( wchar_t* orig, char* dest, int destBufferLength ) +{ if ( orig == NULL ) - { return; } - - if ( destBufferLength > 0 ) { - if ( destBufferLength <= ( int ) wcslen ( orig ) ) { + { + return; + } + + if ( destBufferLength > 0 ) + { + if ( destBufferLength <= ( int ) wcslen ( orig ) ) + { throw - 1; } } - + size_t origsize = wcslen ( orig ) + 1; size_t convertedChars = 0; const size_t newsize = origsize; @@ -163,27 +199,32 @@ void wchar2char ( wchar_t* orig, char* dest, int destBufferLength ) { } -std::wstring string2wstring ( std::string& orig ) { +std::wstring string2wstring ( std::string& orig ) +{ std::wstring ws; - ws.assign ( orig.begin(), orig.end() ); + ws . assign ( orig . begin (), orig . end () ); return ws; } -std::string wstring2string ( std::wstring& orig ) { +std::string wstring2string ( std::wstring& orig ) +{ std::string s; - s.assign ( orig.begin(), orig.end() ); + s . assign ( orig . begin (), orig . end () ); return s; } -std::unique_ptr<char[]> make_unique_str ( int size ) { - return std::unique_ptr<char[]> ( new char[size + 1] ); +std::unique_ptr <char[]> make_unique_str ( int size ) +{ + return std::unique_ptr <char[]> ( new char[size + 1] ); } -void remove_char(char *src, const char tgt) +void remove_char ( char* src, const char tgt ) { - char * fp = src; - while (*src) { - if (*src != tgt) { + char* fp = src; + while ( *src ) + { + if ( *src != tgt ) + { *fp = *src; fp++; } @@ -192,5 +233,3 @@ void remove_char(char *src, const char tgt) *fp = '\0' ; } - - http://git-wip-us.apache.org/repos/asf/kylin/blob/3c2329dc/odbc/Common/StringUtils.h ---------------------------------------------------------------------- diff --git a/odbc/Common/StringUtils.h b/odbc/Common/StringUtils.h index 5aec343..d7f1634 100644 --- a/odbc/Common/StringUtils.h +++ b/odbc/Common/StringUtils.h @@ -15,8 +15,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - + + #pragma once #include <memory> @@ -24,12 +24,12 @@ //UTIL -std::unique_ptr<wchar_t[]> char2wchar ( char* orig ); -std::unique_ptr<wchar_t[]> char2wchar ( const char* orig ); +std::unique_ptr <wchar_t[]> char2wchar ( char* orig ); +std::unique_ptr <wchar_t[]> char2wchar ( const char* orig ); void char2wchar ( char* orig, wchar_t* dest, int destLength ); -std::unique_ptr<char[]> wchar2char ( wchar_t* orig ); -std::unique_ptr<char[]> wchar2char ( const wchar_t* orig ); +std::unique_ptr <char[]> wchar2char ( wchar_t* orig ); +std::unique_ptr <char[]> wchar2char ( const wchar_t* orig ); void wchar2char ( wchar_t* orig, char* dest, int destLength ); std::wstring string2wstring ( std::string& orig ); @@ -38,9 +38,10 @@ std::string wstring2string ( std::wstring& orig ); void trimwhitespace ( char* str ); void copyTrimmed ( char** dest, char* src ); -std::unique_ptr<char[]> str_base64_encode ( char* raw ); -std::unique_ptr<char[]> str_base64_decode ( char* enc ); +std::unique_ptr <char[]> str_base64_encode ( char* raw ); +std::unique_ptr <char[]> str_base64_decode ( char* enc ); + +std::unique_ptr <char[]> make_unique_str ( int size ); -std::unique_ptr<char[]> make_unique_str ( int size ); +void remove_char ( char* src, const char tgt ); -void remove_char(char *src, const char tgt); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/kylin/blob/3c2329dc/odbc/Common/base64.cpp ---------------------------------------------------------------------- diff --git a/odbc/Common/base64.cpp b/odbc/Common/base64.cpp index 7b09664..9e26b46 100644 --- a/odbc/Common/base64.cpp +++ b/odbc/Common/base64.cpp @@ -15,101 +15,131 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #include "Base64.h" #include <iostream> static const std::string base64_chars = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; +"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +"abcdefghijklmnopqrstuvwxyz" +"0123456789+/"; -static inline bool is_base64 ( unsigned char c ) { +static inline bool is_base64 ( unsigned char c ) +{ return ( isalnum ( c ) || ( c == '+' ) || ( c == '/' ) ); } -std::string base64_encode ( unsigned char const* bytes_to_encode, unsigned int in_len ) { +std::string base64_encode ( unsigned char const* bytes_to_encode, unsigned int in_len ) +{ std::string ret; int i = 0; int j = 0; unsigned char char_array_3[3]; unsigned char char_array_4[4]; - - while ( in_len-- ) { + + while ( in_len-- ) + { char_array_3[i++] = * ( bytes_to_encode++ ); - - if ( i == 3 ) { + + if ( i == 3 ) + { char_array_4[0] = ( char_array_3[0] & 0xfc ) >> 2; char_array_4[1] = ( ( char_array_3[0] & 0x03 ) << 4 ) + ( ( char_array_3[1] & 0xf0 ) >> 4 ); char_array_4[2] = ( ( char_array_3[1] & 0x0f ) << 2 ) + ( ( char_array_3[2] & 0xc0 ) >> 6 ); char_array_4[3] = char_array_3[2] & 0x3f; - - for ( i = 0; ( i < 4 ) ; i++ ) - { ret += base64_chars[char_array_4[i]]; } - + + for ( i = 0; ( i < 4 ); i++ ) + { + ret += base64_chars[char_array_4[i]]; + } + i = 0; } } - - if ( i ) { + + if ( i ) + { for ( j = i; j < 3; j++ ) - { char_array_3[j] = '\0'; } - + { + char_array_3[j] = '\0'; + } + char_array_4[0] = ( char_array_3[0] & 0xfc ) >> 2; char_array_4[1] = ( ( char_array_3[0] & 0x03 ) << 4 ) + ( ( char_array_3[1] & 0xf0 ) >> 4 ); char_array_4[2] = ( ( char_array_3[1] & 0x0f ) << 2 ) + ( ( char_array_3[2] & 0xc0 ) >> 6 ); char_array_4[3] = char_array_3[2] & 0x3f; - + for ( j = 0; ( j < i + 1 ); j++ ) - { ret += base64_chars[char_array_4[j]]; } - + { + ret += base64_chars[char_array_4[j]]; + } + while ( ( i++ < 3 ) ) - { ret += '='; } + { + ret += '='; + } } - + return ret; } -std::string base64_decode ( std::string const& encoded_string ) { - int in_len = encoded_string.size(); +std::string base64_decode ( std::string const& encoded_string ) +{ + int in_len = encoded_string . size (); int i = 0; int j = 0; int in_ = 0; unsigned char char_array_4[4], char_array_3[3]; std::string ret; - - while ( in_len-- && ( encoded_string[in_] != '=' ) && is_base64 ( encoded_string[in_] ) ) { - char_array_4[i++] = encoded_string[in_]; in_++; - - if ( i == 4 ) { + + while ( in_len-- && ( encoded_string[in_] != '=' ) && is_base64 ( encoded_string[in_] ) ) + { + char_array_4[i++] = encoded_string[in_]; + in_++; + + if ( i == 4 ) + { for ( i = 0; i < 4; i++ ) - { char_array_4[i] = base64_chars.find ( char_array_4[i] ); } - + { + char_array_4[i] = base64_chars . find ( char_array_4[i] ); + } + char_array_3[0] = ( char_array_4[0] << 2 ) + ( ( char_array_4[1] & 0x30 ) >> 4 ); char_array_3[1] = ( ( char_array_4[1] & 0xf ) << 4 ) + ( ( char_array_4[2] & 0x3c ) >> 2 ); char_array_3[2] = ( ( char_array_4[2] & 0x3 ) << 6 ) + char_array_4[3]; - + for ( i = 0; ( i < 3 ); i++ ) - { ret += char_array_3[i]; } - + { + ret += char_array_3[i]; + } + i = 0; } } - - if ( i ) { + + if ( i ) + { for ( j = i; j < 4; j++ ) - { char_array_4[j] = 0; } - + { + char_array_4[j] = 0; + } + for ( j = 0; j < 4; j++ ) - { char_array_4[j] = base64_chars.find ( char_array_4[j] ); } - + { + char_array_4[j] = base64_chars . find ( char_array_4[j] ); + } + char_array_3[0] = ( char_array_4[0] << 2 ) + ( ( char_array_4[1] & 0x30 ) >> 4 ); char_array_3[1] = ( ( char_array_4[1] & 0xf ) << 4 ) + ( ( char_array_4[2] & 0x3c ) >> 2 ); char_array_3[2] = ( ( char_array_4[2] & 0x3 ) << 6 ) + char_array_4[3]; - - for ( j = 0; ( j < i - 1 ); j++ ) { ret += char_array_3[j]; } + + for ( j = 0; ( j < i - 1 ); j++ ) + { + ret += char_array_3[j]; + } } - + return ret; -} \ No newline at end of file +} + http://git-wip-us.apache.org/repos/asf/kylin/blob/3c2329dc/odbc/Common/base64.h ---------------------------------------------------------------------- diff --git a/odbc/Common/base64.h b/odbc/Common/base64.h index f0af44d..5710e3c 100644 --- a/odbc/Common/base64.h +++ b/odbc/Common/base64.h @@ -15,7 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #pragma once #include <string> @@ -23,6 +23,3 @@ std::string base64_encode ( unsigned char const*, unsigned int len ); std::string base64_decode ( std::string const& s ); - - -