From e8469026c856a146c62ce7a98fa4e41f66ddfaf3 Mon Sep 17 00:00:00 2001 From: matlabbe Date: Fri, 11 Apr 2014 20:34:07 +0000 Subject: [PATCH] fixed compilation error in Mac Os X git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@211 620bd6b2-0a58-f614-fd9a-1bd335dccda9 --- app/CMakeLists.txt | 1 + src/utilite/UConversion.cpp | 316 ++++++++++++++++++++++++++++++++++++ src/utilite/UConversion.h | 253 +++++++++++++++++++++++++++++ src/utilite/UDirectory.cpp | 30 +--- 4 files changed, 574 insertions(+), 26 deletions(-) create mode 100644 src/utilite/UConversion.cpp create mode 100644 src/utilite/UConversion.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 4ee4cfe4..00c36575 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -47,6 +47,7 @@ SET(SRC_FILES ../src/utilite/UPlot.cpp ../src/utilite/UDirectory.cpp ../src/utilite/UFile.cpp + ../src/utilite/UConversion.cpp ../src/rtabmap/PdfPlot.cpp ${moc_srcs} ${moc_uis} diff --git a/src/utilite/UConversion.cpp b/src/utilite/UConversion.cpp new file mode 100644 index 00000000..eded914f --- /dev/null +++ b/src/utilite/UConversion.cpp @@ -0,0 +1,316 @@ +// Taken from UtiLite library r266 [www.utilite.googlecode.com] +/* +* utilite is a cross-platform library with +* useful utilities for fast and small developing. +* Copyright (C) 2010 Mathieu Labbe +* +* utilite is free library: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* utilite is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this program. If not, see . +*/ + +#include "utilite/UConversion.h" + +#include +#include + +std::string uReplaceChar(const std::string & str, char before, char after) +{ + std::string result = str; + for(unsigned int i=0; i= 'a' && result[i]<='z') + { + result[i] = result[i] - 'a' + 'A'; + } + } + return result; +} + +std::string uToLowerCase(const std::string & str) +{ + std::string result = str; + for(unsigned int i=0; i= 'A' && result[i]<='Z') + { + result[i] = result[i] - 'A' + 'a'; + } + } + return result; +} + +std::string uNumber2Str(unsigned int number) +{ + std::stringstream s; + s << number; + return s.str(); +} + +std::string uNumber2Str(int number) +{ + std::stringstream s; + s << number; + return s.str(); +} + +std::string uNumber2Str(float number) +{ + std::stringstream s; + s << number; + return s.str(); +} + +std::string uNumber2Str(double number) +{ + std::stringstream s; + s << number; + return s.str(); +} + +std::string uBool2Str(bool boolean) +{ + std::string s; + if(boolean) + { + s = "true"; + } + else + { + s = "false"; + } + return s; +} + +bool uStr2Bool(const char * str) +{ + return !(str && (strcmp(str, "false") == 0 || strcmp(str, "FALSE") == 0 || strcmp(str, "0") == 0)); +} + +std::string uBytes2Hex(const char * bytes, unsigned int bytesLen) +{ + std::string hex; + if(!bytes || bytesLen == 0) + { + return hex; + } + const unsigned char * bytes_u = (const unsigned char*)(bytes); + + hex.resize(bytesLen*2); + char * pHex = &hex[0]; + const unsigned char * pEnd = (bytes_u + bytesLen); + for(const unsigned char * pChar = bytes_u; pChar != pEnd; ++pChar, pHex += 2) + { + pHex[0] = uHex2Ascii(*pChar, 0); + pHex[1] = uHex2Ascii(*pChar, 1); + } + return hex; +} + +std::vector uHex2Bytes(const std::string & hex) +{ + return uHex2Bytes(&hex[0], hex.length()); +} + +std::vector uHex2Bytes(const char * hex, int hexLen) +{ + std::vector bytes; + if(!hex || hexLen % 2 || hexLen == 0) + { + return bytes; // must be pair + } + + unsigned int bytesLen = hexLen / 2; + bytes.resize(bytesLen); + unsigned char * pBytes = (unsigned char *)&bytes[0]; + const unsigned char * pHex = (const unsigned char *)hex; + + unsigned char * pEnd = (pBytes + bytesLen); + for(unsigned char * pChar = pBytes; pChar != pEnd; pChar++, pHex += 2) + { + *pChar = (uAscii2Hex(pHex[0]) << 4) | uAscii2Hex(pHex[1]); + } + return bytes; +} + +// The hex str MUST not contains any null values (0x00) +std::string uHex2Str(const std::string & hex) +{ + std::vector bytes = uHex2Bytes(hex); + return std::string(&bytes[0], bytes.size()); +} + +static const char HEX2ASCII[256][2] = +{ + {'0','0'},{'0','1'},{'0','2'},{'0','3'},{'0','4'},{'0','5'},{'0','6'},{'0','7'},{'0','8'},{'0','9'},{'0','A'},{'0','B'},{'0','C'},{'0','D'},{'0','E'},{'0','F'}, + {'1','0'},{'1','1'},{'1','2'},{'1','3'},{'1','4'},{'1','5'},{'1','6'},{'1','7'},{'1','8'},{'1','9'},{'1','A'},{'1','B'},{'1','C'},{'1','D'},{'1','E'},{'1','F'}, + {'2','0'},{'2','1'},{'2','2'},{'2','3'},{'2','4'},{'2','5'},{'2','6'},{'2','7'},{'2','8'},{'2','9'},{'2','A'},{'2','B'},{'2','C'},{'2','D'},{'2','E'},{'2','F'}, + {'3','0'},{'3','1'},{'3','2'},{'3','3'},{'3','4'},{'3','5'},{'3','6'},{'3','7'},{'3','8'},{'3','9'},{'3','A'},{'3','B'},{'3','C'},{'3','D'},{'3','E'},{'3','F'}, + {'4','0'},{'4','1'},{'4','2'},{'4','3'},{'4','4'},{'4','5'},{'4','6'},{'4','7'},{'4','8'},{'4','9'},{'4','A'},{'4','B'},{'4','C'},{'4','D'},{'4','E'},{'4','F'}, + {'5','0'},{'5','1'},{'5','2'},{'5','3'},{'5','4'},{'5','5'},{'5','6'},{'5','7'},{'5','8'},{'5','9'},{'5','A'},{'5','B'},{'5','C'},{'5','D'},{'5','E'},{'5','F'}, + {'6','0'},{'6','1'},{'6','2'},{'6','3'},{'6','4'},{'6','5'},{'6','6'},{'6','7'},{'6','8'},{'6','9'},{'6','A'},{'6','B'},{'6','C'},{'6','D'},{'6','E'},{'6','F'}, + {'7','0'},{'7','1'},{'7','2'},{'7','3'},{'7','4'},{'7','5'},{'7','6'},{'7','7'},{'7','8'},{'7','9'},{'7','A'},{'7','B'},{'7','C'},{'7','D'},{'7','E'},{'7','F'}, + {'8','0'},{'8','1'},{'8','2'},{'8','3'},{'8','4'},{'8','5'},{'8','6'},{'8','7'},{'8','8'},{'8','9'},{'8','A'},{'8','B'},{'8','C'},{'8','D'},{'8','E'},{'8','F'}, + {'9','0'},{'9','1'},{'9','2'},{'9','3'},{'9','4'},{'9','5'},{'9','6'},{'9','7'},{'9','8'},{'9','9'},{'9','A'},{'9','B'},{'9','C'},{'9','D'},{'9','E'},{'9','F'}, + {'A','0'},{'A','1'},{'A','2'},{'A','3'},{'A','4'},{'A','5'},{'A','6'},{'A','7'},{'A','8'},{'A','9'},{'A','A'},{'A','B'},{'A','C'},{'A','D'},{'A','E'},{'A','F'}, + {'B','0'},{'B','1'},{'B','2'},{'B','3'},{'B','4'},{'B','5'},{'B','6'},{'B','7'},{'B','8'},{'B','9'},{'B','A'},{'B','B'},{'B','C'},{'B','D'},{'B','E'},{'B','F'}, + {'C','0'},{'C','1'},{'C','2'},{'C','3'},{'C','4'},{'C','5'},{'C','6'},{'C','7'},{'C','8'},{'C','9'},{'C','A'},{'C','B'},{'C','C'},{'C','D'},{'C','E'},{'C','F'}, + {'D','0'},{'D','1'},{'D','2'},{'D','3'},{'D','4'},{'D','5'},{'D','6'},{'D','7'},{'D','8'},{'D','9'},{'D','A'},{'D','B'},{'D','C'},{'D','D'},{'D','E'},{'D','F'}, + {'E','0'},{'E','1'},{'E','2'},{'E','3'},{'E','4'},{'E','5'},{'E','6'},{'E','7'},{'E','8'},{'E','9'},{'E','A'},{'E','B'},{'E','C'},{'E','D'},{'E','E'},{'E','F'}, + {'F','0'},{'F','1'},{'F','2'},{'F','3'},{'F','4'},{'F','5'},{'F','6'},{'F','7'},{'F','8'},{'F','9'},{'F','A'},{'F','B'},{'F','C'},{'F','D'},{'F','E'},{'F','F'} +}; + +unsigned char uHex2Ascii(const unsigned char & c, bool rightPart) +{ + if(rightPart) + { + return HEX2ASCII[c][1]; + } + else + { + return HEX2ASCII[c][0]; + } +} + +unsigned char uAscii2Hex(const unsigned char & c) +{ + switch(c) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return c-'0'; + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + return c-'A'+10; + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + return c-'a'+10; + default: + return 0x00; + } +} + +std::string uFormatv (const char *fmt, va_list args) +{ + // Allocate a buffer on the stack that's big enough for us almost + // all the time. Be prepared to allocate dynamically if it doesn't fit. + size_t size = 1024; + std::vector dynamicbuf(size); + char *buf = &dynamicbuf[0]; + + va_list argsTmp; + + while (1) { +#if defined(WIN32) && !defined(__MINGW32__) + argsTmp = args; +#else + va_copy(argsTmp, args); +#endif + + // Try to vsnprintf into our buffer. + int needed = vsnprintf (buf, size, fmt, argsTmp); + va_end(argsTmp); + // NB. C99 (which modern Linux and OS X follow) says vsnprintf + // failure returns the length it would have needed. But older + // glibc and current Windows return -1 for failure, i.e., not + // telling us how much was needed. + if (needed < (int)size-1 && needed >= 0) { + // It fit fine so we're done. + return std::string (buf, (size_t) needed); + } + + // vsnprintf reported that it wanted to write more characters + // than we allotted. So try again using a dynamic buffer. This + // doesn't happen very often if we chose our initial size well. + size = needed>=0?needed+2:size*2; + dynamicbuf.resize (size); + buf = &dynamicbuf[0]; + } + return std::string(); // would not reach this, but for compiler complaints... +} + +std::string uFormat (const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + std::string buf = uFormatv(fmt, args); + va_end(args); + return buf; +} + +#ifdef WIN32 +// returned whar_t * must be deleted : delete [] wText; +wchar_t * createWCharFromChar(const char * text) +{ + DWORD length = MultiByteToWideChar (CP_ACP, 0, text, -1, NULL, 0); + wchar_t * wText = new wchar_t[length]; + MultiByteToWideChar (CP_ACP, 0, text, -1, wText, length ); + return wText; +} + +// returned char * must be deleted : delete [] text; +char * createCharFromWChar(const wchar_t * wText) +{ + DWORD length = WideCharToMultiByte (CP_ACP, 0, wText, -1, NULL, 0, NULL, NULL); + char * text = new char[length]; + WideCharToMultiByte (CP_ACP, 0, wText, -1, text, length, NULL, NULL); + return text; +} +#endif diff --git a/src/utilite/UConversion.h b/src/utilite/UConversion.h new file mode 100644 index 00000000..d717e891 --- /dev/null +++ b/src/utilite/UConversion.h @@ -0,0 +1,253 @@ +/* +* utilite is a cross-platform library with +* useful utilities for fast and small developing. +* Copyright (C) 2010 Mathieu Labbe +* +* utilite is free library: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* utilite is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this program. If not, see . +*/ + +#ifndef UCONVERSION_H +#define UCONVERSION_H + +//#include "utilite/UtiLiteExp.h" // DLL export/import defines + +#include +#include +#include + +/** + * \file UConversion.h + * \brief Some conversion functions + * + * This contains functions to do some convenient conversion like + * uNumber2str(), uBytes2Hex() or uHex2Bytes(). +*/ + +/** + * Replace old characters in a string to new ones. + * Example : + * @code + * std::string str = "Hello"; + * uReplaceChar(str, 'l', 'p'); + * // The results is str = "Heppo"; + * @endcode + * + * @param str the string + * @param before the character to be replaced by the new one + * @param after the new character replacing the old one + * @return the modified string + */ +std::string uReplaceChar(const std::string & str, char before, char after); + +/** + * Replace old characters in a string with the specified string. + * Example : + * @code + * std::string str = "Hello"; + * uReplaceChar(str, 'o', "oween"); + * // The results is str = "Helloween"; + * @endcode + * + * @param str the string + * @param before the character to be replaced by the new one + * @param after the new string replacing the old character + * @return the modified string + */ +std::string uReplaceChar(const std::string & str, char before, const std::string & after); + +/** + * Transform characters from a string to upper case. + * Example : + * @code + * std::string str = "hello!"; + * str = uToUpperCase(str); + * //str is now equal to "HELLO!" + * @endcode + * @param str the string + * @return the modified string + */ +std::string uToUpperCase(const std::string & str); + +/** + * Transform characters from a string to lower case. + * Example : + * @code + * std::string str = "HELLO!"; + * str = uToLowerCase(str, false); + * //str is now equal to "hello!" + * @endcode + * @param str the string + * @return the modified string + */ +std::string uToLowerCase(const std::string & str); + +/** + * Convert a number (unsigned int) to a string. + * @param number the number to convert in a string + * @return the string + */ +std::string uNumber2Str(unsigned int number); +/** + * Convert a number (int) to a string. + * @param number the number to convert in a string + * @return the string + */ +std::string uNumber2Str(int number); +/** + * Convert a number (float) to a string. + * @param number the number to convert in a string + * @return the string + */ +std::string uNumber2Str(float number); +/** + * Convert a number (double) to a string. + * @param number the number to convert in a string + * @return the string + */ +std::string uNumber2Str(double number); + +/** + * Convert a bool to a string. + * The format used is "true" and "false". + * @param boolean the boolean to convert in a string + * @return the string + */ +std::string uBool2Str(bool boolean); +/** + * Convert a string to a boolean. + * The format used is : + * "false", "FALSE" or "0" give false. All others give true. + * @param str the string to convert in a boolean + * @return the boolean + */ +bool uStr2Bool(const char * str); + +/** + * Convert a bytes array to an hexadecimal string. + * The resulting string is twice the size of the bytes array. The hexadecimal + * Characters are in upper case. + * Example : + * @code + * char bytes[] = {0x3F}; + * std::string hex = uBytes2Hex(bytes, 1); + * // The string constains "3F". + * @endcode + * + * @param bytes the bytes array + * @param bytesLen the length of the bytes array + * @return the hexadecimal string + */ +std::string uBytes2Hex(const char * bytes, unsigned int bytesLen); +/** + * Convert an hexadecimal string to a bytes array. + * The string must be pair length. The hexadecimal + * Characters can be in upper or lower case. + * Example : + * @code + * std::string hex = "1f3B"; + * std::vector bytes = uHex2Bytes(hex); + * // The array contains {0x1F, 0x3B}. + * @endcode + * + * @param hex the hexadecimal string + * @return the bytes array + */ +std::vector uHex2Bytes(const std::string & hex); +/** + * Convert an hexadecimal string to a bytes array. + * The string must be pair length. The hexadecimal + * Characters can be in upper or lower case. + * Example : + * @code + * std::vector bytes = uHex2Bytes("1f3B", 4); + * // The array contains {0x1F, 0x3B}. + * @endcode + * + * @param hex the hexadecimal string + * @param bytesLen the hexadecimal string length + * @return the bytes array + */ +std::vector uHex2Bytes(const char * hex, int hexLen); + +/** + * Convert an hexadecimal string to an ascii string. A convenient way + * when using only strings. + * The hexadecimal str MUST not contains any null values 0x00 ("00"). + * Think to use of hex2bytes() to handle 0x00 values. + * Characters can be in upper or lower case. + * Example : + * @code + * std::string str = uHex2Str("48656C6C4F21"); + * // The string contains "Hello!". + * @endcode + * + * @see hex2bytes + * @param hex the hexadecimal string + * @return the ascii string + */ +std::string uHex2Str(const std::string & hex); + +/** + * Convert hexadecimal (left or right part) value to an ascii character. + * Example : + * @code + * unsigned char F = uHex2Ascii(0xFA, false); + * unsigned char A = uHex2Ascii(0xFA, true); + * @endcode + * @see ascii2hex + * @param c the hexadecimal value + * @param rightPart If we want the character corresponding to the right of left part (4 bits) of the byte value. + * @return the ascii character (in upper case) + */ +unsigned char uHex2Ascii(const unsigned char & c, bool rightPart); + +/** + * Convert an ascii character to an hexadecimal value (right 4 bits). + * Characters can be in upper or lower case. + * Example : + * @code + * unsigned char hex = uAscii2Hex('F'); + * // The results is hex = 0x0F; + * @endcode + * @see hex2ascii + * @param c the ascii character + * @return the hexadecimal value + */ +unsigned char uAscii2Hex(const unsigned char & c); + +/** + * Format a string like printf, and return it as a std::string + */ +std::string uFormatv (const char *fmt, va_list ap); + +/** + * Format a string like printf, and return it as a std::string + */ +std::string uFormat (const char *fmt, ...); + +#ifdef WIN32 +/** + * Convert multi-byte string to unicode (wide-char) string. + * Note that returned whar_t * must be deleted : delete [] wText; + */ +wchar_t * createWCharFromChar(const char * text); + +/** + * Convert unicode (wide-char) string to multi-byte string. + * Note that returned char * must be deleted : delete [] text; + */ +char * createCharFromWChar(const wchar_t * wText); +#endif + +#endif /* UCONVERSION_H */ diff --git a/src/utilite/UDirectory.cpp b/src/utilite/UDirectory.cpp index 724a8b45..99f6b684 100644 --- a/src/utilite/UDirectory.cpp +++ b/src/utilite/UDirectory.cpp @@ -1,4 +1,4 @@ -// Taken from UtiLite library r185 [www.utilite.googlecode.com] +// Taken from UtiLite library r266 [www.utilite.googlecode.com] /* * utilite is a cross-platform library with @@ -40,6 +40,7 @@ #include "utilite/UStl.h" #include "utilite/UFile.h" #include "utilite/UDirectory.h" +#include "utilite/UConversion.h" #ifdef WIN32 @@ -48,11 +49,8 @@ bool sortCallback(const std::string & a, const std::string & b) return uStrNumCmp(a,b) < 0; } #elif __APPLE__ -int sortCallback(const void * aa, const void * bb) +int sortCallback(const struct dirent ** a, const struct dirent ** b) { - const struct dirent ** a = (const struct dirent **)aa; - const struct dirent ** b = (const struct dirent **)bb; - return uStrNumCmp((*a)->d_name, (*b)->d_name); } #else @@ -103,26 +101,6 @@ void UDirectory::setPath(const std::string & path, const std::string & extension this->update(); } -#ifdef WIN32 -// returned whar_t * must be deleted : delete [] wText; -wchar_t * createWCharFromChar(const char * text) -{ - DWORD length = MultiByteToWideChar (CP_ACP, 0, text, -1, NULL, 0); - wchar_t * wText = new wchar_t[length]; - MultiByteToWideChar (CP_ACP, 0, text, -1, wText, length ); - return wText; -} - -// returned char * must be deleted : delete [] text; -char * createCharFromWChar(const wchar_t * wText) -{ - DWORD length = WideCharToMultiByte (CP_ACP, 0, wText, -1, NULL, 0, NULL, NULL); - char * text = new char[length]; - WideCharToMultiByte (CP_ACP, 0, wText, -1, text, length, NULL, NULL); - return text; -} -#endif - void UDirectory::update() { if(exists(path_)) @@ -268,7 +246,7 @@ bool UDirectory::exists(const std::string & dirPath) #else DWORD dwAttrib = GetFileAttributes(dirPath.c_str()); #endif - r = (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); + r = (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); #else DIR *dp; if((dp = opendir(dirPath.c_str())) != NULL)