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

📄 amstream.c

📁 JPEG解压软件,包含PC端的测试程序,程序结构比较清晰
💻 C
字号:
/*
 * AMStream.c
 *
 *
 */
#include "AMComDef.h"

#include <stdio.h>
#include <windows.h>
#include "AMPlat.h"
#include "AMMem.h"
#include "AMMemStream.h"
#include "AMStream.h"



#define  FILE_FLAG    1
#define  MEMORY_FLAG  2

HMSTREAM	AMStreamOpenFromFile(MPCChar path, MPCChar name,MWord mode)
{
	LPSTREAMHANDLE hStream;
	MPChar szFile;
	FILE * fs ;
	
	if( MNull == path ||MNull == name)
		return MNull;
	
	szFile = (MPChar)AMMemAlloc(MNull,strlen(path)+strlen(name)+1);
	if(MNull == szFile)
		 return  MNull;
    sprintf(szFile,"%s%s",path,name);

	switch(mode)
	{
		case	STREAM_READ:
			fs	=	fopen(szFile, "rb");
			break;
		case	STREAM_WRITE:
			fs	=	fopen(szFile, "wb");
			break;
		case	STREAM_APPEND:
			fs	=	fopen(szFile, "ab");
			break;
		case	STREAM_R_PLUS:
			fs	=	fopen(szFile, "rb+");
			break;
		case	STREAM_W_PLUS:
			fs	=	fopen(szFile, "wb+");
			break;
		case	STREAM_A_PLUS:
			fs	=	fopen(szFile, "ab+");
			break;
		default:
			fs	=	fopen(szFile, "rb");;
			break;
	}
	if ( MNull == fs)
		return MNull;

	hStream = (LPSTREAMHANDLE)AMMemAlloc(MNull,sizeof(STREAMHANDLE));
	if(MNull == hStream)
		return MNull;
	AMMemSet(hStream,0,sizeof(STREAMHANDLE));
	
	hStream->pty.file_pty.file_path = (MPChar)AMMemAlloc(MNull,strlen(path)+1);
	if(hStream->pty.file_pty.file_path == MNull)
	{
		AMMemFree(MNull,szFile);
		AMMemFree(MNull,hStream);
		return MNull;
	}
	strcpy(hStream->pty.file_pty.file_path,path);

	hStream->pty.file_pty.file_name = (MPChar)AMMemAlloc(MNull,strlen(name)+1);
	if(hStream->pty.file_pty.file_name == MNull)
	{
		AMMemFree(MNull,hStream->pty.file_pty.file_path);
		AMMemFree(MNull,szFile);
		AMMemFree(MNull,hStream);
		return MNull;
	}
	strcpy(hStream->pty.file_pty.file_name,name);

	hStream->handle = fs;
	hStream->flag   = FILE_FLAG;

	AMMemFree(MNull,szFile);
	return hStream;	
}

HMSTREAM    AMStreamOpenFromMem(void* mem,MLong mem_size)
{
	LPSTREAMHANDLE hStream;
	
	if(MNull == mem || mem_size == 0)
		return MNull;
	hStream = (LPSTREAMHANDLE)AMMemAlloc(MNull,sizeof(STREAMHANDLE));
	if(!hStream)
		return 	MNull;
	AMMemSet(hStream,0,sizeof(STREAMHANDLE));
	
	hStream->handle = mem;
	hStream->pty.mem_pty.size  = mem_size;
	hStream->flag  = MEMORY_FLAG;
	hStream->pty.mem_pty.off_set = 0;
		
	return hStream;
}

MBool	AMStreamClose(HMSTREAM stream_handle)
{
	MBool ret = MFalse;
	MLong lTmp;

	LPSTREAMHANDLE hStream = (LPSTREAMHANDLE ) stream_handle;
	if(MNull == hStream || MNull == hStream->handle)
		return MFalse;

	switch(hStream->flag){
		case FILE_FLAG:
			lTmp = fclose(hStream->handle);
			if (MNull != hStream->pty.file_pty.file_path )
				AMMemFree(MNull,hStream->pty.file_pty.file_path);
			if (MNull != hStream->pty.file_pty.file_name )
				AMMemFree(MNull,hStream->pty.file_pty.file_name);

			AMMemFree(MNull,hStream);
			if(0 == lTmp) ret = MTrue;
			break;
		case MEMORY_FLAG:
			ret = AMStreamMemClose(hStream);
			break;
		default:
			break;
	}
	return ret;
}

