OSDN Git Service

Initial commit for new package.
[mingw/wtklite.git] / strres.cpp
1 /*
2  * strres.cpp
3  *
4  * ---------------------------------------------------------------------------
5  *
6  * Implementation of a minimal C++ class framework for use with the
7  * Microsoft Windows Application Programming Interface.
8  *
9  * $Id$
10  *
11  * This file provides the implementation of the constructor for the
12  * StringResource class.
13  *
14  * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
15  * Copyright (C) 2012, MinGW.org Project.
16  *
17  * ---------------------------------------------------------------------------
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining a
20  * copy of this software and associated documentation files (the "Software"),
21  * to deal in the Software without restriction, including without limitation
22  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
23  * and/or sell copies of the Software, and to permit persons to whom the
24  * Software is furnished to do so, subject to the following conditions:
25  *
26  * The above copyright notice and this permission notice shall be included
27  * in all copies or substantial portions of the Software.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
32  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35  * DEALINGS IN THE SOFTWARE.
36  *
37  * ---------------------------------------------------------------------------
38  *
39  */
40 #define WIN32_LEAN_AND_MEAN
41
42 #include "wtkplus.h"
43 #include <string.h>
44 #include <stdio.h>
45
46 /* MSDN says that the maximum length of any one string resource
47  * is 4097 characters; it isn't clear if this limit includes the
48  * terminating NUL, so we'll assume we may need one more.
49  */
50 #define WTK_STRING_RESOURCE_MAX  4098
51
52 WTK::StringResource::StringResource( HINSTANCE inst, unsigned int id )
53 {
54   /* First, allocate a temporary buffer, of sufficient size to
55    * accommodate a copy of the longest possible string resource.
56    */
57   char *tmp; value = NULL;
58   if( (tmp = (char *)(malloc( WTK_STRING_RESOURCE_MAX ))) == NULL )
59     throw( runtime_error( "Insufficient memory" ) );
60
61   /* Attempt to load the specified resource into this buffer...
62    */
63   if( ! LoadString( inst, id, tmp, WTK_STRING_RESOURCE_MAX ) )
64   {
65     /* ...but if that fails, release the temporary buffer
66      * to avoid leaking memory...
67      */
68     free( (void *)(tmp) );
69
70     /* ...and bail out, with a suitable diagnostic message.
71      */
72     throw( runtime_error( error_text( "String resource #%u not found", id )) );
73   }
74
75   /* Having successfully loaded the resource, adjust the memory
76    * allocation to fit, without wastage...
77    */
78   if( (value = (char *)(realloc( tmp, 1 + strlen( tmp ) ))) == NULL )
79     /*
80      * ...making the temporary buffer reference permanent, in
81      * the event that the reallocation should fail.
82      */
83     value = tmp;
84 }
85
86 /* $RCSfile$: end of file */