[PR]

水無瀬の部屋 > Programming > sample > cur2cpp > putcpp.cpp
最終更新日: 2007/03/21

   1: //*********************************************************
   2: // プロジェクト: cur2cpp
   3: //   ファイル名: putcpp.cpp
   4: //*********************************************************
   5: #include "putcpp.h"
   6: #include <filefmt/curfile.h>
   7: #include <filefmt/bmpfile.h>
   8: 
   9: 
  10: //---------------------------------------------------------
  11: // マクロ の 定義
  12: //---------------------------------------------------------
  13: #define DBG_ALLOCNAME          "LoadMonochromeCursorFileData"
  14: #define CALC_MASK_SIZE( cih )  ((cih).width * (cih).height / CHAR_BIT)
  15: 
  16: 
  17: //---------------------------------------------------------
  18: // ファイルスコープ関数 の 宣言
  19: //---------------------------------------------------------
  20: static size_t make_code( char *buf, size_t bufsize, const CURSORINFOHEAD *cih, const BITMAPINFOHEADER *bih, const BYTE *maskXOR, const BYTE *maskAND );
  21: 
  22: 
  23: //*********************************************************
  24: // GenerateCode
  25: //*********************************************************
  26: char * 
  27: GenerateCode
  28: 	(
  29: 		const CURSORINFOHEAD   *cih,
  30: 		const BITMAPINFOHEADER *bih,
  31: 		const BYTE             *maskXOR,
  32: 		const BYTE             *maskAND
  33: 	)
  34: {
  35: 	// パラメタの仮定
  36: 	ASSERT( IsValidReadPtr( cih, sizeof( *cih ) ) );
  37: 	ASSERT( IsValidReadPtr( cih, sizeof( *bih ) ) );
  38: 	ASSERT( IsValidReadPtr( maskXOR, CALC_MASK_SIZE( *cih ) ) );
  39: 	ASSERT( IsValidReadPtr( maskAND, CALC_MASK_SIZE( *cih ) ) );
  40: 
  41: 	//
  42: 	const size_t count = make_code( null, 0, cih, bih, maskXOR, maskAND );
  43: 	char *buf = (char *)malloc( count * sizeof( *buf ) );
  44: 	if ( !buf )
  45: 	{
  46: 		return buf;
  47: 	}
  48: 
  49: 	//
  50: 	VERIFY( count == make_code( buf, count, cih, bih, maskXOR, maskAND ) );
  51: 
  52: 	return buf;
  53: }//GenerateCode
  54: 
  55: 
  56: //******************************************************************************************************************
  57: //
  58: //******************************************************************************************************************
  59: static size_t put_bitmask( char *buf, size_t bufsize, size_t offset, const CURSORINFOHEAD *cih, const char *name, const BYTE *mask );
  60: static size_t put_bitmask_line( char *buf, size_t bufsize, size_t offset, const BYTE *mask, int size );
  61: static size_t put_bitmask_byte( char *buf, size_t bufsize, size_t offset, const BYTE mask );
  62: static size_t put_function( char *buf, size_t bufsize, size_t offset, const CURSORINFOHEAD *cih, const char *nameAND, const char *nameXOR );
  63: static size_t put_return( char *buf, size_t bufsize, size_t offset );
  64: static size_t put_indent( char *buf, size_t bufsize, size_t offset, int depth );
  65: 
  66: 
  67: //*********************************************************
  68: // make_code
  69: //*********************************************************
  70: size_t 
  71: make_code
  72: 	(
  73: 		      char             *buf,
  74: 		      size_t            bufsize,
  75: 		const CURSORINFOHEAD   *cih,
  76: 		const BITMAPINFOHEADER *bih,
  77: 		const BYTE             *maskXOR,
  78: 		const BYTE             *maskAND
  79: 	)
  80: {
  81: 	// パラメタの仮定
  82: 	ASSERT(  buf || (!buf && (0 == bufsize)) );
  83: 	ASSERT( !buf || IsValidStringBufferPtr( buf, bufsize ) );
  84: 	ASSERT( !buf || (DESTROY_TEXT_BUFFER( buf, bufsize ), true) ); // [破壊]
  85: 	ASSERT( IsValidReadPtr( cih, sizeof( *cih ) ) );
  86: 	ASSERT( IsValidReadPtr( cih, sizeof( *bih ) ) );
  87: 	ASSERT( IsValidReadPtr( maskXOR, CALC_MASK_SIZE( *cih ) ) );
  88: 	ASSERT( IsValidReadPtr( maskAND, CALC_MASK_SIZE( *cih ) ) );
  89: 
  90: 	//
  91: 	size_t count = 0;
  92: 
  93: 	//
  94: 	count += strputs(     buf, bufsize, count, "{" );
  95: 	count += put_return( buf, bufsize, count );
  96: 
  97: 	//
  98: 	count += strputs(     buf, bufsize, count, "#define " );
  99: 	count += strputs(     buf, bufsize, count, "m" ); // マクロ名
 100: 	count += strputs(     buf, bufsize, count, "( a, b, c, d ) (((((((( 0 a, (((((((( 0 b, (((((((( 0 c, (((((((( 0 d" );
 101: 	count += put_return( buf, bufsize, count );
 102: 
 103: 	//
 104: 	count += strputs(     buf, bufsize, count, "#define X )*2+1" );
 105: 	count += put_return( buf, bufsize, count );
 106: 
 107: 	//
 108: 	count += strputs(     buf, bufsize, count, "#define _ )*2" );
 109: 	count += put_return( buf, bufsize, count );
 110: 
 111: 	// ビットパーターン配列を生成する
 112: 	count += put_bitmask( buf, bufsize, count, cih, "maskAND", maskAND );
 113: 	count += put_bitmask( buf, bufsize, count, cih, "maskXOR", maskXOR );
 114: 	
 115: 	//
 116: 	count += strputs(     buf, bufsize, count, "#undef " );
 117: 	count += strputs(     buf, bufsize, count, "m" ); // マクロ名
 118: 	count += put_return( buf, bufsize, count );
 119: 
 120: 	//
 121: 	count += strputs(     buf, bufsize, count, "#undef X" );
 122: 	count += put_return( buf, bufsize, count );
 123: 
 124: 	//
 125: 	count += strputs(     buf, bufsize, count, "#undef _" );
 126: 	count += put_return( buf, bufsize, count );
 127: 	
 128: 	// 一行あける
 129: 	count += put_return( buf, bufsize, count );
 130: 
 131: 	// 関数 CreateCursor() の呼び出しを生成する
 132: 	count += put_function( buf, bufsize, count, cih, "maskAND", "maskXOR" );
 133: 
 134: 	//
 135: 	count += strputs(     buf, bufsize, count, "}" );
 136: 	count += put_return( buf, bufsize, count );
 137: 
 138: 	//
 139: 	if (  buf && (count < bufsize) ) { buf[ count ] = '\0'; }
 140: 	if ( !buf || (count < bufsize) ) { ++count; }
 141: 
 142: 	//
 143: 	ASSERT( !buf || IsValidStringPtr( buf ) );
 144: 	ASSERT( !buf || (count == 1 + strlen( buf )) || (0 == bufsize) );
 145: 	ASSERT( !buf || (count == make_code( null, 0, cih, bih, maskXOR, maskAND )) );
 146: 	return count;
 147: }//make_code
 148: 
 149: //*********************************************************
 150: // put_bitmask
 151: //*********************************************************
 152: static
 153: size_t 
 154: put_bitmask
 155: 	(
 156: 		      char           *buf,
 157: 		      size_t          bufsize,
 158: 		      size_t          offset,
 159: 		const CURSORINFOHEAD *cih,
 160: 		const char           *name,
 161: 		const BYTE           *mask 
 162: 	)
 163: {
 164: 	// パラメタの仮定
 165: 	ASSERT( !buf || (offset < bufsize) );
 166: 	ASSERT(  buf || (!buf && (0 == bufsize)) );
 167: 	ASSERT( !buf || IsValidStringBufferPtr( buf, bufsize ) );
 168: 	ASSERT( !buf || (DESTROY_TEXT_BUFFER( buf + offset, bufsize - offset ), true) ); // [破壊]
 169: 	ASSERT( IsValidReadPtr( cih, sizeof( *cih ) ) );
 170: 	ASSERT( IsValidStringPtr( name ) );
 171: 	ASSERT( IsValidReadPtr( mask, CALC_MASK_SIZE( *cih ) ) );
 172: 	
 173: 	//
 174: 	size_t count = 0;
 175: 
 176: 	//
 177: 	int depth = 1;
 178: 
 179: 	//
 180: 	count += put_indent( buf, bufsize, offset + count, depth ); //
 181: 	count += strputs(     buf, bufsize, offset + count, "const BYTE " ); //
 182: 	count += strputs(     buf, bufsize, offset + count, name );            // 配列名 
 183: 	count += strputs(     buf, bufsize, offset + count, "[ ] = " );      //
 184: 	count += put_return( buf, bufsize, offset + count );
 185: 
 186: 	//
 187: 	++depth;
 188: 
 189: 	//
 190: 	count += put_indent( buf, bufsize, offset + count, depth );
 191: 	count += strputs(     buf, bufsize, offset + count, "{" );
 192: 	count += put_return( buf, bufsize, offset + count );
 193: 
 194: 	//
 195: 	++depth;
 196: 
 197: 	// 行は逆順に入っている
 198: 	{for( int i = cih->height; 0 < i; --i )
 199: 	{
 200: 		const int line_byte = cih->width / CHAR_BIT; // 1行のバイト数を求める
 201: 		const int line_offset = (i - 1) * line_byte;
 202: 
 203: 		ASSERT( (0 <= line_offset) && (line_offset < CALC_MASK_SIZE( *cih )) );
 204: 
 205: 		count += put_indent(       buf, bufsize, offset + count, depth );
 206: 		count += put_bitmask_line( buf, bufsize, offset + count, mask + line_offset, line_byte );
 207: 		count += strputs(           buf, bufsize, offset + count, ", " );
 208: 		count += put_return(       buf, bufsize, offset + count );
 209: 	}}
 210: 
 211: 	//
 212: 	--depth;
 213: 
 214: 	//
 215: 	count += put_indent( buf, bufsize, offset + count, depth );
 216: 	count += strputs(     buf, bufsize, offset + count, "};" );
 217: 	count += put_return( buf, bufsize, offset + count );
 218: 
 219: 	//
 220: 	ASSERT( !buf || (count == put_bitmask( null, 0, offset, cih, name, mask )) );
 221: 	return count;
 222: }//put_bitmask
 223: 
 224: //*********************************************************
 225: // put_bitmask_line
 226: //*********************************************************
 227: static
 228: size_t 
 229: put_bitmask_line
 230: 	(
 231: 		      char   *buf,
 232: 		      size_t  bufsize,
 233: 		      size_t  offset,
 234: 		const BYTE   *mask,
 235: 		      int     size
 236: 	)
 237: {
 238: 	// パラメタの仮定
 239: 	ASSERT( !buf || (offset < bufsize) );
 240: 	ASSERT(  buf || (!buf && (0 == bufsize)) );
 241: 	ASSERT( !buf || IsValidStringBufferPtr( buf, bufsize ) );
 242: 	ASSERT( !buf || (DESTROY_TEXT_BUFFER( buf + offset, bufsize - offset ), true) ); // [破壊]
 243: 	ASSERT( 0 < size );
 244: 	ASSERT( IsValidReadPtr( mask, size ) );
 245: 
 246: 	//
 247: 	size_t count = 0;
 248: 
 249: 	//
 250: 	count += strputs( buf, bufsize, offset + count, "m" ); // マクロ名 
 251: 	count += strputs( buf, bufsize, offset + count, "( " );
 252: 
 253: 	//
 254: 	const char *separetor = "";
 255: 	{for( int i = 0; i < size; ++i )
 256: 	{
 257: 		count += strputs(           buf, bufsize, offset + count, separetor );
 258: 		count += put_bitmask_byte( buf, bufsize, offset + count, mask[ i ] );
 259: 
 260: 		separetor = ",";
 261: 	}}
 262: 
 263: 	//
 264: 	count += strputs( buf, bufsize, offset + count, " )" );
 265: 
 266: 	ASSERT( !buf || (count == put_bitmask_line( null, 0, offset, mask, size )) );
 267: 	return count;
 268: }//put_bitmask_line
 269: 
 270: //*********************************************************
 271: // put_bitmask_byte
 272: //*********************************************************
 273: static
 274: size_t 
 275: put_bitmask_byte
 276: 	(
 277: 		      char   *buf,
 278: 		      size_t  bufsize,
 279: 		      size_t  offset,
 280: 		const BYTE    mask
 281: 	)
 282: {
 283: 	// パラメタの仮定
 284: 	ASSERT( !buf || (offset < bufsize) );
 285: 	ASSERT(  buf || (!buf && (0 == bufsize)) );
 286: 	ASSERT( !buf || IsValidStringBufferPtr( buf, bufsize ) );
 287: 	ASSERT( !buf || (DESTROY_TEXT_BUFFER( buf + offset, bufsize - offset ), true) ); // [破壊]
 288: 
 289: 	//
 290: 	size_t count = 0;
 291: 
 292: 	//
 293: 	const char *separetor = "";
 294: 	{for( int i = CHAR_BIT; 0 < i; --i )
 295: 	{
 296: 		count += strputs( buf, bufsize, offset + count, separetor );
 297: 
 298: 		//
 299: 		const bool bit = (0 != (mask & (1 << (i - 1))));
 300: 		count += strputs( buf, bufsize, offset + count, bit ? "X" : "_" );
 301: 
 302: 		separetor = " ";
 303: 	}}
 304: 
 305: 	ASSERT( !buf || (count == put_bitmask_byte( null, 0, offset, mask )) );
 306: 	return count;
 307: }//put_bitmask_byte
 308: 
 309: //*********************************************************
 310: // put_function
 311: //*********************************************************
 312: static
 313: size_t 
 314: put_function
 315: 	(
 316: 		      char           *buf,
 317: 		      size_t          bufsize,
 318: 		      size_t          offset,
 319: 		const CURSORINFOHEAD *cih,     // 関数 CreateCursor() に作成させるカーソルの情報
 320: 		const char           *nameAND, // 関数 CreateCursor() の第6引数の名前
 321: 		const char           *nameXOR  // 関数 CreateCursor() の第7引数の名前
 322: 	)
 323: {
 324: 	// パラメタの仮定
 325: 	ASSERT( !buf || (offset < bufsize) );
 326: 	ASSERT(  buf || (!buf && (0 == bufsize)) );
 327: 	ASSERT( !buf || IsValidStringBufferPtr( buf, bufsize ) );
 328: 	ASSERT( !buf || (DESTROY_TEXT_BUFFER( buf + offset, bufsize - offset ), true) ); // [破壊]
 329: 	ASSERT( IsValidReadPtr( cih, sizeof( *cih ) ) );
 330: 	ASSERT( IsValidStringPtr( nameAND ) );
 331: 	ASSERT( IsValidStringPtr( nameXOR ) );
 332: 
 333: 	//
 334: 	size_t count = 0;
 335: 
 336: 	//
 337: 	int depth = 1;
 338: 
 339: 	//
 340: 	count += put_indent( buf, bufsize, offset + count, depth );
 341: 	count += strputs(     buf, bufsize, offset + count, "return CreateCursor" );
 342: 	count += put_return( buf, bufsize, offset + count );
 343: 
 344: 	//
 345: 	++depth;
 346: 
 347: 	//
 348: 	count += put_indent( buf, bufsize, offset + count, depth );
 349: 	count += strputs(     buf, bufsize, offset + count, "(" );
 350: 	count += put_return( buf, bufsize, offset + count );
 351: 
 352: 	//
 353: 	++depth;
 354: 
 355: 	// hInstance
 356: 	count += put_indent( buf, bufsize, offset + count, depth );
 357: 	count += strputs(     buf, bufsize, offset + count, "hInstance, // カーソルを作成するモジュールのインスタンス" );
 358: 	count += put_return( buf, bufsize, offset + count );
 359: 
 360: 	// cih->wHotSpotX
 361: 	{
 362: 		char num[ 32 ];
 363: 		VERIFY( 0 < snprintf( num, numof(num), "%d", cih->wHotSpotX ) );
 364: 		count += put_indent( buf, bufsize, offset + count, depth );
 365: 		count += strputs(     buf, bufsize, offset + count, num );
 366: 		count += strputs(     buf, bufsize, offset + count, ", // ホットスポットの x 座標" );
 367: 		count += put_return( buf, bufsize, offset + count );
 368: 	}
 369: 
 370: 	// cih->wHotSpotY
 371: 	{
 372: 		char num[ 32 ];
 373: 		VERIFY( 0 < snprintf( num, numof(num), "%d", cih->wHotSpotY ) );
 374: 		count += put_indent( buf, bufsize, offset + count, depth );
 375: 		count += strputs(     buf, bufsize, offset + count, num );
 376: 		count += strputs(     buf, bufsize, offset + count, ", // ホットスポットの y 座標" );
 377: 		count += put_return( buf, bufsize, offset + count );
 378: 	}
 379: 
 380: 	// cih->width
 381: 	{
 382: 		char num[ 32 ];
 383: 		VERIFY( 0 < snprintf( num, numof(num), "%d", cih->width ) );
 384: 		count += put_indent( buf, bufsize, offset + count, depth );
 385: 		count += strputs(     buf, bufsize, offset + count, num );
 386: 		count += strputs(     buf, bufsize, offset + count, ", // カーソルの幅" );
 387: 		count += put_return( buf, bufsize, offset + count );
 388: 	}
 389: 
 390: 	// cih->height
 391: 	{
 392: 		char num[ 32 ];
 393: 		VERIFY( 0 < snprintf( num, numof(num), "%d", cih->height ) );
 394: 		count += put_indent( buf, bufsize, offset + count, depth );
 395: 		count += strputs(     buf, bufsize, offset + count, num );
 396: 		count += strputs(     buf, bufsize, offset + count, ", // カーソルの高さ" );
 397: 		count += put_return( buf, bufsize, offset + count );
 398: 	}
 399: 
 400: 	// nameAND
 401: 	count += put_indent( buf, bufsize, offset + count, depth );
 402: 	count += strputs(     buf, bufsize, offset + count, nameAND );
 403: 	count += strputs(     buf, bufsize, offset + count, ", // " );
 404: 	count += put_return( buf, bufsize, offset + count );
 405: 
 406: 	// nameXOR
 407: 	count += put_indent( buf, bufsize, offset + count, depth );
 408: 	count += strputs(     buf, bufsize, offset + count, nameXOR );
 409: 	count += strputs(     buf, bufsize, offset + count, "  // " );
 410: 	count += put_return( buf, bufsize, offset + count );
 411: 
 412: 	//
 413: 	--depth;
 414: 
 415: 	//
 416: 	count += put_indent( buf, bufsize, offset + count, depth );
 417: 	count += strputs(     buf, bufsize, offset + count, ");" );
 418: 	count += put_return( buf, bufsize, offset + count );
 419: 
 420: 	ASSERT( !buf || (count == put_function( null, 0, offset, cih, nameAND, nameXOR )) );
 421: 	return count;
 422: }//put_function
 423: 
 424: //*********************************************************
 425: // put_indent
 426: //*********************************************************
 427: static
 428: size_t 
 429: put_indent
 430: 	(
 431: 		char   *buf,
 432: 		size_t  bufsize,
 433: 		size_t  offset,
 434: 		int     depth
 435: 	)
 436: {
 437: 	// パラメタの仮定
 438: 	ASSERT( !buf || (offset < bufsize) );
 439: 	ASSERT(  buf || (!buf && (0 == bufsize)) );
 440: 	ASSERT( !buf || IsValidStringBufferPtr( buf, bufsize ) );
 441: 	ASSERT( !buf || (DESTROY_TEXT_BUFFER( buf + offset, bufsize - offset ), true) ); // [破壊]
 442: 	ASSERT( 0 < depth );
 443: 
 444: 	//
 445: 	size_t count = 0;
 446: 
 447: 	//
 448: 	{for( int n = depth; 0 < n; --n )
 449: 	{
 450: 		count += strputs( buf, bufsize, offset + count, "\t" );
 451: 	}}
 452: 
 453: 	ASSERT( !buf || (count == put_indent( null, 0, offset, depth )) );
 454: 	return count;
 455: }//put_indent
 456: 
 457: //*********************************************************
 458: // put_return
 459: //*********************************************************
 460: static
 461: size_t 
 462: put_return
 463: 	(
 464: 		char   *buf,
 465: 		size_t  bufsize,
 466: 		size_t  offset
 467: 	)
 468: {
 469: 	// パラメタの仮定
 470: 	ASSERT( !buf || (offset < bufsize) );
 471: 	ASSERT(  buf || (!buf && (0 == bufsize)) );
 472: 	ASSERT( !buf || IsValidStringBufferPtr( buf, bufsize ) );
 473: 	ASSERT( !buf || (DESTROY_TEXT_BUFFER( buf + offset, bufsize - offset ), true) ); // [破壊]
 474: 
 475: 	//
 476: 	size_t count = 0;
 477: 
 478: 	count += strputs( buf, bufsize, offset + count, "\r\n" );
 479: 
 480: 	ASSERT( !buf || (count == put_return( null, 0, offset )) );
 481: 	return count;
 482: }//put_return
 483: 
 484: 
 485: //** end **

参照:


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

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

水無瀬の部屋 > sample > cur2cpp > putcpp.cpp

このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/cur2cpp/putcpp_cpp.shtml
『新妻LOVELY×CATION』を応援しています!