00001 #ifndef ROSE_STRING_UTILITY_H
00002 #define ROSE_STRING_UTILITY_H
00003
00004
00005 #include "commandline_processing.h"
00006 #include <vector>
00007 #include <map>
00008 #include <string>
00009 #include <sstream>
00010 #include <stdint.h>
00011 #if ROSE_MICROSOFT_OS
00012
00013 #include <msvc_stdint.h>
00014 #else
00015 #endif
00016
00017
00018
00019
00020
00021
00022
00023
00024 namespace StringUtility
00025 {
00026
00027
00028
00029
00030 std::string getAbsolutePathFromRelativePath ( const std::string & relativePath, bool printErrorIfAny = false);
00031
00032 struct StringWithLineNumber
00033 {
00034 std::string str;
00035 std::string filename;
00036 unsigned int line;
00037
00038 StringWithLineNumber(const std::string& str, const std::string& filename, unsigned int line): str(str), filename(filename), line(line) {}
00039
00040 std::string toString() const;
00041 };
00042
00043 #ifndef USE_ROSE
00044 typedef std::vector<StringWithLineNumber> FileWithLineNumbers;
00045 #else
00046
00047
00048 }
00049
00050 namespace StringUtility
00051 {
00052 typedef std::vector<StringUtility::StringWithLineNumber> FileWithLineNumbers;
00053 #endif
00054
00055 inline std::ostream& operator<<(std::ostream& os, const StringWithLineNumber& s) {
00056 os << s.toString();
00057 return os;
00058 }
00059
00060 std::string toString(const FileWithLineNumbers& strings, const std::string& filename = "<unknown>", int line = 1);
00061
00062 inline FileWithLineNumbers& operator+=(FileWithLineNumbers& a, const FileWithLineNumbers& b) {
00063 a.insert(a.end(), b.begin(), b.end());
00064 return a;
00065 }
00066
00067 inline FileWithLineNumbers operator+(const FileWithLineNumbers& a, const FileWithLineNumbers& b) {
00068 FileWithLineNumbers f = a;
00069 f += b;
00070 return f;
00071 }
00072
00073 #if 0
00074 inline std::ostream& operator<<(std::ostream& os, const FileWithLineNumbers& f) {
00075 os << StringUtility::toString(f);
00076 return os;
00077 }
00078 #endif
00079
00080 inline FileWithLineNumbers& operator<<(FileWithLineNumbers& f, const std::string& str) {
00081
00082 if (!f.empty() && f.back().filename == "") {
00083 f.back().str += str;
00084 } else {
00085 f.push_back(StringWithLineNumber(str, "", 1));
00086 }
00087 return f;
00088 }
00089
00090 inline FileWithLineNumbers& operator<<(FileWithLineNumbers& f, const char* str) {
00091 f << std::string(str);
00092 return f;
00093 }
00094
00095 #if 0
00096
00098 std::string getVariantName ( int v );
00099 #endif
00100
00102 void writeFile ( const std::string& outputString, const std::string& fileNameString, const std::string& directoryName );
00103
00105 std::string readFile ( const std::string& fileName );
00106
00108 FileWithLineNumbers readFileWithPos(const std::string& fileName);
00109
00117
00118 std::string copyEdit ( const std::string& inputString, const std::string & oldToken, const std::string & newToken );
00120 std::string numberToString ( long long x );
00121 std::string numberToString ( unsigned long long x );
00122 std::string numberToString ( long x );
00123 std::string numberToString ( unsigned long x );
00124 std::string numberToString ( int x );
00125 std::string numberToString ( unsigned int x );
00127 std::string intToHex(uint64_t i);
00129
00130
00131
00132
00133
00135 std::string numberToString ( const void* x );
00136
00138 std::string numberToString ( double x );
00140 std::string addrToString( uint64_t x );
00141
00143 std::string indentMultilineString ( const std::string& inputString, int statementColumnNumber );
00144
00146 std::string listToString ( const std::list<int> & X, bool separateStrings = false );
00148 std::string listToString ( const std::list<std::string> & X, bool separateStrings = false );
00150 std::list<std::string> stringToList ( const std::string & X );
00151
00153 std::string listToString ( const std::vector<std::string> & X, bool separateStrings = false );
00154
00156 std::string removeRedundentSubstrings ( std::string X );
00158 std::string removePseudoRedundentSubstrings ( std::string X );
00170
00171
00172
00173
00174 std::string copyEdit ( const std::string& inputString, const std::string& oldToken, const std::string& newToken );
00175 FileWithLineNumbers copyEdit ( const FileWithLineNumbers& inputString, const std::string& oldToken, const std::string& newToken );
00176 FileWithLineNumbers copyEdit ( const FileWithLineNumbers& inputString, const std::string& oldToken, const FileWithLineNumbers& newToken );
00177
00178 inline bool isContainedIn ( const std::string & longString, const std::string & shortString ) {
00179 return longString.find(shortString) != std::string::npos;
00180 }
00181
00182
00184
00185
00187 void splitStringIntoStrings( const std::string& inputString, char separator, std::vector<std::string>& stringList );
00190
00191
00192 unsigned long generate_checksum( std::string s );
00193
00195 std::string convertToLowerCase( const std::string & inputString );
00196
00197
00198
00200 bool popen_wrapper ( const std::string & command, std::vector<std::string> & result );
00201
00203 std::string demangledName ( std::string s );
00204
00205
00207
00211
00213 std::string stripPathFromFileName ( const std::string & fileNameWithPath );
00215 std::string getPathFromFileName ( const std::string & fileNameWithPath );
00217 std::string stripFileSuffixFromFileName ( const std::string & fileNameWithSuffix );
00219 std::string getAbsolutePathFromRelativePath ( const std::string & relativePath, bool printErrorIfAny );
00221 std::string fileNameSuffix ( const std::string & fileName );
00222
00223
00224
00225
00227
00228
00229
00230
00231
00232
00233
00234
00235 std::list<std::string> findfile(std::string patternString, std::string pathString);
00236
00238 std::string escapeNewLineCharaters ( const std::string & X );
00239
00240
00241 enum OSType
00242 {
00243 OS_TYPE_UNKNOWN,
00244 OS_TYPE_LINUX,
00245 OS_TYPE_OSX,
00246 OS_TYPE_WINDOWS,
00247 OS_TPYE_WINDOWSXP};
00248
00249
00250 OSType getOSType();
00251
00252
00253 void homeDir(std::string& homeDir);
00254
00255
00256
00257
00258
00259 enum FileNameLocation
00260 {
00261 FILENAME_LOCATION_UNKNOWN,
00262 FILENAME_LOCATION_USER,
00263 FILENAME_LOCATION_LIBRARY,
00264 FILENAME_LOCATION_NOT_EXIST };
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 static const std::string FILENAME_LIBRARY_UNKNOWN = "Unknown";
00284 static const std::string FILENAME_LIBRARY_USER = "User";
00285 static const std::string FILENAME_LIBRARY_C = "C";
00286 static const std::string FILENAME_LIBRARY_STDCXX = "C++";
00287 static const std::string FILENAME_LIBRARY_STL = "STL";
00288 static const std::string FILENAME_LIBRARY_LINUX = "Linux";
00289 static const std::string FILENAME_LIBRARY_GCC = "GCC";
00290 static const std::string FILENAME_LIBRARY_BOOST = "Boost";
00291 static const std::string FILENAME_LIBRARY_ROSE = "Rose";
00292
00293
00294 typedef std::string FileNameLibrary;
00295
00296
00297
00298 class FileNameClassification
00299 {
00300 private:
00301 FileNameLocation location;
00302
00303
00304 FileNameLibrary library;
00305
00306 int distance;
00307
00308 public:
00309 FileNameClassification(FileNameLocation loc,
00310 const FileNameLibrary& lib,
00311 int dist) : location(loc),
00312 library(lib),
00313 distance(dist)
00314 {}
00315 FileNameClassification() : location(FILENAME_LOCATION_UNKNOWN),
00316 library("Unknown"),
00317 distance(0)
00318 {}
00319
00320
00321
00322 FileNameLocation getLocation() const
00323 { return location; }
00324
00325
00326
00327 FileNameLibrary getLibrary() const
00328 { return library; }
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 int getDistanceFromSourceDirectory() const
00349 { return distance; }
00350
00351 bool isUserCode() const
00352 { return location == FILENAME_LOCATION_USER; }
00353 bool isLibraryCode() const
00354 { return location == FILENAME_LOCATION_LIBRARY; }
00355
00356
00357
00358 std::string getLibraryName() const
00359 { return library; }
00360 };
00361
00362
00363
00364
00365
00366
00367
00368 FileNameClassification classifyFileName(const std::string& fileName,
00369 const std::string& appPath);
00370
00371
00372
00373
00374
00375 FileNameClassification classifyFileName(const std::string& fileName,
00376 const std::string& appPath,
00377 OSType os);
00378
00379
00380
00381
00382
00383
00384
00385 FileNameClassification classifyFileName(const std::string& fileName,
00386 const std::string& appPath,
00387 const std::map<std::string, std::string>& libPathCollection);
00388
00389
00390
00391
00392
00393
00394 FileNameClassification classifyFileName(const std::string& fileName,
00395 const std::string& appPath,
00396 const std::map<std::string, std::string>& libPathCollection,
00397 OSType os);
00398
00399
00400
00401 const std::string
00402 stripDotsFromHeaderFileName(const std::string& name);
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 int directoryDistance(const std::string& left,
00417 const std::string& right);
00418
00419
00420
00421 std::string htmlEscape(const std::string& s);
00422
00423
00424 std::vector<std::string> readWordsInFile( std::string filename);
00425
00426 };
00427
00428
00429
00430 #endif
00431
00432
00433