⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gfile.cpp

📁 一个非常有用的开源代码
💻 CPP
字号:
/*	Copyright (C) 2006, Mike Gashler	This library is free software; you can redistribute it and/or	modify it under the terms of the GNU Lesser General Public	License as published by the Free Software Foundation; either	version 2.1 of the License, or (at your option) any later version.	see http://www.gnu.org/copyleft/lesser.html*/#include "GFile.h"#ifdef WIN32#include <shlobj.h> // to get users' application data dir#include <sys/utime.h> // utime#include <direct.h>#include <io.h>#else#include <unistd.h>#include <utime.h> // utime, which sets file times#endif // WIN32#include <stdio.h>#include "GMacros.h"#include <sys/types.h>#include <stdlib.h> // getenvbool GFile::DoesFileExist(const char *szFilename){   return(access(szFilename, 0) == 0);}bool GFile::DoesDirExist(const char *szDir){	char szBuff[256];	char* pCurDir = getcwd(szBuff, 255);	int nVal = chdir(szDir);	chdir(pCurDir);	return(nVal == 0);}bool GFile::MakeDir(char* szDir){	bool bOK = false;	int n;	for(n = 0; ; n++)	{		if(szDir[n] == '/' || szDir[n] == '\\' || szDir[n] == '\0')		{			char cTmp = szDir[n];			szDir[n] = '\0';#ifdef WIN32			bOK = (mkdir(szDir) == 0);#else // WIN32			bOK = (mkdir(szDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0); // read/write/search permissions for owner and group, and with read/search permissions for others#endif // !WIN32			szDir[n] = cTmp;			if(cTmp == '\0')				break;		}	}	return bOK;}// Find the last slash and return what's past thatconst char* GFile::ClipPath(const char* szBuff){	int n = 0;	int i = 0;	while(szBuff[i] != 0)	{		if(szBuff[i] == '\\' || szBuff[i] == '/')			n = i + 1;		i++;	}	return(szBuff + n);}// Find the last slash and set it to '\0'char* GFile::ClipFilename(char* szBuff){	int n = -1;	int i = 0;	while(szBuff[i] != 0)	{		if(szBuff[i] == '\\' || szBuff[i] == '/')			n = i;		i++;	}   if(n > -1)      szBuff[n + 1] = '\0';	return szBuff;}bool GFile::CpyFile(const char* szSrcPath, const char* szDestPath){	FILE* pSrc = fopen(szSrcPath, "rb");	if(!pSrc)		return false;	FILE* pDest = fopen(szDestPath, "wb");	if(!pDest)	{		fclose(pSrc);		return false;	}	char szBuf[1024];	int nFileSize = filelength(fileno(pSrc));	int nChunkSize;	while(nFileSize > 0)	{		nChunkSize = MIN(nFileSize, 1024);		fread(szBuf, nChunkSize, 1, pSrc);		fwrite(szBuf, nChunkSize, 1, pDest);		nFileSize -= nChunkSize;	}	fclose(pSrc);	fclose(pDest);	return true;}bool GFile::GetGoodStorageDirectory(char *toHere){	char *szReturnValue = NULL;	GAssert(toHere != NULL, "we must be passed an already instantiated char *!");	toHere[0] = '\0';#ifdef WIN32TCHAR szPath[MAX_PATH];if(SUCCEEDED(SHGetFolderPath(NULL,                              CSIDL_LOCAL_APPDATA,                              NULL,                              0,                              szPath))) {  printf( "\nWindows System App Directory:   %s", szPath ); 	strcpy(toHere, szPath);	return true;}#endif // now Linux/Mac OS#ifndef __linux__// Mac OSprintf("Mac OS is not set up for directory structure, but using the Linux one's...\n");/* TODOR * #include <CoreFoundation/CoreFoundation.h>#include <ApplicationServices/ApplicationServices.h>CFStringRef shortUserName;CFNumberRef userUID;CFBooleanRef userIsActive;CFBooleanRef loginCompleted;int MyCGGetSessionInfo{	CFDictionaryRef sessionInfoDict;	sessionInfoDict = CGSessionCopyCurrentDictionary();	if (sessionInfoDict == NULL)		Getting Login Session Information 15			2005-07-07 |  2003, 2005 Apple Computer, Inc. All Rights Reserved.			Supporting Fast User Switching			{				printf(?Unable to get session dictionary.?);				return(1);			}	shortUserName = CFDictionaryGetValue(sessionInfoDict,			kCGSessionUserNameKey);	userUID = CFDictionaryGetValue(sessionInfoDict,			kCGSessionUserIDKey);	userIsActive = CFDictionaryGetValue(sessionInfoDict,			kCGSessionOnConsoleKey);	loginCompleted = CFDictionaryGetValue(sessionInfoDict,			kCGSessionLoginDoneKey);}*/#endif  szReturnValue = getenv ("HOME");  if(!szReturnValue)	  szReturnValue = getenv ("HOMEPATH");  if(!szReturnValue)	  szReturnValue = getenv ("USERPROFILE");	  strcpy(toHere, szReturnValue);    return szReturnValue != NULL; // if we were successful this will have been set to something...//todor	}/*static*/ char* GFile::LoadFileToBuffer(const char* szFilename, int* pnSize){	// Load the script	FileHolder fh(fopen(szFilename, "rb"));	FILE* pFile = fh.Get();	if(!pFile)		return NULL;	int nFileSize = filelength(fileno(pFile));	*pnSize = nFileSize;	ArrayHolder<char*> hBuffer(new char[nFileSize + 1]);	GAssert(hBuffer.Get(), "out of memory");	int nBytesRead = fread(hBuffer.Get(), sizeof(char), nFileSize, pFile);	int err = ferror(pFile);	if(err != 0 || nBytesRead != nFileSize)	{		GAssert(false, "Error reading file");		return NULL;	}	hBuffer.Get()[nFileSize] = '\0'; // null terminate the string that represents the file, now copied to the memory in hBuffer [?] [rdp]	return hBuffer.Drop();}/*static */ bool GFile::ResetFileToBlank(const char* szFilename){	FileHolder *fh = new FileHolder(fopen(szFilename, "wb")); // will close it on the fly...argh!	return fh->Close();}/*static*/ bool GFile::SaveBufferToFile(unsigned char* pBuf, int nSize, const char* szFilename){	FileHolder fh(fopen(szFilename, "wb"));	FILE* pFile = fh.Get();	if(!pFile)		return false;	if(fwrite(pBuf, nSize, 1, pFile) != 1)		return false;	return true;}void GFile::CondensePath(char* szPath){
	int n;
