Custom Common Files and directories operation function Library

Source: Internet
Author: User

The custom common Files and directories operation function library, in the win and Linux platform to do cross-platform processing. (Cross-platform processing can be used as a reference comparison.) In the win directory the symbol can be \ or/, but under Linux can only be/. )

The following is a source file that implements the code for the interface function. Each interface function has a detailed description of the function.

/* Determine if a file or directory exists
* In the operating system, the directory is also a file, if you want to determine whether a directory exists, you should use DirectoryExists,
* To determine if a file exists and is an archive file, you should use Isarchive.
* @ Returns True if the file or directory exists otherwise false
*% file path character length must not exceed MAX_PATH
*/
BOOL Fileutil::fileexists (LPCTSTR sfilepath) {#if PLATFORM = = WINDOWS32 | | PLATFORM = = Windows64dword dwattr = getfileattributes (Sfilepath); if (dwattr = = (DWORD)-1) return False;else return true;# endif#if PLATFORM = = LINUX32 | | PLATFORM = = Linux64struct Stat File;int res = stat (Sfilepath, &file); if ( -1! = Res && S_isreg (file.st_mode)) {RE Turn true;} else return false; #endif}

/* Determine if the file exists and is an archive that can be read directly (such as a custom text file or TXT file)
*% file path character length must not exceed MAX_PATH
*/
BOOL Fileutil::isarchive (LPCTSTR sfilepath) {#if PLATFORM = = WINDOWS32 | | PLATFORM = = Windows64dword dwattr = getfileattributes (Sfilepath); if (dwattr = = (DWORD)-1 | | (Dwattr & file_attribute_archive) = = 0) return false;else return true; #endif # if PLATFORM = = LINUX32 | | PLATFORM = = linux64//simply determine if the file exists struct stat file;int res = stat (Sfilepath, &file); if ( -1! = Res && s_isreg (file. St_mode)) {return true;} else return false; #endif}

/* Determine if the directory exists, the file exists and the file is a directory and returns true otherwise false
*% file path character length must not exceed MAX_PATH
*/
BOOL Fileutil::isdirectory (LPCTSTR sdirpath) {#if PLATFORM = = WINDOWS32 | | PLATFORM = = Windows64dword dwattr = getfileattributes (Sdirpath); if (dwattr = = (DWORD)-1 | | (Dwattr & file_attribute_directory) = = 0) return false;else return true; #endif # if PLATFORM = = LINUX32 | | PLATFORM = = Linux64struct Stat File;int res = stat (Sdirpath, &file); if ( -1! = Res && s_isdir (File.st_mode)) {RET Urn true;} return false; #endif}

/* Get the file or directory name
* C:\abc\123.txt-123.txt
* c:\abc\efg\--EFG
* parameter sdirbuf for storing file name strings
* The parameter dwbuflen is the buffer character (non-byte) length of the Snamebuf parameter, which contains the terminating character that needs to be preserved,
If the Dwbuflen value is 0, the function does not copy the file name to the SNAMEBUF;
If the Dwbuflen value is not 0, the function copies the file name to Snamebuf and writes the Terminator in Snamebuf;
If the buffer is insufficient, only dwBufLen-1 characters are copied and the Terminator is written in Snamebuf.
The @ function returns the length of the character (with the Terminator) required to copy the file name
*/
size_t Fileutil::extractfilename (LPCTSTR sfilepath, LPTSTR snamebuf, size_t dwbuflen) {LPCTSTR SNameStart, sNameEnd = Sfilepath + _tcslen (sfilepath)-1;//skips the directory name after continuous '/' or ' \ ' while (Snameend >= sfilepath && (*snameend = = '/' | | *sn Ameend = = ' \ \ ') {snameend--;} Snamestart = snameend;snameend++;//Location directory name start position while (Snamestart >= sfilepath) {if (*snamestart = = '/' | | *snamestart = = ' \ \ ') break;snamestart--;} snamestart++;//Copy Directory name if (Snamestart < snameend) {size_t Dwnamelen = snameend-snamestart;if (Dwbuflen > 0) {if ( Dwbuflen > Dwnamelen) dwbuflen = Dwnamelen;else dwbuflen--;memcpy (snamebuf, Snamestart, sizeof (*snamestart) * Dwbuflen); Snamebuf[dwbuflen] = 0;} return Dwnamelen;} return 0;}


/* Get the file name part in the path of the files without the file suffix section
* C:\abc.txt to ABC
* parameter snamebuf for storing file name strings
* The parameter dwbuflen is the buffer character (non-byte) length of the Snamebuf parameter, which contains the terminating character that needs to be preserved,
If the Dwbuflen value is 0, the function does not copy the file name to the SNAMEBUF;
If the Dwbuflen value is not 0, the function copies the file name to Snamebuf and writes the Terminator in Snamebuf;
If the buffer is insufficient, only dwBufLen-1 characters are copied and the Terminator is written in Snamebuf.
The @ function returns the length of the character (with the Terminator) required to copy the file name
*/
size_t fileutil::extractfilenameonly (LPCTSTR sfilename, LPTSTR snamebuf, size_t dwbuflen) {//File name is empty then return 0if directly (!* sFileName) {if (Dwbuflen > 0) snamebuf[0] = 0;return 0;} LPCTSTR Snamestart, snameend = sFileName + _tcslen (sfilename)-1;//If the file is a directory if (*snameend = = '/' | | *snameend = = ' \ ') {// Skip directory name after continuous '/' or ' \ ' while (Snameend >= sfilename && (*snameend = = '/' | | *snameend = ' \ \ ')) {snameend--;} snameend++;} Else{lpctstr sptr = snameend;//Find the starting position of the file suffix while (sptr >= sfilename) {if (*sptr = = '. ') {snameend = Sptr;break;} if (*sptr = = '/' | | *sptr = = ' \ \ ') Break;}}  Snamestart = snameend-1;//Location directory name start position while (Snamestart >= sfilename) {if (*snamestart = = '/' | | *snamestart = = ' \ \ ') ) break;snamestart--;} snamestart++;//Copy Directory name if (Snamestart < snameend) {size_t Dwnamelen = snameend-snamestart;if (Dwbuflen > 0) {if ( Dwbuflen > Dwnamelen) dwbuflen = Dwnamelen;else dwbuflen--;memcpy (snamebuf, Snamestart, sizeof (*snamestart) * Dwbuflen); Snamebuf[dwbuflen] = 0;} Return DwNAmelen;} return 0;}

/* Get the file suffix part of the filename or file path
* Abc.txt---txt
* The return value contains the suffix symbol '. '
*/
LPCTSTR Fileutil::extractfileext (LPCTSTR sfilename) {LPCTSTR Sresult = Null;while (*sfilename) {if (*sFileName = = '. ') Sresult = sfilename;sfilename++;} return sresult;}

/* Get the directory path where the files are located
* C:\abc\efg\123.txt-c:\abc\efg\
* C:\abc\efg\-C:\abc\
* parameter sdirbuf for storing directory strings
* The parameter dwbuflen is the buffer character (non-byte) length of the Sdirname parameter, which contains the terminating character that needs to be preserved,
If the Dwbuflen value is 0, the function does not copy the directory path to the SDIRBUF;
If the Dwbuflen value is not 0, the function copies the directory path to Sdirbuf and writes the Terminator in Sdirbuf;
If the buffer is insufficient, only dwBufLen-1 characters are copied and the Terminator is written in Sdirbuf.
The @ function returns the length of the character required to copy the directory path (with a terminator)
*/
size_t fileutil::extractfiledirectory (LPCTSTR sfilepath, LPTSTR sdirbuf, size_t dwbuflen) {LPCTSTR SDirEnd = SFilePath + _ Tcslen (Sfilepath)-1;while (sdirend >= sfilepath && *sdirend! = '/' && *sdirend! = ' \ \ ') {sdirend--;} if (Sdirend > Sfilepath) {size_t Dwnamelen = sdirend-sfilepath;if (Dwbuflen > 0) {if (Dwbuflen > Dwnamelen) Dwbuflen = Dwnamelen;else dwbuflen--;memcpy (sdirbuf, Sfilepath, sizeof (*SDIRBUF) * Dwbuflen); Sdirbuf[dwbuflen] = 0;} return Dwnamelen;} return 0;}


/* Get top-level directory name
* (abc\efg\ to ABC)
* parameter ppchilddirpath the directory path after the top-level directory, parameters can be empty
* parameter sdirname for storing directory strings
* The parameter dwbuflen is the buffer character (non-byte) length of the Sdirname parameter, which contains the terminating character that needs to be preserved,
If the Dwbuflen value is 0, the function does not copy the directory name to the Sdirname;
If the Dwbuflen value is not 0, the function copies the directory name to the Sdirname and writes the Terminator to the Sdirname;
If the buffer is insufficient, only dwBufLen-1 characters are copied and the Terminator is written in Sdirname.
The @ function returns the length of the character (with the Terminator) required to copy the directory name
*/
size_t Fileutil::extracttopdirectoryname (LPCTSTR Sdirpath, out LPCTSTR *ppchilddirpath, LPTSTR sDirName, SIZE_T Dwbuflen) {LPCTSTR snameend;//skips the directory name before sequential '/' or ' \ ' while (*sdirpath && (*sdirpath = = '/' | | *sdirpath = ' \ \ ')) {SDIRP ath++;} Snameend = sdirpath;//Location directory name start position while (*snameend) {if (*snameend = = '/' | | *snameend = = ' \ \ ') break;snameend++;} Copy directory name if (Snameend > Sdirpath) {size_t Dwnamelen = snameend-sdirpath;if (Dwbuflen > 0) {if (Dwbuflen > DwN Amelen) Dwbuflen = Dwnamelen;else dwbuflen--;memcpy (sdirname, Sdirpath, sizeof (*sdirpath) * Dwbuflen); sDirName[ Dwbuflen] = 0;if (ppchilddirpath) *ppchilddirpath = snameend;} return Dwnamelen;} return 0;}


/* Create a directory by layer
* If the directory C:\a\b\c\d is created, the parent directory of the final directory does not exist and the parent directory is created and the final directory is created
* The function returns True if the directory is completely created, otherwise false is returned.
*% if the creation of a parent directory succeeds and the subdirectory fails, the function returns false and the parent directory that has been created is not deleted.
*% Total character length of directory path, function requirement must be within MAX_PATH character length
*/
BOOL Fileutil::D eepcreatedirectory (LPCTSTR sdirpath) {TCHAR spath[4096]; LPTSTR spathptr = spath; size_t Dwnamelen, Dwbuflen = Arraycount (spath)-1;dword Dwattr;while (true) {Dwnamelen = Extracttopdirectoryname ( Sdirpath, &sdirpath, Spathptr, Dwbuflen);//Get the top-level directory name * (abc\efg\-to-ABC)//Discard if the directory name is longer than the directory buffer length (Dwnamelen > = Dwbuflen) return false;//If the directory name length of 0 means that all directories have been created if (Dwnamelen = = 0) return true;spathptr + = Dwnamelen; #if PLATFORM = = WIN DOWS32 | | PLATFORM = = windows64//If directory name is not a drive name check and create directory if (spathptr[-1]! = ': ') {//create this directory if the directory does not exist dwattr = GetFileAttributes (spath) if ((dwattr = = (DWORD)-1 && getlasterror () = = Error_file_not_found)) {if (! CreateDirectory (spath, NULL)) return false;} If the file exists and the file is not a directory, return Falseelse if (! ( Dwattr & File_attribute_directory) {return false;}} #endif # if PLATFORM = = LINUX32 | | PLATFORM = = linux64struct Stat filestat;if ( -1 = = Stat (spath, &filestat))//No this file or directory {if (ENOENT = = errno)//error code is ENOENT: No such file or directory {if ( -1 = = mkdir (spatH, S_IRWXG))//Create a level directory {return false;}} #endifsPathPtr [0] = '/'; Spathptr++;if (Dwbuflen > Dwnamelen) Dwbuflen-= Dwnamelen + 1;else dwbuflen = 0;} return false;}

Some of these custom macro definitions and types are defined as follows:

Custom macros for cross-platform reasons (when used without cross-platform requirements, you can delete unwanted parts)

#define _tcslen (str) strlen (str)

typedef const CHAR *LPCTSTR, *LPCSTR;

typedef size_t SIZE_T;

Other macros

Get array length

#define Arraycount (a)(sizeof (a)/sizeof ((a) [0])

Custom Common Files and directories operation function Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.