MPCChar	AMStreamFileGetName(HMSTREAM stream_handle)
{
	LPSTREAMHANDLE hStream = (LPSTREAMHANDLE ) stream_handle;
	if((MNull == hStream) || (hStream->flag != FILE_FLAG))
		return MNull;
		
	return hStream->pty.file_pty.file_name;
}

MPCChar	AMStreamFileGetPath(HMSTREAM stream_handle)
{
	LPSTREAMHANDLE hStream = (LPSTREAMHANDLE ) stream_handle;
	if((MNull == hStream) || (hStream->flag != FILE_FLAG))
		return MNull;
		
	return hStream->pty.file_pty.file_path;
}

MBool	AMStreamIsFileExist(MPCChar path, MPCChar name)
{
	if (MNull==AMStreamOpenFromFile(path,name,STREAM_READ))
		return MFalse;
	return MTrue;
}

HMSTREAM AMStreamFileCreate(MPCChar path, MPCChar name)
{
	LPSTREAMHANDLE hStream;
	MPChar szFileName;
	FILE * fs;

	szFileName = (MPChar)AMMemAlloc(MNull,strlen(path)+strlen(name)+1);
	if(MNull == szFileName)
		return MNull;
	sprintf(szFileName,"%s%s",path,name);

	hStream = (LPSTREAMHANDLE)AMMemAlloc(MNull,sizeof(STREAMHANDLE));
	if(!hStream)
	{
		AMMemFree(MNull,szFileName);
		return  MNull;
	}
	AMMemSet(hStream,0, sizeof(STREAMHANDLE));

	fs = fopen(szFileName,"wb");
	if (fs == MNull)
	{
		AMMemFree(MNull,szFileName);
		AMMemFree(MNull,hStream);
		hStream = MNull;
	}else{
		hStream->pty.file_pty.file_path = (MPChar)AMMemAlloc(MNull,strlen(path)+1);
		if(hStream->pty.file_pty.file_path == MNull)
		{
			AMMemFree(MNull,szFileName);
			AMMemFree(MNull,hStream);
			return MNull;
		}
		strcpy(hStream->pty.file_pty.file_path,path);

		hStream->pty.file_pty.file_name = (MPChar)AMMemAlloc(MNull,strlen(name)+1);
		if(hStream->pty.file_pty.file_name == MNull)
		{
			AMMemFree(MNull,hStream->pty.file_pty.file_path);
			AMMemFree(MNull,szFileName);
			AMMemFree(MNull,hStream);
			return MNull;
		}
		strcpy(hStream->pty.file_pty.file_name,name);
		hStream->handle = fs;
		hStream->flag   = FILE_FLAG;
	}

	AMMemFree(MNull,szFileName);
	return hStream;	
}

MBool	AMStreamFileDestroy(MPCChar path, MPCChar name)
{
	MPChar szFileName;

	if(MNull == path || MNull == name )
		return MFalse;

	szFileName = (MPChar)AMMemAlloc(MNull,strlen(path)+strlen(name)+1);
	if(MNull == szFileName)
		return MFalse;
	sprintf(szFileName,"%s%s",path,name);

	AMMemFree(MNull,szFileName);
	return DeleteFile(szFileName); 
}

MBool	AMStreamFileRename(MPCChar old_file_path, MPCChar old_file_name,MPCChar new_file_path, MPCChar new_file_name)
{
	MPChar szOldFile,szNewFile;

	if((MNull == old_file_path) || (MNull == old_file_name) || (MNull == new_file_path) || (MNull == new_file_name))
		return MFalse;
		
	szOldFile = (MPChar)AMMemAlloc(MNull,strlen(old_file_path)+strlen(old_file_name)+1);
	if(MNull == szOldFile)
		return MFalse;
	sprintf(szOldFile,"%s%s",old_file_path,old_file_name);

	szNewFile = (MPChar)AMMemAlloc(MNull,strlen(new_file_path)+strlen(new_file_name)+1);
	if(MNull == szNewFile)
		return MFalse;
	sprintf(szNewFile,"%s%s",new_file_path,new_file_name);

	AMMemFree(MNull,szOldFile);
	AMMemFree(MNull,szNewFile);
	return MoveFile(szOldFile,szNewFile);

}


