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

📄 filesystem.cpp

📁 JK Proxy Project - Version 0.1 ------------------------------ This was going to be a proxy serve
💻 CPP
字号:
/*
+-----------------------------------------------------------------------------+
|                                                                             |
|   filesystem.cpp                                                            |
|   Last change: yyyy-mm-dd                                                   |
|                                                                             |
|   Author: Jan Krumsiek                                                      |
|   eMail:  proxy@krumsiek.com                                                |
|   Web:    http://www.krumsiek.com/proxy                                     |
|                                                                             |
|   Copyright 2003 - Jan Krumsiek                                             |
|   This source code can freely be modified and redistributed. No liability   |
|   whatsoever is taken by the author for any use of this software.           |
|                                                                             |
|   Description:                                                              |
|   This module contains functions for file access. Most of this code is      |
|   OS-specific and written within #ifdef...#endif directives.                |             
|                                                                             |
+-----------------------------------------------------------------------------+
*/

#include "proxymain.h"
#include "filesystem.h"
#include "time.h"
#include "fstream.h"

#ifdef WIN32
	// include windows header
	#include "windows.h"
#endif

UINT GetFileLength(CHARPTR path)
// returns the size of a file
{
#ifdef WIN32
	// use WIN32 GetFileSize function

	// open file
	HANDLE file = CreateFile(path , 
						GENERIC_READ, 0, 0, OPEN_EXISTING,0 , 0);
	UINT ret = GetFileSize(file, NULL);
	CloseHandle(file);
	return ret;

#endif
}

void KillFile(CHARPTR file)
// deletes a file
{
#ifdef WIN32
	BOOL val = DeleteFile(file);
#endif
}



int FileExists(CHARPTR name)
// checks if a file exits
{
#ifdef WIN32
	// use WIN32 CreateFile() to check if file can be opened
	HANDLE check = CreateFile(name,GENERIC_READ, 0, 0, OPEN_EXISTING, 0,0);
	CloseHandle(check);

	return (check == (void*)0xFFFFFFFF) ? false : true;

#endif
}



void WriteToFile(CHARPTR filepath, CHARPTR data)
// opens or creates a file and writes 'data' into it
// 'data' must be zero-terminated
{
	/* old code - line breaks didn't work
	// open file
	FILE* hfile = fopen(filepath,"w+");
	// write to file
	fputs(data, hfile);
	int i = strlen(data);
	// close file
	fclose(hfile);
	*/
	ofstream file (filepath, ios::out | ios::binary);
    
	file.write(data,strlen(data));
	file.close();




}

void CreateDir(CHARPTR name)
// creates a directory
{
#ifdef WIN32
	// use win32 CreateDirectory function
	CreateDirectory(name,0);
#endif
}


void GetFileCont(CHARPTR filename, CHARPTR* cont, UINT* length)
// this function reads the contents of 'filename' and saves a
// new pointer in *cont and file length is saved in *length
{
	FILE* open=0;

	// Get file length
	*length = GetFileLength(filename) ;  // [DBG]

	// Open file
	open = fopen(filename,"r");

	// If open == 0 then failed to open file
	if (open == 0)
	{
		*cont = 0;
	}
	else
	{

		// Allocate memory for file contents
		*cont = new char[int(*length)];
		// pre-set with zeros
		memset(*cont,0,int(*length));

		// Read from file
		size_t test = fread(*cont,*length,1,open);

		// Close file
		fclose(open);
	}
}

#ifdef WIN32

CHARPTR GetAppDir()
// returns application directory (directory where EXE file is located)
// WIN32 only
{
	char appexe[256];
	UINT len = GetModuleFileName(GetModuleHandle(NULL),appexe,256);
	// set zero terminator
	appexe[len] = 0;
	// extract path
	return ExtractDir(appexe);
}

#endif




FILELIST* GetDirContents(CHARPTR dir)
// returns all files of a directory with full path specification in a
// FILELIST struct
{

	CHARPTR dirw=0,dirs=0;
	UINT len = strlen(dir);
	FILELIST* ret = new FILELIST;

	// add slash if necessary and wildcard
	if ((dir[len-1] == '/') || (dir[len-1] == '\\'))
	{
		// only add wildcard
		dirw = ConnectStrings(dir,"*.*\0");
		// only copy string
		dirs = (CHARPTR)CopyAndPtr(dir,strlen(dir));
	}
	else
	{
		// add slash and wildcard
		dirw = ConnectStrings(dir,"/*.*\0");
		// add slash
		dirs = ConnectStrings(dir,"/\0");
	}



#ifdef WIN32
	UINT count=0;

		// *** WIN32 implementation start
	WIN32_FIND_DATA find;

	// now we need to iterate through all files to count the number of file
	HANDLE search = FindFirstFile(dirw,&find);
		
	while (true)
	{	
		// only increase counter if not "." or ".." (check for single
		// period at the beginning of file string) and if file is no directory
		if ((find.cFileName[0] != '.') && ((find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0))
			count++;

		

		if (FindNextFile(search,&find) == 0) break;
	}
	FindClose(search);

	// now reserve enough memory to contain all pointers
	ret->inum = count;
	ret->files = new CHARPTR[count];


	// iterate through a files again and save filenames
	search = FindFirstFile(dirw,&find);
	// initialize iterator
	UINT i=0;
	while (true)
	{	
		// only save in array if not "." or ".." (check for single
		// period at the beginning of file string
		if ((find.cFileName[0] != '.') && ((find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0))
		{
			// connect filename and directory and save in struct
			ret->files[i] = ConnectStrings(dirs,find.cFileName);
			// increase iterator
			i++;

		}

		if (FindNextFile(search,&find) == 0) break;
	}
	FindClose(search);

	delete dirw;
	delete dirs;

	// return pointer to structure
	return ret;
#endif

}

int DeleteFileList(FILELIST* files)
// frees all memory allocated for a FILELIST struct
{
	// delete all contained strings
	for (int i=1; i <= files->inum; i++)
		delete files->files[i-1];

	// delete struct as such
	delete files;

	return 0;
}

tm GetFileLWTime(CHARPTR file)
// returns last write time of a file in standard tm format
{
#ifdef WIN32
	HANDLE hfile=0;
	tm ret;
	FILETIME lw;
	SYSTEMTIME lwconv;

	// open file
	hfile = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, 
                 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0);

	// obtain date of last write
	GetFileTime(hfile,NULL,NULL,&lw);
	// convert to windows system time struct
	FileTimeToSystemTime(&lw,&lwconv);
	// and assign all values to tm struct
	ret.tm_year		= lwconv.wYear - 1900;
	ret.tm_mon		= lwconv.wMonth-1;
	ret.tm_mday		= lwconv.wDay;
	ret.tm_hour		= lwconv.wHour;
	ret.tm_min		= lwconv.wMinute;	
	ret.tm_sec		= lwconv.wSecond;
	ret.tm_wday		= lwconv.wDayOfWeek;
	ret.tm_yday		= 0;
	ret.tm_isdst	= 0;


	// close file
	CloseHandle(hfile);
	


	return ret;
#endif
}



⌨️ 快捷键说明

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