1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
// For licensing and usage information, read docs/winui_license.txt
//****************************************************************************
/***************************************************************************
mkhelp.c
Simple resource HIDC to Help entry tool.
MSH - 20070815
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "osdcore.h"
static int compare( const void *arg1, const void *arg2 );
static void extract_help_ids(const char *buffer, FILE *fp);
/*-------------------------------------------------
main - primary entry point
-------------------------------------------------*/
int main(int argc, char *argv[])
{
const char *resourcefile, *varname, *type;
FILE *src, *dst;
char *buffer;
int bytes = 0;
varname = "dwHelpIDs";
type = "DWORD";
/* needs at least three arguments */
if (argc < 1)
{
fprintf(stderr,
"Usage:\n"
" mkhelp <resource.rc>\n"
"\n"
"The default <type> is DWORD, with an assumed NULL terminator pair\n"
);
return 0;
}
/* extract arguments */
resourcefile = argv[1];
/* open source file */
src = fopen(resourcefile, "rb");
if (src == NULL)
{
fprintf(stderr, "Unable to open resource file '%s'\n", resourcefile);
return 1;
}
/* determine file size */
fseek(src, 0, SEEK_END);
bytes = ftell(src);
fseek(src, 0, SEEK_SET);
/* allocate memory */
buffer = (char*)malloc(bytes + 1);
if (buffer == NULL)
{
fclose(src);
fprintf(stderr, "Out of memory allocating %d byte buffer\n", bytes);
return 1;
}
/* read the source file */
fread(buffer, 1, bytes, src);
buffer[bytes] = 0;
fclose(src);
/* open dest file */
dst = stdout;
fprintf(dst,"/*\n * Help ID array - Generated by mkhelp\n */\n\n");
fprintf(dst, "#include <windows.h>\n#include \"resource.h\"\n#include \"resource.hm\"\n\n");
/* write the initial header */
fprintf(dst, "extern const %s %s[] =\n{\n", type, varname);
extract_help_ids(buffer, dst);
fprintf(dst, "\n};\n");
/* close the files */
free(buffer);
return 0;
}
static int compare( const void *arg1, const void *arg2 )
{
/* Compare all of both strings: */
return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}
static void extract_help_ids(const char *buffer, FILE *fp)
{
const char *ptr = buffer;
char **help_ids = (char **)malloc(500 * sizeof(char *));
int num_help_id = 0;
int i;
//memset(help_ids, '\0', sizeof(*help_ids));
memset(help_ids, 0, 500);
while(*ptr) {
if (strncmp("HIDC_", ptr, 5) == 0) {
char id_name[128];
char *end = id_name;
char *id;
memset(id_name, '\0', sizeof(id_name));
while (*ptr && *ptr != '\x0d' && *ptr != '\x0a' ) {
*end++ = *ptr++;
}
id = (char *)malloc(strlen(id_name));
memset(id, '\0', strlen(id_name));
memcpy(id, &id_name[1], strlen(&id_name[1]));
help_ids[num_help_id] = id;
num_help_id++;
} else {
ptr++;
}
}
/* Sort using Quicksort algorithm: */
qsort( (void *)help_ids, (size_t)num_help_id, sizeof( char * ), compare );
// Now print them out.
ptr = help_ids[0];
for (i = 0; i < num_help_id; i++) {
if (i > 0) {
if (strcmp(ptr, help_ids[i]) == 0) {
continue;
}
}
fprintf(fp, "\t%-30s,H%s,\n", help_ids[i], help_ids[i]);
ptr = help_ids[i];
}
fprintf(fp, "\t%-30i,%i\n", 0, 0);
// free our allocations.
for (i = 0; i < num_help_id; i++) {
free(help_ids[i]);
}
free (help_ids);
}
|