📄 staffilesystem.cpp
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2001 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/#include "STAF.h"#include <deque>#include <algorithm>#include "STAFFileSystem.h"#include "STAFThreadManager.h"// Typesstruct STAFFSEntryImpl{ STAFFSEntryImpl(const STAFString &thePathString, STAFFSEntryType_t theType, unsigned int theHighSize, unsigned int theLowSize, time_t theModTime) : pathString(thePathString), type(theType), highSize(theHighSize), lowSize(theLowSize), modTime(theModTime) { /* Do Nothing*/ } STAFString pathString; STAFFSEntryType_t type; unsigned int highSize; unsigned int lowSize; time_t modTime;};typedef std::deque<STAFFSEntry_t> STAFFSEntryList;struct STAFFSEnumHandleImpl{ STAFFSEntryList entries;};struct STAFFSOSFileLockImpl{ STAFFSOSFileLockImpl(HANDLE theHandle) : handle(theHandle) { /* Do Nothing */ } HANDLE handle;};// Some global stringsstatic STAFString sSlash(kUTF8_SLASH);static STAFString sBackSlash(kUTF8_BSLASH);static STAFString sBothSlash(sSlash + sBackSlash);static STAFString s2BackSlashes(sBackSlash + sBackSlash);static STAFString s2Slashes(sSlash + sSlash);static STAFString sPeriod(kUTF8_PERIOD);static STAFString sColon(kUTF8_COLON);static STAFString sStar(kUTF8_STAR);static STAFString sCR(kUTF8_CR);static STAFString sLF(kUTF8_LF);/*****************************************************************************//* Helper APIs *//*****************************************************************************/static bool isWinNT(){ static STAFMutexSem helperSem; static bool haveChecked = false; static bool rc = false; if (!haveChecked) { STAFMutexSemLock lock(helperSem); if (!haveChecked) { OSVERSIONINFO osVersionInfo = { sizeof(OSVERSIONINFO), 0 }; if ((GetVersionEx(&osVersionInfo) == TRUE) && (osVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)) { rc = true; } haveChecked = true; } } return rc;}struct STAFSortEnumByName{ STAFSortEnumByName(STAFFSCaseSensitive_t caseSensitive) : fCaseSensitive(caseSensitive) { /* Do nothing */ } bool operator()(STAFFSEntry_t lhs, STAFFSEntry_t rhs) { unsigned int comp = 0; if (fCaseSensitive == kSTAFFSCaseSensitive) { STAFStringCompareTo(lhs->pathString.getImpl(), rhs->pathString.getImpl(), &comp, 0); } else { STAFStringCompareTo(lhs->pathString.toUpperCase().getImpl(), rhs->pathString.toUpperCase().getImpl(), &comp, 0); } return (comp == 1); } STAFFSCaseSensitive_t fCaseSensitive;};static bool sortEnumBySize(STAFFSEntry_t lhs, STAFFSEntry_t rhs){ if (lhs->highSize < rhs->highSize) return true; if (lhs->highSize > rhs->highSize) return true; return (lhs->lowSize < rhs->lowSize);}static bool sortEnumByModTime(STAFFSEntry_t lhs, STAFFSEntry_t rhs){ return (STAFTimestamp(lhs->modTime) < STAFTimestamp(rhs->modTime));}// Remove any trailing slashes (that are not also leading slashes) from a path// and return the updated string.static STAFString removeTrailingSlashes(const STAFString &path){ STAFString newPath = path; if (newPath.findFirstNotOf(sBothSlash, STAFString::kChar) != STAFString::kNPos) { unsigned int lastNonSlashLoc = newPath.findLastNotOf(sBothSlash, STAFString::kChar); if (lastNonSlashLoc + 1 != newPath.length()) { newPath = newPath.subString(0, lastNonSlashLoc + 1, STAFString::kChar); } } return newPath;}/*****************************************************************************//* General File System APIs *//*****************************************************************************/STAFRC_t STAFFSInfo(void *info, STAFFSInfoType_t infoType){ if (info == 0) return kSTAFInvalidParm; STAFRC_t retCode = kSTAFOk; try { switch (infoType) { case kSTAFFSPathSep: { *reinterpret_cast<STAFString_t *>(info) = STAFString(kUTF8_SCOLON).adoptImpl(); break; } case kSTAFFSFileSep: { *reinterpret_cast<STAFString_t *>(info) = STAFString(kUTF8_BSLASH).adoptImpl(); break; } case kSTAFFSLineSep: { *reinterpret_cast<STAFString_t *>(info) = STAFString(sCR + sLF).adoptImpl(); break; } case kSTAFFSCaseSensitivity: { *reinterpret_cast<STAFFSCaseSensitive_t *>(info) = kSTAFFSCaseInsensitive; break; } default: { retCode = kSTAFInvalidParm; } } } catch (...) { retCode = kSTAFUnknownError; } return retCode;}/*****************************************************************************//* Path APIs *//*****************************************************************************/STAFRC_t STAFFSAssemblePath(STAFString_t *path, STAFStringConst_t root, unsigned int numDirs, STAFStringConst_t *dirs, STAFStringConst_t name, STAFStringConst_t extension){ if (path == 0) return kSTAFInvalidParm; STAFRC_t retCode = kSTAFOk; try { STAFString theRoot(root); STAFString theName(name); STAFString theExtension(extension); STAFString result; // If a root was specified add it if (theRoot.length() != 0) result += theRoot; // Add the directories if (numDirs != 0) { // Add a backslash if we need it if ((result.length() != 0) && (result.subString(result.length(STAFString::kChar) - 1, STAFString::kRemainder, STAFString::kChar) != sBackSlash)) { result += sBackSlash; } // Tack on each directory and a backslash, except the last one for (unsigned int i = 0; i < (numDirs - 1); ++i) { result += dirs[i]; result += sBackSlash; } // Add the last directory without a backslash result += dirs[numDirs - 1]; } if ((theName.length() != 0) || (theExtension.length() != 0)) { // Add a backslash if we need it if ((result.length() != 0) && (result.subString(result.length(STAFString::kChar) - 1, STAFString::kRemainder, STAFString::kChar) != sBackSlash)) { result += sBackSlash; } // Add the name if (theName.length() != 0) result += theName; // Add the extension if (theExtension.length() != 0) { result += sPeriod; result += theExtension; } } *path = result.adoptImpl(); } catch (...) { retCode = kSTAFUnknownError; } return retCode;}STAFRC_t STAFFSDisassemblePath(STAFStringConst_t path, STAFString_t *root, unsigned int *numDirs, STAFString_t **dirs, STAFString_t *name, STAFString_t *extension){ if (path == 0) return kSTAFInvalidParm; STAFRC_t retCode = kSTAFOk; try { STAFString thePath(path); // See if they have a root slash present. Remove it, if present. STAFString theRoot; if (thePath.subString(1, 1, STAFString::kChar) == sColon) { // Check if the path begins with a drive letter and a colon // If so, assign the root to be <Drive>: theRoot = thePath.subString(0, 2, STAFString::kChar); thePath = thePath.subString(thePath.findFirstNotOf(sBothSlash, 2, STAFString::kChar)); } else if (thePath.subString(0, 2, STAFString::kChar) == s2BackSlashes || thePath.subString(0, 2, STAFString::kChar) == s2Slashes) { // Check if the path starts with a share name using format: // \\computername\sharename // or // //computername/sharename // If so, assign the root to be \\computername\sharename unsigned int nextSlashPos = thePath.findFirstOf( sBothSlash, 2, STAFString::kChar); if ((nextSlashPos != STAFString::kNPos) && (nextSlashPos >= 3)) { // Has format \\...\ unsigned int startShareNamePos = thePath.findFirstNotOf( sBothSlash, nextSlashPos + 1, STAFString::kChar); if (startShareNamePos != STAFString::kNPos) { unsigned int endShareNamePos = thePath.findFirstOf( sBothSlash, startShareNamePos, STAFString::kChar); if (endShareNamePos != STAFString::kNPos) { // Has format \\...\...\[...] theRoot = thePath.subString(0, endShareNamePos); thePath = thePath.subString(endShareNamePos + 1); } else { // Has format \\...\... theRoot = thePath; thePath = ""; } } } } if (root) *root = theRoot.adoptImpl(); // Remove any trailing slashes (that are not also leading slashes) thePath = removeTrailingSlashes(thePath); // Now, find all the directories std::deque<STAFString> theDirs; for (unsigned int slashLoc = thePath.findFirstOf(sBothSlash); slashLoc != STAFString::kNPos; slashLoc = thePath.findFirstOf(sBothSlash)) { theDirs.push_back(thePath.subString(0, slashLoc)); thePath = thePath.subString(thePath.findFirstNotOf(sBothSlash, slashLoc)); } if (numDirs) *numDirs = theDirs.size(); if (dirs) { if (theDirs.size() == 0) *dirs = 0; else *dirs = new STAFString_t[theDirs.size()]; for (unsigned int i = 0; i < theDirs.size(); ++i) (*dirs)[i] = theDirs[i].adoptImpl(); } // If the name is "." or ".." then leave it alone, otherwise // separate the name and the extension if ((thePath == sPeriod) || (thePath == (sPeriod + sPeriod))) { if (name) *name = thePath.adoptImpl(); if (extension) *extension = STAFString().adoptImpl(); } else { // Next find and remove the extension STAFString theExtension; unsigned int extLoc = thePath.findLastOf(sPeriod); // Note: If find a period and it is the last character // (e.g. grannyapple.), keep the period as part of the name if ((extLoc != STAFString::kNPos) && (extLoc != (thePath.length() - 1))) { theExtension = thePath.subString(extLoc + thePath.sizeOfChar(extLoc)); thePath = thePath.subString(0, extLoc); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -