📄 astyle_main.cpp
字号:
vector<string> subDirectory; // sub directories of directory WIN32_FIND_DATA FindFileData; // for FindFirstFile and FindNextFile // Find the first file in the directory string firstFile = directory + "\\*"; HANDLE hFind = FindFirstFile(firstFile.c_str(), &FindFileData); if (hFind == INVALID_HANDLE_VALUE) error("Cannot open directory", directory.c_str()); // save files and sub directories do { // skip hidden or read only if (FindFileData.cFileName[0] == '.' || (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) || (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) continue; // if a sub directory and recursive, save sub directory if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && g_isRecursive) { string subDirectoryPath = directory + g_fileSeparator + FindFileData.cFileName; if (isPathExclued(subDirectoryPath)) { if (!g_isQuiet) cout << "exclude " << subDirectoryPath.substr(g_mainDirectoryLength) << endl; } else subDirectory.push_back(subDirectoryPath); continue; } // save the file name string filePathName = directory + g_fileSeparator + FindFileData.cFileName; // check exclude before wildcmp to avoid "unmatched exclude" error bool isExcluded = isPathExclued(filePathName); // save file name if wildcard match if (wildcmp(wildcard.c_str(), FindFileData.cFileName)) { if (isExcluded) cout << "exclude " << filePathName.substr(g_mainDirectoryLength) << endl; else fileName.push_back(filePathName); } } while (FindNextFile(hFind, &FindFileData) != 0); // check for processing error FindClose(hFind); DWORD dwError = GetLastError(); if (dwError != ERROR_NO_MORE_FILES) error("Error processing directory", directory.c_str()); // recurse into sub directories // if not doing recursive subDirectory is empty for (unsigned i = 0; i < subDirectory.size(); i++) {// cout << "directory " << subDirectory[i] << endl; getFileNames(subDirectory[i], wildcard, fileName); continue; } return;}/** * WINDOWS function to get the current directory. * NOTE - getenv("CD") does not work for Windows Vista. * The Wndows function GetCurrentDirectory is used instead. * * @return The path of the current directory */string getCurrentDirectory(const string &fileName){ char currdir[MAX_PATH]; currdir[0] = '\0'; if (!GetCurrentDirectory(sizeof(currdir), currdir)) error("Cannot find file", fileName.c_str()); return string(currdir);}#else // not _WIN32/** * LINUX function to resolve wildcards and recurse into sub directories. * The fileName vector is filled with the path and names of files to process. * * @param directory The path of the directory to be processed. * @param wildcard The wildcard to be processed (e.g. *.cpp). * @param filenam An empty vector which will be filled with the path and names of files to process. */void getFileNames(const string &directory, const string &wildcard, vector<string> &fileName){ struct dirent *entry; // entry from readdir() struct stat statbuf; // entry from stat() vector<string> subDirectory; // sub directories of this directory // errno is defined in <errno.h> and is set for errors in opendir, readdir, or stat errno = 0; DIR *dp = opendir(directory.c_str()); if (errno) error("Cannot open directory", directory.c_str()); // save the first fileName entry for this recursion const unsigned firstEntry = fileName.size(); // save files and sub directories while ((entry = readdir(dp)) != NULL) { // get file status string entryFilepath = directory + g_fileSeparator + entry->d_name; stat(entryFilepath.c_str(), &statbuf); if (errno) error("Error getting file status in directory", directory.c_str()); // skip hidden or read only if (entry->d_name[0] == '.' || !(statbuf.st_mode & S_IWUSR)) continue; // if a sub directory and recursive, save sub directory if (S_ISDIR(statbuf.st_mode) && g_isRecursive) { if (isPathExclued(entryFilepath)) cout << "exclude " << entryFilepath.substr(g_mainDirectoryLength) << endl; else subDirectory.push_back(entryFilepath); continue; } // if a file, save file name if (S_ISREG(statbuf.st_mode)) { // check exclude before wildcmp to avoid "unmatched exclude" error bool isExcluded = isPathExclued(entryFilepath); // save file name if wildcard match if (wildcmp(wildcard.c_str(), entry->d_name)) { if (isExcluded) cout << "exclude " << entryFilepath.substr(g_mainDirectoryLength) << endl; else fileName.push_back(entryFilepath); } } } closedir(dp); if (errno) error("Error reading directory", directory.c_str()); // sort the current entries for fileName if (firstEntry < fileName.size()) sort(&fileName[firstEntry], &fileName[fileName.size()]); // recurse into sub directories // if not doing recursive, subDirectory is empty if (subDirectory.size() > 1) sort(subDirectory.begin(), subDirectory.end()); for (unsigned i = 0; i < subDirectory.size(); i++) {// cout << "directory " << subDirectory[i] << endl; getFileNames(subDirectory[i], wildcard, fileName); continue; } return;}/** * LINUX function to get the current directory. * This is done if the fileSpec does not contain a path. * It is probably from an editor sending a single file. * * @param fileName The filename is used only for the error message. * @return The path of the current directory */string getCurrentDirectory(const string &fileName){ char *currdir = getenv("PWD"); if (currdir == NULL) error("Cannot find file", fileName.c_str()); return string(currdir);}#endif // _WIN32// From The Code Project http://www.codeproject.com/string/wildcmp.asp// Written by Jack Handy - jakkhandy@hotmail.com// Modified to compare case insensitive for Windows (the LC macro)int wildcmp(const char *wild, const char *data){ const char *cp = NULL, *mp = NULL; bool cmpval; while ((*data) && (*wild != '*')) { if (!g_isCaseSensitive) cmpval = (tolower(*wild) != tolower(*data)) && (*wild != '?'); else cmpval = (*wild != *data) && (*wild != '?'); if (cmpval) { return 0; } wild++; data++; } while (*data) { if (*wild == '*') { if (!*++wild) { return 1; } mp = wild; cp = data+1; } else { if (!g_isCaseSensitive) cmpval = (tolower(*wild) == tolower(*data) || (*wild == '?')); else cmpval = (*wild == *data) || (*wild == '?'); if (cmpval) { wild++; data++; } else { wild = mp; data = cp++; } } } while (*wild == '*') { wild++; } return !*wild;}// compare a path to the exclude vector// used for both directories and filenames// return true if a matchbool isPathExclued(const string &subPath){ bool retVal = false; // read the exclude vector chacking for a match for (size_t i = 0; i < g_excludeVector.size(); i++) { string exclude = g_excludeVector[i]; if (subPath.length() > exclude.length()) { size_t compareStart = subPath.length() - exclude.length(); char lastPathChar = subPath[compareStart - 1]; // exclude must start with a directory name if (lastPathChar == g_fileSeparator) { string compare = subPath.substr(compareStart); if (!g_isCaseSensitive) { // make it case insensitive for Windows for (size_t j=0; j<compare.length(); j++) compare[j] = (char)tolower(compare[j]); for (size_t j=0; j<exclude.length(); j++) exclude[j] = (char)tolower(exclude[j]); } // compare sub directory to exclude data - must check them all if (compare == exclude) { g_excludeHitsVector[i] = true; retVal = true; break; } } } } return retVal;}// make sure file separators are correct type (Windows or Linux)// remove ending file separator// remove beginning file separatot if requested and NOT a complete file pathvoid standardizePath(string &path, bool removeBeginningSeparator /*false*/){#ifdef __VMS struct FAB fab; struct NAML naml; char less[NAML$C_MAXRSS]; char sess[NAM$C_MAXRSS]; int r0_status; // If we are on a VMS system, translate VMS style filenames to unix // style. fab = cc$rms_fab; fab.fab$l_fna = (char *)-1; fab.fab$b_fns = 0; fab.fab$l_naml = &naml; naml = cc$rms_naml; strcpy (sess, path.c_str()); naml.naml$l_long_filename = (char *)sess; naml.naml$l_long_filename_size = path.length(); naml.naml$l_long_expand = less; naml.naml$l_long_expand_alloc = sizeof (less); naml.naml$l_esa = sess; naml.naml$b_ess = sizeof (sess); naml.naml$v_no_short_upcase = 1; r0_status = sys$parse (&fab); if (r0_status == RMS$_SYN) { error("File syntax error", path.c_str()); } else { if (!$VMS_STATUS_SUCCESS(r0_status)) { (void)lib$signal (r0_status); } } less[naml.naml$l_long_expand_size - naml.naml$b_ver] = '\0'; sess[naml.naml$b_esl - naml.naml$b_ver] = '\0'; if (naml.naml$l_long_expand_size > naml.naml$b_esl) { path = decc$translate_vms (less); } else { path = decc$translate_vms (sess); }#endif /* __VMS */ // make sure separators are correct type (Windows or Linux) for (size_t i = 0; i < path.length(); i++) { i = path.find_first_of("/\\", i); if (i == string::npos) break; path[i] = g_fileSeparator; } // remove separator from the end if (path[path.length()-1] == g_fileSeparator) path.erase(path.length()-1, 1); // remove beginning separator if requested if (removeBeginningSeparator && (path[0] == g_fileSeparator)) path.erase(0, 1);}void error(const char *why, const char* what){ (*_err) << why << ' ' << what << '\n' << endl; exit(EXIT_FAILURE);}void printHelp(){ (*_err) << endl; (*_err) << " Artistic Style " << _version << endl; (*_err) << " Maintained by: Jim Pattee\n"; (*_err) << " Original Author: Tal Davidson\n"; (*_err) << endl; (*_err) << "Usage : astyle [options] Source1.cpp Source2.cpp [...]\n"; (*_err) << " astyle [options] < Original > Beautified\n"; (*_err) << endl; (*_err) << "When indenting a specific file, the resulting indented file RETAINS the\n"; (*_err) << "original file-name. The original pre-indented file is renamed, with a\n"; (*_err) << "suffix of \".orig\" added to the original filename.\n"; (*_err) << endl; (*_err) << "By default, astyle is set up to indent C/C++/C#/Java files, with 4 spaces\n"; (*_err) << "per indent, a maximal indentation of 40 spaces inside continuous statements,\n"; (*_err) << "and NO formatting.\n"; (*_err) << endl; (*_err) << "Option's Format:\n"; (*_err) << "----------------\n"; (*_err) << " Long options (starting with '--') must be written one at a time.\n"; (*_err) << " Short options (starting with '-') may be appended together.\n"; (*_err) << " Thus, -bps4 is the same as -b -p -s4.\n"; (*_err) << endl; (*_err) << "Predefined Style Options:\n"; (*_err) << "-------------------------\n"; (*_err) << " --style=ansi\n"; (*_err) << " ANSI style formatting/indenting.\n"; (*_err) << endl; (*_err) << " --style=gnu\n"; (*_err) << " GNU style formatting/indenting.\n"; (*_err) << endl; (*_err) << " --style=kr\n"; (*_err) << " Kernighan&Ritchie style formatting/indenting.\n"; (*_err) << endl; (*_err) << " --style=linux\n"; (*_err) << " Linux mode (8 spaces per indent, break definition-block\n"; (*_err) << " brackets but attach command-block brackets).\n"; (*_err) << endl; (*_err) << " --style=java\n"; (*_err) << " Java mode, with standard java style formatting/indenting.\n"; (*_err) << endl; (*_err) << "Tab and Bracket Options:\n"; (*_err) << "------------------------\n"; (*_err) << " default indent option\n"; (*_err) << " If no indentation option is set,\n"; (*_err) << " the default option of 4 spaces will be used.\n"; (*_err) << endl; (*_err) << " --indent=spaces=# OR -s#\n"; (*_err) << " Indent using # spaces per indent. Not specifying #\n"; (*_err) << " will result in a default of 4 spaces per indent.\n"; (*_err) << endl; (*_err) << " --indent=tab OR --indent=tab=# OR -t OR -t#\n"; (*_err) << " Indent using tab characters, assuming that each\n"; (*_err) << " tab is # spaces long. Not specifying # will result\n"; (*_err) << " in a default assumption of 4 spaces per tab.\n"; (*_err) << endl; (*_err) << " --force-indent=tab=# OR -T#\n"; (*_err) << " Indent using tab characters, assuming that each\n"; (*_err) << " tab is # spaces long. Force tabs to be used in areas\n"; (*_err) << " Astyle would prefer to use spaces.\n"; (*_err) << endl; (*_err) << " default brackets option\n"; (*_err) << " If no brackets option is set,\n"; (*_err) << " the brackets will not be changed.\n"; (*_err) << endl; (*_err) << " --brackets=break OR -b\n"; (*_err) << " Break brackets from pre-block code (i.e. ANSI C/C++ style).\n"; (*_err) << endl; (*_err) << " --brackets=attach OR -a\n"; (*_err) << " Attach brackets to pre-block code (i.e. Java/K&R style).\n"; (*_err) << endl; (*_err) << " --brackets=linux OR -l\n"; (*_err) << " Break definition-block brackets and attach command-block\n"; (*_err) << " brackets.\n"; (*_err) << endl; (*_err) << " --brackets=break-closing OR -y\n"; (*_err) << " Break brackets before closing headers (e.g. 'else', 'catch', ...).\n"; (*_err) << " Should be appended to --brackets=attach or --brackets=linux.\n"; (*_err) << endl; (*_err) << "Indentation options:\n"; (*_err) << "--------------------\n"; (*_err) << " --indent-classes OR -C\n"; (*_err) << " Indent 'class' blocks, so that the inner 'public:',\n"; (*_err) << " 'protected:' and 'private: headers are indented in\n"; (*_err) << " relation to the class block.\n"; (*_err) << endl; (*_err) << " --indent-switches OR -S\n"; (*_err) << " Indent 'switch' blocks, so that the inner 'case XXX:'\n"; (*_err) << " headers are indented in relation to the switch block.\n"; (*_err) << endl; (*_err) << " --indent-cases OR -K\n"; (*_err) << " Indent case blocks from the 'case XXX:' headers.\n"; (*_err) << " Case statements not enclosed in blocks are NOT indented.\n"; (*_err) << endl; (*_err) << " --indent-brackets OR -B\n"; (*_err) << " Add extra indentation to '{' and '}' block brackets.\n"; (*_err) << endl; (*_err) << " --indent-blocks OR -G\n"; (*_err) << " Add extra indentation entire blocks (including brackets).\n"; (*_err) << endl; (*_err) << " --indent-namespaces OR -N\n"; (*_err) << " Indent the contents of namespace blocks.\n"; (*_err) << endl; (*_err) << " --indent-labels OR -L\n"; (*_err) << " Indent labels so that they appear one indent less than\n"; (*_err) << " the current indentation level, rather than being\n"; (*_err) << " flushed completely to the left (which is the default).\n"; (*_err) << endl; (*_err) << " --indent-preprocessor OR -w\n"; (*_err) << " Indent multi-line #define statements.\n"; (*_err) << endl; (*_err) << " --max-instatement-indent=# OR -M#\n"; (*_err) << " Indent a maximal # spaces in a continuous statement,\n"; (*_err) << " relative to the previous line.\n"; (*_err) << endl; (*_err) << " --min-conditional-indent=# OR -m#\n"; (*_err) << " Indent a minimal # spaces in a continuous conditional\n"; (*_err) << " belonging to a conditional header.\n"; (*_err) << endl; (*_err) << "Formatting options:\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -