string_functions.h

Go to the documentation of this file.
00001 #ifndef ROSE_STRING_UTILITY_H
00002 #define ROSE_STRING_UTILITY_H
00003 
00004 // Move this to rose.h or a build a rose_utility.h file later
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 // This is the boost solution for lack of support for stdint.h (e.g. types such as "uint64_t")
00013 #include <msvc_stdint.h>
00014 #else
00015 #endif
00016 
00017 // extern const char** roseGlobalVariantNameList;
00018 //Rama: 12/14/06: Changed the class to namespace and removed 'static'ness of the erstwhile "member functions"
00019 //There is still a lot of clean up do be done: Like 
00020 //      reorganize the functions
00021 //      Make a single utililities namespace for ROSE with different functionalites
00022 //      including the functions in util directory.
00023 
00024 namespace StringUtility
00025    {
00026   // Container class for numerous string utility functions that are useful in different parts of the ROSE project.
00027   // Rama (12/22/2006): Clearly, some of the following are not string utilities, but file utilities.
00028   // This class is the wrapper around the realpath of UNIX
00029 
00030      std::string getAbsolutePathFromRelativePath ( const std::string & relativePath, bool printErrorIfAny = false); // Real declaration is below
00031 
00032      struct StringWithLineNumber
00033         {
00034           std::string str;      // DQ (1/23/2010): this name is difficult to trace within the code.
00035           std::string filename; // Empty string means generated code
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   // workaround of bug 315, separating definitions for a namespace
00047   // Liao, 2/16/2009
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            // Add loose text to the output file
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          // enum VariantT;
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        //  string numberToString ( unsigned int x );
00130        //  std::string numberToString ( size_t x );
00131 
00132 
00133        // DQ (8/10/2010): Changed to take parameter as const.
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            // int isSameName ( const std::string& s1, const std::string& s2 );
00172 
00173            // char* stringDuplicate ( const char* tempString );
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        //  bool isContainedIn ( const char* longString, const char* shortString );
00178            inline bool isContainedIn ( const std::string & longString, const std::string & shortString ) {
00179              return longString.find(shortString) != std::string::npos;
00180            }
00181 
00182 
00184            // char* stringConcatinate ( const char* targetString , const char* endingString );
00185 
00187            void splitStringIntoStrings( const std::string& inputString, char separator, std::vector<std::string>& stringList );
00190 
00191        //  unsigned short int chksum(char *buffer, int len);
00192            unsigned long generate_checksum( std::string s );
00193 
00195            std::string convertToLowerCase( const std::string & inputString );
00196 
00197    // std::string mangledName ( std::string s );
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        // DQ (3/5/2006): Copies from ROSE class (deprecated in there previous location)
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 /* = false */ );       
00221            std::string fileNameSuffix                  ( const std::string & fileName );          
00222 
00223   // True only if this is a valid C++ source file name extension (suffix). Duplicate of CommandlineProcessing::isCppFileNameSuffix(). 
00224 //           bool isCppFileNameSuffix                    ( const std::string & fileName ); 
00225 
00227        /*
00228         * The function 
00229         * findfile
00230         * traverse the current directory, searching
00231         * for files with a given string in their name.
00232         * input:    string to match and directory to match it in.
00233         * output:   any file found, returned as a list of strings with a full path.
00234         */
00235            std::list<std::string> findfile(std::string patternString, std::string pathString);
00236 
00238            std::string escapeNewLineCharaters ( const std::string & X );
00239 
00240            // RSS 7/1/2008 New functionality to support filename processing
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            // Return OSType based on uname kernel name results
00250            OSType getOSType();
00251 
00252            // Populate homeDir from $HOME environment var
00253            void homeDir(std::string& homeDir);
00254 
00255            /* Files can be classified as being in one of three
00256             * locations: We don't know if it's user or system It is a
00257             * user (application) file It is a system library This file
00258             * does not exist */
00259            enum FileNameLocation 
00260                                { 
00261                                                            FILENAME_LOCATION_UNKNOWN, 
00262                                    FILENAME_LOCATION_USER,    
00263                                    FILENAME_LOCATION_LIBRARY,
00264                                    FILENAME_LOCATION_NOT_EXIST };
00265            
00266            /* Files can be classified as being part of one of these
00267             * libraries: Unknown, it isn't a library - it's part of
00268             * the user application, or any of the libraries that the
00269             * enum values imply, this list will likely be added to
00270             * over time */
00271            /* 
00272            enum FileNameLibrary { FILENAME_LIBRARY_UNKNOWN,
00273                                   FILENAME_LIBRARY_USER,
00274                                   FILENAME_LIBRARY_C,
00275                                   FILENAME_LIBRARY_STDCXX,
00276                                   FILENAME_LIBRARY_STL,
00277                                   FILENAME_LIBRARY_LINUX,
00278                                   FILENAME_LIBRARY_GCC,
00279                                   FILENAME_LIBRARY_BOOST,
00280                                   FILENAME_LIBRARY_ROSE };
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            // CH (2/16/2010): Use this typedef to avoid following changes
00294            typedef std::string FileNameLibrary;
00295 
00296            /* This is the return type of classifyFileName, which
00297             * provides all the details it infers */
00298            class FileNameClassification
00299            {
00300            private:
00301                FileNameLocation location;
00302 
00303                // CH (2/12/2010): Change 'library' type from enum to string to let user set it
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                /* Return the FileNameLocation which is described above
00321                 * with the definition of the enum */
00322                FileNameLocation getLocation() const
00323                    { return location; }
00324 
00325                /* Return the FileNameLibrary which is described above
00326                 * with the definition of the enum */
00327                FileNameLibrary getLibrary() const
00328                    { return library; }
00329 
00330                /* Return the "distance" of the filename from the
00331                 * appPath that was supplied during the call.  The
00332                 * distance is defined as the number of cd's that only
00333                 * move up or down one directory that it would take to
00334                 * move from the directory of the filename to the
00335                 * directory that was given by appPath.  This is
00336                 * intended as a heuristic to gage whether or not one
00337                 * believes that the filename is related to the source
00338                 * (appPath) directory.  Examples:
00339                 *
00340                 * Between /a/b/c/file.h and /a/b/d/e/ the distance is 3
00341                 * because one must cd ..; cd d; cd e; to get to appPath
00342                 *
00343                 * *EXCEPTION*: if the appPath is an ancestor of filename
00344                 * then the distance will be 0.  The idea being that this
00345                 * filename is "in" the appPath somewhere and thus part
00346                 * of the application.
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                /* Return a string name for the library indicated by
00357                 * getLibrary() */
00358                std::string getLibraryName() const
00359                    { return library; }
00360            };
00361 
00362            /* Given a fileName and an appPath that is a path to some
00363             * application's source code directory, return a
00364             * FileNameClassification indicating whether the fileName
00365             * is part of the source code or some system library and
00366             * automatically determine the operating system from the
00367             * host uname */
00368            FileNameClassification classifyFileName(const std::string& fileName,
00369                                                    const std::string& appPath);
00370 
00371            /* Given a fileName and an appPath that is a path to some
00372             * application's source code directory, return a
00373             * FileNameClassification indicating whether the fileName
00374             * is part of the source code or some system library */
00375            FileNameClassification classifyFileName(const std::string& fileName,
00376                                                    const std::string& appPath,
00377                                                    OSType os);
00378 
00379            /* Given a fileName and an appPath that is a path to some
00380             * application's source code directory, and a collection 
00381             * of library paths, return a FileNameClassification
00382             * indicating whether the fileName is part of the source 
00383             * code or some system library and automatically determine 
00384             * the operating system from the host uname */
00385            FileNameClassification classifyFileName(const std::string& fileName,
00386                                                    const std::string& appPath,
00387                                                    const std::map<std::string, std::string>& libPathCollection);
00388 
00389            /* Given a fileName and an appPath that is a path to some
00390             * application's source code directory, and a collection 
00391             * of library paths, return a FileNameClassification
00392             * indicating whether the fileName is part of the source 
00393             * code or some system library */ 
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            /* Remove leading dots plus a space from a header file name
00400             * that is fiven in the format that g++ -H returns */
00401            const std::string
00402            stripDotsFromHeaderFileName(const std::string& name);
00403 
00404            /* Essentially the edit distance without substituion in
00405             * directory name tokens between two directories. Returns
00406             * the "distance" between left and right. The distance is
00407             * defined as the number of cd's that only move up or down
00408             * one directory that it would take to move from the
00409             * directory of the filename to the directory that was
00410             * given by appPath.  This is intended as a heuristic to
00411             * gage whether or not one believes that the left is
00412             * related to the right directory.  Examples:
00413             *
00414             * Between /a/b/c/file.h and /a/b/d/e/ the distance is 3
00415             * because one must cd ..; cd d; cd e */
00416             int directoryDistance(const std::string& left,
00417                                   const std::string& right);
00418 
00419 
00420             /* Added htmlEscape necessary for QROSE work to this utility library - tps (9Oct2008) */
00421             std::string htmlEscape(const std::string& s);
00422 
00423     // DQ (2/3/2009): Moved this function from attach_all_info.C
00424        std::vector<std::string> readWordsInFile( std::string filename);
00425 
00426    };
00427 
00428 
00429 // endif for ROSE_STRING_UTILITY_H
00430 #endif
00431 
00432 
00433 

Generated on Tue Jan 31 05:31:38 2012 for ROSE by  doxygen 1.4.7