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

📄 eccfile.cpp

📁 ECC (椭圆曲线加密算法) 的源代码c++builder,很难得到的源代码
💻 CPP
字号:
/* ==========================================================================

	Ecc - Erik's Code Collection
	Copyright (C) 2003 - Erik Dienske

	This file is part of Ecc.

	Ecc is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Ecc is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Ecc; if not, write to the Free Software Foundation, Inc.,
	59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	
===========================================================================*/

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "EccFile.h"
#include "EccString.h"
#include "EccError.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
#include <Registry.hpp>	// CreateFileAssociation()
#include "shlobj.h"		// SHChangeNotify()
/*	#include "shlobj.h" Requires conditional define NO_WIN32_LEAN_AND_MEAN */
#include <fstream>
#include <StrUtils.hpp>	// AnsiReplaceStr
//---------------------------------------------------------------------------

//===========================================================================
namespace ecc {
//===========================================================================

//---------------------------------------------------------------------------

bool MakeFileList(String path, TStringList* list, const bool include_path)
{
	HANDLE fhandle;
	String fitem;
	WIN32_FIND_DATA filedata;
	bool done = false;

	/* Get the first item: */
	path = IncludeTrailingBackslash(path);
	fhandle = FindFirstFile( String(path + "*").c_str(), &filedata);
	if (fhandle == INVALID_HANDLE_VALUE)
		return false;

	while (!done)
	{
		fitem = String(filedata.cFileName);
		/* Check if the filedata-object is a directory or a file: */
		if (!(filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		{
			/* Object is a file: */
			if (include_path)
				list->Append(path + fitem);
			else
				list->Append(fitem);
		}
		/* Is it the end? */
		if (!FindNextFile(fhandle, &filedata))
		{
			if (GetLastError() == ERROR_NO_MORE_FILES)
				done = true;
		}
	}

	FindClose(fhandle);
	return true;
}
//---------------------------------------------------------------------------

bool MakeDirList(String path, TStringList* list)
{
	HANDLE fhandle;
	String fitem;
	WIN32_FIND_DATA filedata;
	bool done = false;

	/* Get the first item: */
	path = IncludeTrailingBackslash(path);
	fhandle = FindFirstFile( String(path + "*").c_str(), &filedata);
	if (fhandle == INVALID_HANDLE_VALUE)
		return false;

	while (!done)
	{
		fitem = String(filedata.cFileName);
		/* Check if the filedata-object is a directory or a file: */
		if (filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			/* Object is a directory: */
			if ((fitem != ".") && (fitem != ".."))
			{
				list->Append(path + fitem);
				//ScanDir(path + fitem, list); // Recurse.
			}
		}
		/* Is it the end? */
		if (!FindNextFile(fhandle, &filedata))
		{
			if (GetLastError() == ERROR_NO_MORE_FILES)
				done = true;
		}
	}
	FindClose(fhandle);
	return true;
}
//---------------------------------------------------------------------------

bool MakeRelativeDirList(String path, TStringList* list)
{
	HANDLE fhandle;
	String fitem;
	WIN32_FIND_DATA filedata;
	bool done = false;

	/* Get the first item: */
	path = IncludeTrailingBackslash(path);
	fhandle = FindFirstFile( String(path + "*").c_str(), &filedata);
	if (fhandle == INVALID_HANDLE_VALUE)
		return false;

	while (!done)
	{
		fitem = String(filedata.cFileName);
		/* Check if the filedata-object is a directory or a file: */
		if (filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			/* Object is a directory: */
			if ((fitem != ".") && (fitem != ".."))
			{
				list->Append(fitem);
				//ScanDir(path + fitem, list); // Recurse.
			}
		}
		/* Is it the end? */
		if (!FindNextFile(fhandle, &filedata))
		{
			if (GetLastError() == ERROR_NO_MORE_FILES)
				done = true;
		}
	}

	FindClose(fhandle);
	return true;
}
//---------------------------------------------------------------------------

bool StringToFile(String const str, String const fname)
{
	bool error = false;
	TStringList *sl_text = new TStringList();

	try
	{
		sl_text->Text = str;
		sl_text->SaveToFile(fname);
	}
	catch(Exception &err)
	{
		ShowMessage("Could not write to file:\n'" + fname + "'\n\nError:\n" + err.Message);
		error = true;
	}

	delete sl_text;

	return error;
}
//---------------------------------------------------------------------------

String FileToString(String const fname)
{
	if (!FileExists(fname)) return "";

	String str;
	TStringList *sl_text = new TStringList();
	try
	{
		sl_text->LoadFromFile(fname);
		str = sl_text->Text;
	}
	__finally
	{
		delete sl_text;
	}
	return str;
}
//---------------------------------------------------------------------------

String ExtractFileNameNoExt(String fname)
{
	fname = ExtractFileName(fname);
	int ext_pos = fname.Pos(ExtractFileExt(fname)) -1;
	if (ext_pos > 0) fname = fname.SubString(1,  ext_pos);
	return fname;
}
//---------------------------------------------------------------------------

String ReplaceIllegalFileNameChars(String fname, const String replace)
{
	String ill_chars = "\\/:*?\"<>|";
	if (replace.LastDelimiter(ill_chars))
		ECC_THROW("'replace' cannot contain illegal chars.");

	int pos;
	while (1)
	{
		pos = fname.LastDelimiter(ill_chars);
		if (!pos) break;
		if (replace.IsEmpty()) fname.Delete(pos, 1);
		else fname = AnsiReplaceStr(fname, fname[pos], replace);
	}
	return fname;
}
//---------------------------------------------------------------------------

long GetFileSize(const String fname)
{
	HANDLE fhandle;
	WIN32_FIND_DATA filedata;

	fhandle = FindFirstFile(fname.c_str(), &filedata);
	if (fhandle == INVALID_HANDLE_VALUE)
		return -1;

	long size = (filedata.nFileSizeHigh * MAXDWORD) + filedata.nFileSizeLow;

	FindClose(fhandle);
	return size;
}
//---------------------------------------------------------------------------

String FloatToHumanFileSize(float fsize)
{
	if (fsize < 0) return "?";

	if (fsize < 10000)
		return FloatToStr(fsize) + " B";

	fsize = fsize/1024;
	if (fsize < 10000)
		return (FloatToStrF(fsize, ffFixed, 7, 0) + " kB");

	fsize = fsize/1024;
	if (fsize < 10000)
		return (FloatToStrF(fsize, ffFixed, 7, 1) + " MB");

	fsize = fsize/1024;
	return (FloatToStrF(fsize, ffFixed, 7, 1) + " GB");
}
//---------------------------------------------------------------------------

String FileAttributeStr(const String fname)
{
	HANDLE fhandle;
	WIN32_FIND_DATA filedata;

	fhandle = FindFirstFile(fname.c_str(), &filedata);
	if (fhandle == INVALID_HANDLE_VALUE)
		return "";
	if ((filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		return "";

	String str;
	str += (filedata.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)	? "A" : "";
	str += (filedata.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)	? "H" : "";
	str += (filedata.dwFileAttributes & FILE_ATTRIBUTE_READONLY)? "R" : "";
	str += (filedata.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)	? "S" : "";

	FindClose(fhandle);
	return str;

/* 	int attrs = FileGetAttr(fname);
	if (attrs & faDirectory) return "";

	String str;
	str += (attrs & faArchive)	? "A" : "";
	str += (attrs & faHidden)	? "H" : "";
	str += (attrs & faReadOnly) ? "R" : "";
	str += (attrs & faSysFile) 	? "S" : "";
	return str; */
}
//---------------------------------------------------------------------------

void AddDoubleZeroToString(String &fname)
{
	fname.SetLength(fname.Length() + 1);
	fname[fname.Length()] = '\0';
	fname.SetLength(fname.Length() + 1);
	fname[fname.Length()] = '\0';
}
//---------------------------------------------------------------------------

bool FileToRecycleBin(String fpath)
{
	AddDoubleZeroToString(fpath);

	SHFILEOPSTRUCT op;
	ZeroMemory(&op,sizeof(op));
	//op.hwnd 	= Handle;
	op.hwnd 	= 0;
	op.wFunc 	= FO_DELETE;
	op.pFrom 	= fpath.c_str();
	op.fFlags 	= FOF_ALLOWUNDO;
	return !SHFileOperation(&op);
}
//---------------------------------------------------------------------------

bool MoveFile(String src_fpath, String dest_fpath)
{
   AddDoubleZeroToString(src_fpath);
   AddDoubleZeroToString(dest_fpath);

   SHFILEOPSTRUCT op;
   ZeroMemory(&op,sizeof(op));
   //op.hwnd 	= Handle;
   op.hwnd 		= 0;
   op.wFunc 	= FO_MOVE;
   op.pFrom 	= src_fpath.c_str();
   op.pTo 		= dest_fpath.c_str();
   op.fFlags	= FOF_RENAMEONCOLLISION + FOF_NOCONFIRMATION;
   return !SHFileOperation(&op);
}
//---------------------------------------------------------------------------

TDateTime FILETIMEToTDateTime(const FILETIME ftime)
{
	/* Create a union/struct for DosDateTime: */
	union
	{
		struct
		{
			WORD time;
			WORD date;
		};
		unsigned int fat_dt;
	} u_date;

	/* Convert Coordinated Universal Time (UTC) to a local file time: */
	FILETIME local_ftime;
	TDateTime fdatetime;
	try
	{
		FileTimeToLocalFileTime(
			&ftime,			// lpFileTime, pointer to UTC file time to convert
			&local_ftime);	// lpLocalFileTime, pointer to converted file time
		/* Convert to DosDateTime so BCB can do something with it: */
		FileTimeToDosDateTime(
			&local_ftime,	// lpFileTime, pointer to 64-bit file time
			&u_date.date, 	// lpFatDate, pointer to variable for MS-DOS date
			&u_date.time);	// lpFatTime, pointer to variable for MS-DOS time
	}
	catch (...)
	{
		return TDateTime(1911, 11, 11);
	}

	/* Convert Windows time as a BCB TDateTime: */
	try
	{
		fdatetime = FileDateToDateTime(u_date.fat_dt);
	}
	catch (...)
	{
		fdatetime = TDateTime(1911, 11, 11);
	}
	return fdatetime;
}
//---------------------------------------------------------------------------

bool CreateFileAssociation(String ext, String file_type_str,
		String app_name, String app_path,
		bool click_start)
{
	TRegistry *reg = new TRegistry();
	bool ok = true;

	try
	{
		reg->RootKey = HKEY_CLASSES_ROOT;
		if (reg->OpenKey(ext, true))
		{
			reg->WriteString("", app_name + ext);
			reg->CloseKey();
		}
		if (reg->OpenKey(app_name + ext, true))
		{
			reg->WriteString("", file_type_str);
			reg->CloseKey();
		}
		if (click_start)
		{	/* Clicking opens app_path */
			if (reg->OpenKey(app_name + ext + "\\shell\\open\\command", true))
			{
				reg->WriteString("", "\"" + app_path + "\" \"%1\"");
				reg->CloseKey();
			}
		}
		else
		{	/* Give this file app_name's icon */
			if (reg->OpenKey(app_name + ext + "\\DefaultIcon", true))
			{
				reg->WriteString("", app_path + ",0");
				reg->CloseKey();
			}
		}

		/* Let the shell know file associations have changed: */
		SHChangeNotify(
			SHCNE_ASSOCCHANGED,	// LONG wEventId
			SHCNF_IDLIST,		// UINT uFlags
			NULL, 				// LPCVOID dwItem1
			NULL				// LPCVOID dwItem2
		);
	}
	catch(...)
	{
		ok = false;
	}
	delete reg;
	return ok;
}
//---------------------------------------------------------------------------

void AppendStrToFile(String msg, String fpath, bool include_datetime)
{
	if (fpath.IsEmpty()) return;

	std::ofstream log(fpath.c_str(), std::ios::out|std::ios::app); // Append, and create if necessary.
	if (!log) return;

	if (include_datetime)
		log	<< "[" << Now().DateTimeString().c_str() << "] ";
	log	<< msg.c_str();
	log << std::endl;
}
//---------------------------------------------------------------------------

//===========================================================================
} // namespace ecc;
//===========================================================================

⌨️ 快捷键说明

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