#ifdef WIN32
	for(n = 0; szPath[n] != '\0'; n++)
	{
		if(szPath[n] == '\\')
			szPath[n] = '/';
	}
#endif // WIN32	int nPrevSlash = -1;	int nPrevPrevSlash = -1;	for(n = 0; szPath[n] != '\0'; n++)	{		if(szPath[n] == '/')		{			nPrevPrevSlash = nPrevSlash;			nPrevSlash = n;			if(strncmp(szPath + n, "/./", 3) == 0)			{				int nDelSize = 2;				int i;				for(i = n + 1; ; i++)				{					szPath[i] = szPath[i + nDelSize];					if(szPath[i] == '\0')						break;				}				n--; // so we'll catch the current slash the next time around the loop -- handle it next time!							}			else if(nPrevPrevSlash >= 0 && strncmp(szPath + n, "/../", 4) == 0)			{				int nDelSize = n - nPrevPrevSlash + 3;				int i;				for(i = nPrevPrevSlash; ; i++)				{					szPath[i] = szPath[i + nDelSize];					if(szPath[i] == '\0')						break;				}				nPrevSlash = -1;				nPrevPrevSlash = -1;				n = -1;			}						}	}}/* static */ time_t GFile::GetSecondsSince1970(const char *filename){	struct stat buffer;	int         status;	status = stat(filename, &buffer);	//todo error check	return buffer.st_mtime; // number of seconds since 0000 UTC 1970... :-)}// todo test this.../*static */ time_t GFile::SetFileSecondsModifiedSince1970ToThis(const char *filename, time_t toThis){	struct stat buffer;	int         status;	status = stat(filename, &buffer);	//now propagate a utimbuf to the answer of the previous, except for modified is different :-)		struct utimbuf setToThis;	setToThis.actime = buffer.st_atime;	setToThis.modtime = toThis;	utime(filename, &setToThis);	//time_t    actime    Access time. 	//time_t    modtime   Modification time. 		return GetSecondsSince1970(filename);// todo use case test: running this should return the same number you passed it to be set as, [then running getseconds the same, and preceded by different]}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -