00001
00002
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 #ifndef ASTYLE_MAIN_H
00028 #define ASTYLE_MAIN_H
00029
00030
00031
00032
00033
00034 #include <ctime>
00035 #include "astyle.h"
00036
00037 #if defined(_MSC_VER) || defined(__DMC__)
00038 #include <sys/utime.h>
00039 #include <sys/stat.h>
00040 #else
00041 #include <utime.h>
00042 #include <sys/stat.h>
00043 #endif // end compiler checks
00044
00045 #ifdef ASTYLE_JNI
00046 #include <jni.h>
00047 #ifndef ASTYLE_LIB // ASTYLE_LIB must be defined for ASTYLE_JNI
00048 #define ASTYLE_LIB
00049 #endif
00050 #endif // ASTYLE_JNI
00051
00052
00053 #if defined(__GNUC__) && __GNUC__ < 3
00054 #error - Use GNU C compiler release 3 or higher
00055 #endif
00056
00057
00058 #if defined(_MSC_VER) && _MSC_VER < 1200 // check for V6.0
00059 #error - Use Microsoft compiler version 6 or higher
00060 #endif
00061
00062
00063
00064 namespace astyle
00065 {
00066
00067
00068 enum processReturn { CONTINUE, END_SUCCESS, END_FAILURE };
00069
00070
00071
00072
00073
00074
00075
00076 template<typename T>
00077 class ASStreamIterator : public ASSourceIterator
00078 {
00079 public:
00080 bool checkForEmptyLine;
00081
00082
00083 ASStreamIterator(T *in);
00084 virtual ~ASStreamIterator();
00085 bool getLineEndChange(int lineEndFormat) const;
00086 string nextLine(bool emptyLineWasDeleted);
00087 string peekNextLine();
00088 void peekReset();
00089 void saveLastInputLine();
00090
00091 private:
00092 T * inStream;
00093 string buffer;
00094 string prevBuffer;
00095 int eolWindows;
00096 int eolLinux;
00097 int eolMacOld;
00098 char outputEOL[4];
00099 streamoff peekStart;
00100 bool prevLineDeleted;
00101
00102 public:
00103 bool compareToInputBuffer(const string &nextLine) const
00104 { return (nextLine == prevBuffer); }
00105 const char* getOutputEOL() const { return outputEOL; }
00106 bool hasMoreLines() const { return !inStream->eof(); }
00107 };
00108
00109
00110
00111
00112
00113 class ASConsole
00114 {
00115 private:
00116
00117 bool isRecursive;
00118 string origSuffix;
00119 bool noBackup;
00120 bool preserveDate;
00121 bool isVerbose;
00122 bool isQuiet;
00123 bool isFormattedOnly;
00124 bool optionsFileRequired;
00125
00126 bool hasWildcard;
00127 size_t mainDirectoryLength;
00128 bool filesAreIdentical;
00129 bool lineEndsMixed;
00130 int linesOut;
00131 int filesFormatted;
00132 int filesUnchanged;
00133 char outputEOL[4];
00134 char prevEOL[4];
00135
00136 string optionsFileName;
00137 string targetDirectory;
00138 string targetFilename;
00139
00140 vector<string> excludeVector;
00141 vector<bool> excludeHitsVector;
00142 vector<string> fileNameVector;
00143 vector<string> optionsVector;
00144 vector<string> fileOptionsVector;
00145 vector<string> fileName;
00146
00147 public:
00148 ASConsole() {
00149
00150 isRecursive = false;
00151 origSuffix = ".orig";
00152 noBackup = false;
00153 preserveDate = false;
00154 isVerbose = false;
00155 isQuiet = false;
00156 isFormattedOnly = false;
00157 optionsFileRequired = false;
00158
00159 hasWildcard = false;
00160 filesAreIdentical = true;
00161 lineEndsMixed = false;
00162 outputEOL[0] = '\0';
00163 prevEOL[0] = '\0';
00164 mainDirectoryLength = 0;
00165 filesFormatted = 0;
00166 filesUnchanged = 0;
00167 linesOut = 0;
00168 }
00169
00170
00171 void convertLineEnds(ostringstream& out, int lineEnd);
00172 void error(const char *why, const char* what) const;
00173 void formatCinToCout(ASFormatter& formatter) const;
00174 FileEncoding getFileEncoding(ifstream& in) const;
00175 bool fileNameVectorIsEmpty();
00176 int getFilesFormatted();
00177 int getFilesUnchanged();
00178 bool getIsFormattedOnly();
00179 bool getIsQuiet();
00180 bool getIsRecursive();
00181 bool getIsVerbose();
00182 bool getLineEndsMixed();
00183 bool getNoBackup();
00184 string getOptionsFileName();
00185 bool getOptionsFileRequired();
00186 string getOrigSuffix();
00187 bool getPreserveDate();
00188 void processFiles(ASFormatter &formatter);
00189 processReturn processOptions(int argc, char** argv, ASFormatter &formatter);
00190 void setIsFormattedOnly(bool state);
00191 void setIsQuiet(bool state);
00192 void setIsRecursive(bool state);
00193 void setIsVerbose(bool state);
00194 void setNoBackup(bool state);
00195 void setOptionsFileName(string name);
00196 void setOptionsFileRequired(bool state);
00197 void setOrigSuffix(string suffix);
00198 void setPreserveDate(bool state);
00199 void standardizePath(string &path, bool removeBeginningSeparator=false) const;
00200 bool stringEndsWith(const string &str, const string &suffix) const;
00201 void updateExcludeVector(string suffixParam);
00202
00203
00204 vector<string> getExcludeVector();
00205 vector<bool> getExcludeHitsVector();
00206 vector<string> getFileNameVector();
00207 vector<string> getOptionsVector();
00208 vector<string> getFileOptionsVector();
00209 vector<string> getFileName();
00210
00211 private:
00212 void correctMixedLineEnds(ostringstream& out);
00213 void formatFile(const string &fileName, ASFormatter &formatter);
00214 string getCurrentDirectory(const string &fileName) const;
00215 void getFileNames(const string &directory, const string &wildcard);
00216 void getFilePaths(string &filePath);
00217 void initializeOutputEOL(LineEndFormat lineEndFormat);
00218 bool isPathExclued(const string &subPath);
00219 void printBadEncoding(FileEncoding encoding) const;
00220 void printHelp() const;
00221 void printMsg(const string &msg) const;
00222 void printVerboseHeader() const;
00223 void printVerboseStats(clock_t startTime) const;
00224 void removeFile(const char* fileName, const char* errMsg) const;
00225 void renameFile(const char* oldFileName, const char* newFileName, const char* errMsg) const;
00226 void setOutputEOL(LineEndFormat lineEndFormat, const char* currentEOL);
00227 void sleep(int seconds) const;
00228 int waitForRemove(const char* oldFileName) const;
00229 int wildcmp(const char *wild, const char *data) const;
00230 void writeOutputFile(const string &fileName, ostringstream &out) const;
00231 };
00232
00233
00234
00235
00236
00237
00238
00239 void importOptions(istream &in, vector<string> &optionsVector);
00240 void isOptionError(const string &arg, const string &errorInfo);
00241 bool isParamOption(const string &arg, const char *option);
00242 bool isParamOption(const string &arg, const char *option1, const char *option2);
00243 bool parseOption(astyle::ASFormatter &formatter, const string &arg, const string &errorInfo);
00244
00245 template<typename ITER>
00246 bool parseOptions(astyle::ASFormatter &formatter, const ITER &optionsBegin,
00247 const ITER &optionsEnd, const string &errorInfo);
00248
00249 }
00250
00251
00252
00253
00254
00255
00256 #ifdef ASTYLE_JNI
00257 void STDCALL javaErrorHandler(int errorNumber, char* errorMessage);
00258 char* STDCALL javaMemoryAlloc(unsigned long memoryNeeded);
00259
00260 extern "C" EXPORT
00261 jstring STDCALL Java_AStyleInterface_AStyleGetVersion(JNIEnv* env, jclass);
00262 extern "C" EXPORT
00263 jstring STDCALL Java_AStyleInterface_AStyleMain
00264 (JNIEnv* env, jobject obj, jstring textInJava, jstring optionsJava);
00265 #endif // ASTYLE_JNI
00266
00267
00268 #endif // closes ASTYLE_MAIN_H