00001 #ifndef IONFLUX_TOOLS
00002 #define IONFLUX_TOOLS
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <string>
00030 #include <iostream>
00031 #include <fstream>
00032 #include <sstream>
00033 #include <iomanip>
00034 #include <vector>
00035 #include <termios.h>
00036 #include <stdio.h>
00037 #include <bzlib.h>
00038 #include <sys/types.h>
00039 #include <dirent.h>
00040 #include <unistd.h>
00041 #include "sha1.h"
00042 #ifndef HAVE_WINSOCK
00043 #include <netinet/in.h>
00044 #else
00045 #include <winsock.h>
00046 #endif
00047 #ifndef NO_FORK
00048 #include <sys/stat.h>
00049 #include <paths.h>
00050 #include <fcntl.h>
00051 #endif
00052 #include "MersenneTwister.h"
00053 #include "ionflux/Tokenizer.hpp"
00054
00059 namespace Ionflux
00060 {
00061
00066 namespace Tools
00067 {
00068
00069 class CharTypeMap;
00070
00078
00079 const std::string MODULE_NAME = "Ionflux Tools";
00081 const std::string MODULE_VERSION = "0.2.3";
00083 const int READ_BUFFER_SIZE = 1024;
00085 const int COMPRESS_BUFFER_SIZE = 1024;
00087 const int DIR_BUFFER_SIZE = 1024;
00089 const int DIR_BUFFER_SIZE_MAX = 65536;
00091 const char DIR_SEPARATOR = '/';
00092
00102 std::string makeHex(const std::string& inputData);
00103
00115 std::string makeReadable(const std::string& inputData,
00116 const std::string& replacement);
00117
00131 std::string makeNiceHex(const std::string& hex, const std::string& readable,
00132 int bytesPerLine, int groupBytes);
00133
00144 std::string sha1(const std::string& secret, bool hexOut = false);
00145
00155 void explode(const std::string& bytes, const std::string& splitString,
00156 std::vector<std::string>& result);
00157
00167 void explodeQuoted(const std::string& bytes, std::vector<std::string>& result);
00168
00180 bool isOneOf(char c, const std::string& testChars);
00181
00192 bool isNumber(const std::string &bytes);
00193
00204 bool isAlpha(const std::string &bytes);
00205
00216 bool isInteger(const std::string &bytes);
00217
00228 bool isFloat(const std::string &bytes);
00229
00242 bool isIdentifier(const std::string &bytes);
00243
00252 std::string readFile(const std::string& fileName);
00253
00266 bool writeFile(const std::string& fileName, const std::string& data,
00267 char writeMode);
00268
00278 bool bz2Compress(const std::string& inputData, std::string &outputBuffer);
00279
00289 bool bz2Decompress(const std::string& inputData, std::string &outputBuffer);
00290
00299 std::string packInt(int data);
00300
00310 std::string packDouble(double data);
00311
00324 std::string packBool(bool data);
00325
00334 int unpackInt(const std::string &data);
00335
00344 double unpackDouble(const std::string &data);
00345
00354 bool unpackBool(const std::string &data);
00355
00361 void socketInit();
00362
00368 void socketShutdown();
00369
00374 void daemonize();
00375
00384 std::string parseHex(const std::string &inputData);
00385
00393 void getPassword(const std::string &prompt, std::string &password);
00394
00407 void getRandomBytes(std::string &bytes, unsigned int n, MTRand *source);
00408
00421 void getRandomIdentifier(std::string &bytes, unsigned int n, MTRand *source);
00422
00435 bool readDir(const std::string &dirName, std::vector<std::string> &target,
00436 bool prependDirName = false, bool recursive = false);
00437
00442 std::string getCurrentDir();
00443
00457 bool getFileList(const std::vector<std::string>& pathList,
00458 std::vector<std::string>& target, bool recursive = false,
00459 const std::string& filterExpr = "");
00460
00478 int getOpPrecedence(const std::string &op, bool unary = false);
00479
00493 std::string escape(const std::string &source,
00494 const std::string& escapeWhat = " '\"");
00495
00506 std::string toUpper(const std::string &text, unsigned int numChars = 0,
00507 unsigned int offset = 0);
00508
00519 std::string toLower(const std::string &text, unsigned int numChars = 0,
00520 unsigned int offset = 0);
00521
00534 bool toBool(const std::string &text);
00535
00545 unsigned int utf8GetSize(unsigned char byte);
00546
00555 unsigned int utf8GetSize(const std::string& bytes);
00556
00566 bool utf8ToUInt(const std::string& bytes, unsigned int& target);
00567
00579 bool utf8ToUInt(const std::string& bytes, std::vector<unsigned int>& target);
00580
00590 std::string uintToUtf8(unsigned int uniChar);
00591
00599 void uintToUtf8(const std::vector<unsigned int>& uniChars,
00600 std::string& target);
00601
00615 std::string utf8Trim(const std::string& bytes, bool leftTrim = true,
00616 bool rightTrim = true);
00617
00629 bool utf8IsOneOf(unsigned int c, const std::string& testChars);
00630
00642 bool utf8IsOneOf(unsigned int c, const std::vector<unsigned int>& testChars);
00643
00652 std::string utf8Escape(const std::string& source);
00653
00662 std::string utf8Unescape(const std::string& source);
00663
00673 std::string quote(const std::string& source,
00674 const unsigned char quoteChar = '\'');
00675
00685 std::string unquote(const std::string& source,
00686 const std::string& addQuoteChars = "");
00687
00701 std::string trim(const std::string& bytes, bool leftTrim = true,
00702 bool rightTrim = true);
00703
00710 const CharTypeMap& getCharTypes();
00711
00720 std::string urlEncode(const std::string& bytes);
00721
00730 std::string urlDecode(const std::string& bytes);
00731
00741 std::string appendDirSeparator(const std::string& path,
00742 unsigned char separator = DIR_SEPARATOR);
00743
00753 std::string prependDirSeparator(const std::string& path,
00754 unsigned char separator = DIR_SEPARATOR);
00755
00765 bool validatePath(const std::string& path, int mask = 0755);
00766
00776 std::string xmlEscape(const std::string& bytes);
00777
00784 MTRand& getRandomizer();
00785
00787
00788 }
00789
00790 }
00791
00795 #endif