[go: up one dir, main page]

Menu

[r11]: / engine / chknpc.c  Maximize  Restore  History

Download this file

284 lines (237 with data), 4.6 kB

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* NPC link checker 1.4
*
* This is a quick hack, not the epitomy of optimisation or coding style
* Should be portable though
*
* This program is in the public domain
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#if defined( __linux__) || defined(__BEOS__) || defined(__APPLE__)
#define NO_STRICMP
#endif
char **pageindex;
int pages;
#ifdef NO_STRICMP
int stricmp(char *a,char *b)
{
for(;;)
{
if(*a != *b)
if(tolower(*a) != tolower(*b))
return (int)tolower(*a) - (int)tolower(*b);
if(!*a)
return 0;
a++;
b++;
}
return 0;
}
#endif
int find_page(char *page)
{
int ctr;
// Check for intrinsics
if(!stricmp(page,"exit"))
return 1;
// Check page index
for(ctr=0;ctr<pages;ctr++)
{
if(!stricmp(page,pageindex[ctr]))
{
// printf("%s: found link %s [%d/%d]\n",page,pageindex[ctr],ctr,pages);
return 1;
}
}
return 0;
}
void process_file(char *fname)
{
FILE *fp;
char buffer[1024];
int ctr,line,errors,links=0,llink=0,rlink=0;
char *p,*ptr1,*ptr2;
char curpage[128];
printf("Checking %s\n",fname);
strcpy(curpage,"<unknown>");
fp=fopen(fname,"r");
if(!fp)
{
printf("Cannot open file %s\n",fname);
return;
}
errors=0;
// First, count pages
pages=0;
do
{
fgets(buffer,1024,fp);
if(strstr(buffer,"[page=\""))
pages++;
} while(!feof(fp));
if(!pages)
{
printf("Warning: No pages defined!\n");
fclose(fp);
return;
}
// Now allocate room for the pages
pageindex=(char **)calloc(pages,sizeof(char *));
if(!pageindex)
{
printf("Could not allocate page index, quitting\n");
exit(1);
}
for(ctr=0;ctr<pages;ctr++)
{
pageindex[ctr]=(char *)malloc(128);
if(!pageindex[ctr])
{
printf("Out of memory, quitting\n");
exit(1);
}
}
// Back to start
fseek(fp,0L,SEEK_SET);
// Now read in the page index
ctr=0;
line=0;
do
{
fgets(buffer,1024,fp);
line++;
if(strstr(buffer,"[page=\""))
{
// Find opening quote
ptr1=strchr(buffer,'\"');
ptr1++;
// Find ending quote
ptr2=strrchr(ptr1,'\"');
if(!ptr2)
{
printf("No ending quote in page declaration at line %d\n",line);
errors++;
continue;
}
*ptr2=0; // Kill the ending quote
strcpy(pageindex[ctr],ptr1);
ctr++;
}
} while(!feof(fp));
// Back to start again
fseek(fp,0L,SEEK_SET);
// Now find the links
line=0;
do
{
fgets(buffer,1024,fp);
line++;
// If it's a page declaration, reset links count
if(strstr(buffer,"[page=\""))
{
links=0;
// Get current page name
ptr1=strchr(buffer,'\"');
ptr1++;
strcpy(curpage,ptr1);
ptr1=strchr(curpage,'\"');
if(ptr1) *ptr1=0; // Kill ending quote
}
// Mark if there are L&R links
if(strstr(buffer,"[left=\""))
llink=1;
if(strstr(buffer,"[right=\""))
rlink=1;
// If end of page and no links, there's a problem
if(strstr(buffer,"[endpage]"))
{
if(!links)
{
printf("Page '%s' has no links at line %d\n",curpage,line);
errors++;
}
if(llink && !rlink)
printf("Warning: Page '%s' has only a left link at line %d\n",curpage,line);
llink=0;rlink=0; // Reset L&R flags
strcpy(curpage,"<unknown>");
}
// Check for links
p = NULL;
#define CHECK_LINKS(CHK) \
if(!p)\
{\
p=strstr(buffer,CHK);\
if(p)\
{\
links++;\
}\
}
CHECK_LINKS("[goto=\"");
CHECK_LINKS("[linkto=\"");
CHECK_LINKS("[nextpage=\"");
CHECK_LINKS("[append=\"");
CHECK_LINKS("[left=\"");
CHECK_LINKS("[right=\"");
// CHECK_LINKS("[random_page=\""); // Don't check destination
// Random Pages are a special case
if(!p)
{
p=strstr(buffer,"random_page=\"");
if(p)
{
links++;
p=NULL; // prevent checking (hack)
}
}
// Check quotes
if(p)
{
// Find opening quote
ptr1=strchr(p,'\"');
ptr1++;
// Find ending quote
ptr2=strrchr(ptr1,'\"');
if(!ptr2)
{
printf("No ending quote in link at line %d in page '%s'\n",line,curpage);
errors++;
continue;
}
*ptr2=0; // Kill the ending quote
if(!find_page(ptr1))
{
printf("Page '%s' not found in link at line %d in page '%s'\n",ptr1,line,curpage);
errors++;
}
// else
// printf("%s: found link %s\n",p,ptr1);
}
} while(!feof(fp));
if(!find_page("start"))
printf("Warning: No start page in this file\n");
if(errors)
printf("%d errors found\n",errors);
//else
// printf("No errors\n");
// Free everything and quit
for(ctr=0;ctr<pages;ctr++)
free(pageindex[ctr]);
free(pageindex);
}
int main(int argc,char *argv[])
{
int ctr;
if(argc<2)
{
printf("NPC link checker 1.4\n");
printf("Syntax: checknpc <filenames>\n");
exit(1);
}
for(ctr=1;ctr<argc;ctr++)
process_file(argv[ctr]);
printf("Done\n");
return 0;
}