MLong	AMStreamFileGetSize(MPCChar file_path, MPCChar file_name)
{
	MLong ret = 0;
	HMSTREAM FileStream;
	if(MNull == file_path || MNull == file_name)
		return 0;
	FileStream = AMStreamOpenFromFile(file_path,file_name,STREAM_READ);
	if(!FileStream)
	{
		ret = AMStreamGetSize(FileStream);
		AMStreamClose(FileStream);
	}
    return  ret;
}

MLong	AMStreamGetSize(HMSTREAM stream_handle)
{
	MLong ret;
	MLong lTmp;
	LPSTREAMHANDLE hStream = (LPSTREAMHANDLE ) stream_handle;
	if(MNull == hStream || MNull == hStream->handle)
		return 0;
	switch(hStream->flag){
		case FILE_FLAG:
				lTmp = ftell(hStream->handle);
				fseek( hStream->handle, 0, SEEK_END );
				ret  = ftell(hStream->handle);
				fseek(hStream->handle,lTmp,SEEK_SET);
			break;
		case MEMORY_FLAG:
			ret = AMStreamMemGetSize(hStream);
			break;
		default :
			ret = 0;
			break;

	}
   return ret;
}

MLong	AMStreamRead(HMSTREAM stream_handle,MPVoid buf,MLong size)
{
	MLong ret;
	LPSTREAMHANDLE hStream = (LPSTREAMHANDLE ) stream_handle;
	if((MNull == hStream) || (MNull == hStream->handle) ||(MNull == buf) || (size == 0))
			return 0;
			
	switch(hStream->flag){
		case  FILE_FLAG:
				ret = fread(buf, 1, size, hStream->handle);
				break;
		case  MEMORY_FLAG:
				ret = AMStreamMemRead(hStream,buf,size);
				break;
		default:
			ret = 0;
			break;
	}
	
	return ret;
}

MLong  AMStreamWrite(HMSTREAM stream_handle,void* buf,MLong size)
{
	MLong ret;
	LPSTREAMHANDLE hStream = (LPSTREAMHANDLE ) stream_handle;
	if((MNull ==hStream) ||(MNull == hStream->handle) || (MNull == buf) || (size == 0))
			return 0;
			
	switch(hStream->flag){
		case FILE_FLAG:
				ret = fwrite(buf, 1, size, hStream->handle);
				break;
		case MEMORY_FLAG:
				ret = AMStreamMemWrite(hStream,buf,size);
			    break;
		default:
				ret = 0;
				break;
	}
	return ret;
}


MBool	AMStreamFlush(HMSTREAM stream_handle)
{
	MBool ret;
	LPSTREAMHANDLE hStream = (LPSTREAMHANDLE ) stream_handle;
	if(MNull == hStream || MNull == hStream->handle)
		return MFalse;

	switch(hStream->flag){
		case FILE_FLAG:
			 if (0==fflush(hStream->handle))
		          ret = MTrue;
			  else
				  ret = MFalse;
			  break;
		case MEMORY_FLAG:
				ret = AMStreamMemFlush(hStream);
				break;
		default:
				ret = MFalse;
				break;
	}
	return ret;
}

MLong	AMStreamSeek(HMSTREAM stream_handle,MShort start, MLong offset)
{
	MLong ret;
	MLong s = 0;

	LPSTREAMHANDLE hStream = (LPSTREAMHANDLE ) stream_handle;
	if(MNull == hStream || MNull == hStream->handle)
		return 0;
		
	switch(hStream->flag){
		case FILE_FLAG:
			switch(start) {
				case STREAM_BEGIN:
					 s = SEEK_SET;
					 break;
				case STREAM_END:
					 s = SEEK_END;
					 break;
				case STREAM_CUR:
					 s = SEEK_CUR;
					 break;
				default:
					 break;
			} 
			ret = fseek(hStream->handle, offset, s);
			break;
		case MEMORY_FLAG:
			ret = AMStreamMemSeek(hStream,start,offset);
			break;
		default:
			ret = 0;
			break;	 
	}
	return ret;
}

MLong	AMStreamTell(HMSTREAM stream_handle)
{
	MLong ret;
	LPSTREAMHANDLE hStream = (LPSTREAMHANDLE ) stream_handle;
	if(MNull == hStream || MNull == hStream->handle)
		return 0;
	switch(hStream->flag){
		case FILE_FLAG :
			ret = ftell(hStream->handle);
			break;
		case MEMORY_FLAG :
			ret = AMStreamMemTell(hStream);
			break;
		default :
			ret = 0;
		   break;
	}
	return ret;
}

⌨️ 快捷键说明

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