[PR]

水無瀬の部屋 > Programming > sample > exe2ico > util.cpp
最終更新日: 2007/03/29

   1: //*********************************************************
   2: // プロジェクト: exe2ico
   3: //  ファイル名: util.cpp
   4: //*********************************************************
   5: #include "util.h"
   6: 
   7: 
   8: //---------------------------------------------------------
   9: // ファイルスコープ関数 の 宣言
  10: //---------------------------------------------------------
  11: static FILE *CreateOutputResourceFile( const char *srcfile, const char *ext, int nID );
  12: 
  13: 
  14: //*********************************************************
  15: // GetResourcePtr
  16: //*********************************************************
  17: void *
  18: GetResourcePtr
  19: 	(
  20: 		      HMODULE  hModule,
  21: 		const char    *name,
  22: 		const char    *type
  23: 	)
  24: {
  25: 	// パラメタの仮定
  26: 	ASSERT( IsValidInstanceHandle( hModule ) );
  27: 	ASSERT( IsValidResourceName( name ) );
  28: 	ASSERT( IsValidResourceName( type ) );
  29: 
  30: 	HRSRC hResource = FindResource( hModule, name, type );
  31: 	if ( ! hResource )
  32: 		return null;
  33: 
  34: 	HGLOBAL hmemResource = LoadResource( hModule, hResource );
  35: 	if ( ! hmemResource )
  36: 		return null;
  37: 
  38: 	return LockResource( hmemResource );
  39: }//GetResourcePtr
  40: 
  41: //*********************************************************
  42: // OutputResourceFile
  43: //*********************************************************
  44: bool
  45: OutputResourceFile
  46: 	(
  47: 		const void  *data,
  48: 		      size_t  size,
  49: 		const char  *srcfile,
  50: 		const char  *ext,
  51: 		      int    nID
  52: 	)
  53: {
  54: 	// パラメタの仮定
  55: 	ASSERT( 0 <= nID );
  56: 	ASSERT( 0 <  size );
  57: 	ASSERT( IsPathExist( srcfile ) );
  58: 	ASSERT( IsValidReadPtr( data, size ) );
  59: 	ASSERT( IsValidStringPtr( ext ) );
  60: 	ASSERT( '.' == ext[ 0 ] );
  61: 
  62: 	// 出力ファイル
  63: 	FILE *fw = CreateOutputResourceFile( srcfile, ext, nID );
  64: 	if ( !fw )
  65: 	{
  66: 		return false;
  67: 	}
  68: 
  69: 	VERIFY( size == fwrite( data, 1, size, fw ) );
  70: 	VERIFY( 0 == fclose( fw ) );
  71: 	return true;
  72: }//OutputResourceFile
  73: 
  74: 
  75: //******************************************************************************************************************
  76: // private
  77: //******************************************************************************************************************
  78: static void GetFileBodyNameBuffer( char *buffer, int bufsize, const char *path );
  79: static bool  CreateOutputDirectory( const char *filename );
  80: static void  MakeOutputDirectoryName( char *buffer, int bufsize, const char *filepath );
  81: static void  MakeOutputName( char *filename, int bufsize, const char *path, const char *pre, const char *ext, int nID );
  82: 
  83: 
  84: //*********************************************************
  85: // CreateOutputResourceFile
  86: //*********************************************************
  87: static 
  88: FILE *
  89: CreateOutputResourceFile
  90: 	(
  91: 		const char  *srcfile,
  92: 		const char  *ext,
  93: 		      int    nID
  94: 	)
  95: {
  96: 	// パラメタの仮定
  97: 	ASSERT( 0 <= nID );
  98: 	ASSERT( IsPathExist( srcfile ) );
  99: 	ASSERT( IsValidStringPtr( ext ) );
 100: 	ASSERT( '.' == ext[ 0 ] );
 101: 
 102: 	// 出力先ディレクトリ
 103: 	char outpath[ MAX_PATH_BUF ];	
 104: 	MakeOutputDirectoryName( outpath, numof(outpath), srcfile );
 105: 	if ( ! CreateOutputDirectory( outpath ) )
 106: 	{
 107: 		return false;
 108: 	}
 109: 	
 110: 	// 出力ファイル名
 111: 	char filename[ MAX_PATH_BUF ];
 112: 	MakeOutputName( filename, numof(filename), outpath, GetFileExtensionPtr(srcfile), ext, nID );
 113: 	ASSERT( strlen(ext) < strlen(filename) );
 114: 
 115: 	// 出力ファイル
 116: 	return fopen( filename, "wb" );
 117: }//CreateOutputResourceFile
 118: 
 119: //*********************************************************
 120: // CreateOutputDirectory
 121: //*********************************************************
 122: static
 123: bool 
 124: CreateOutputDirectory
 125: 	(
 126: 		const char *outpath
 127: 	)
 128: {
 129: 	// パラメタの仮定
 130: 	ASSERT( IsValidStringPtr( outpath ) );
 131: 
 132: 	if ( !IsPathExist( outpath ) )
 133: 	{
 134: 		VERIFY( CreateDirectory( outpath, null ) );
 135: 	}
 136: 
 137: 	return IsPathExist( outpath );
 138: }//CreateOutputDirectory
 139: 
 140: //*********************************************************
 141: // MakeOutputDirectoryName
 142: //*********************************************************
 143: static
 144: void
 145: MakeOutputDirectoryName
 146: 	(	
 147: 		      char *buffer,
 148: 		      int   bufsize,
 149: 		const char *srcfile
 150: 	)
 151: {
 152: 	// パラメタの仮定
 153: 	ASSERT( IsEnoughPathBufferSize( bufsize ) ); // [WARN]
 154: 	ASSERT( IsPathExist( srcfile ) );
 155: 	ASSERT( IsValidPtr( buffer, bufsize ) );
 156: 	ASSERT( (strtail(srcfile) < buffer) || (buffer + bufsize < srcfile) );
 157: 
 158: 	//
 159: 	char path[ MAX_PATH_BUF ];
 160: 	GetFileLocation( srcfile, path, numof(path) );
 161: 	ASSERT( IsPathExist( path ) );
 162: 
 163: 	//
 164: 	char body[ MAX_PATH_BUF ];
 165: 	GetFileBodyNameBuffer( body, numof(body), srcfile );
 166: 
 167: 	//
 168: 	MakeFullPath( buffer, bufsize, path, body );
 169: 	ASSERT( IsValidStringPtr( buffer ) );
 170: }//MakeOutputDirectoryName
 171: 
 172: //*********************************************************
 173: // MakeOutputName
 174: //*********************************************************
 175: static
 176: void
 177: MakeOutputName
 178: 	(
 179: 		      char *filename, 
 180: 		      int   bufsize,
 181: 		const char *path,
 182: 		const char *pre, 
 183: 		const char *ext, 
 184: 		      int   nID
 185: 	)
 186: {
 187: 	// パラメタの仮定
 188: 	ASSERT( IsEnoughPathBufferSize( bufsize ) ); // [WARN]
 189: 	ASSERT( IsPathExist( path ) );
 190: 	ASSERT( IsValidStringPtr( pre ) );
 191: 	ASSERT( IsValidStringPtr( ext ) );
 192: 	ASSERT( IsValidPtr( filename, bufsize ) );
 193: 	ASSERT( (strtail(path) < filename) || (filename + bufsize < path) );
 194: 	ASSERT( (strtail(ext) < filename) || (filename + bufsize < ext) );
 195: 	ASSERT( '.' == ext[0] );
 196: 
 197: 	//
 198: 	char bodyname[ MAX_PATH_BUF ];
 199: 	snprintf( bodyname, numof(bodyname), "%s%04x%s", (('.' == pre[0]) ? pre+1 : pre), nID, ext );
 200: 
 201: 	//
 202: 	MakeFullPath( filename, bufsize, path, bodyname );
 203: 	ASSERT( IsValidStringPtr( filename ) );
 204: }//MakeOutputName
 205: 
 206: //*********************************************************
 207: // GetFileBodyNameBuffer
 208: //*********************************************************
 209: static
 210: void
 211: GetFileBodyNameBuffer
 212: 	(
 213: 		      char *buffer,
 214: 		      int   bufsize,
 215: 		const char *path
 216: 	)
 217: {
 218: 	// パラメタの仮定
 219: 	ASSERT( IsEnoughPathBufferSize( bufsize ) ); // [WARN]
 220: 	ASSERT( IsValidStringPtr( path ) );
 221: 	ASSERT( IsValidPtr( buffer, bufsize ) );
 222: 	ASSERT( (strtail(path) < buffer) || (buffer + bufsize < path) );
 223: 
 224: 	const char *filename = GetFileNamePtr( path );
 225: 	strcopy( buffer, bufsize, filename );
 226: 	CutFileExtension( buffer );
 227: 	ASSERT( IsValidStringPtr( buffer ) );
 228: }//GetFileBodyNameBuffer
 229: 
 230: 
 231: //** end **
 232: 
 233: 
 234: 

参照:


Google
ご意見・ご感想をお聞かせ下さい。匿名で送信できます。

 * 返信が必要な場合には postmaster@katsura-kotonoha.sakura.ne.jp へ直接メールしてください。

水無瀬の部屋 > sample > exe2ico > util.cpp

このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/exe2ico/util_cpp.shtml
>> Amazon.co.jp 『たまゆら童子』 へ
>> 楽天ブックス 『たまゆら童子』 へ