PUtilities.h

Go to the documentation of this file.
00001 /*
00002     LoopTK: Protein Loop Kinematic Toolkit
00003     Copyright (C) 2007 Stanford University
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License along
00016     with this program; if not, write to the Free Software Foundation, Inc.,
00017     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018 */
00019 
00020 #ifndef PUTILITIES_H
00021 #define PUTILITIES_H
00022 
00023 #include "PLibraries.h"
00024 
00025 #define         isNAN(x)                !((x) <= 0 || (x) > 0)
00026 #define         ends_with(str, x)       (string(str).find(string(x)) != string::npos && \
00027                                          string(str).find(string(x)) == string(str).length() - \
00028                                          string(x).length())
00029 //@package Utilities
00040 class PUtilities {
00041   public:
00042 
00043 // File/directory manipulation functions -- might not be portable
00044 // to all operating systems (Windows?)
00045 
00051     static void copyFile(const string &newFileName, const string &fileToCopy);
00052 
00058     static void makeDirectory(const string &dirName);
00059 
00060 // String manipulation functions
00061 
00067     static void trimSpaces(string &str);
00068 
00075      template <typename T>
00076      static string toStr(const T &x)
00077      {
00078        stringstream ss;
00079        ss << x;
00080        return ss.str();
00081      }
00082 
00088     static string padLeft(const string &s, unsigned int len, char padChar = ' ');
00089 
00095     static string padRight(const string &s, unsigned int len, char padChar = ' ');
00096 
00109     static vector<string> getLinesStarting(const vector<string> &lines,
00110                                 const string &prefix, bool trimPrefix = false,
00111                                 const string &terminationCode = "");
00112 
00118     static vector<string> getTokens(const string &curLine);
00119 
00120 // Text I/O functions
00121 
00134     static vector<string> getLinesStarting(const string &fileName,
00135                                 const string &prefix, bool trimPrefix = false,
00136                                 const string &terminationCode = "");
00137 
00143     static vector<string> getLines(const string &fileName);
00144 
00149     static void appendToFile(const string &fileName, const string &line);
00150 
00151 // Miscellaneous functions
00152 
00158     static void AbortProgram(const string &message);
00159 
00165     static const void *PointerThatIsNot(const void *p1, const void *p2, const void *have);
00166 
00172     template <typename T>
00173     static T** New2DArray(unsigned size) {
00174       T** table = new T*[size];
00175 
00176       for(unsigned i = 0; i < size; i++) {
00177         table[i] = new T[size];
00178       }
00179 
00180       return table;
00181     }
00182 
00188     template <typename T>
00189     static void Delete2DArray(T **table, unsigned size) {
00190       for(unsigned i = 0; i < size; i++) {
00191         delete[] table[i];
00192       }
00193 
00194       delete[] table;
00195     }
00196 
00197 };
00198 
00199 #endif

Generated on Wed May 16 20:22:08 2007 for LoopTK: Protein Loop Kinematic Toolkit by  doxygen 1.5.1