-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtdparser.c
148 lines (131 loc) · 2.67 KB
/
dtdparser.c
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
/*
*
* dtdparser.c
*
* DTDファイルパスがあるかどうか見て、あればパースする。
*
* 08/06/01 FILE URIからパスにコンバート出来る。
* 08/15/01 Win32 port
* 10/18/01 Bug fix
*/
#include <stdio.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/param.h>
#endif
#include "defaults.h"
#include "debug.h"
#ifdef WIN32
# define MAXPATHLEN 512
#endif
/* public */
int dtd_parsing_result = 0;
static char* SYSTEM = "SYSTEM";
static int SYSTEM_LENGTH = 6;
static char* DOCTYPE = "DOCTYPE";
static int DOCTYPE_LENGTH = 7;
static int lines = 0;
/*
* ファイルから、引数の文字列(長さも渡す)を探す。
*/
static int lookup_string (FILE* file, char* string, int length)
{
int ch, match = 0;
int found = 0;
while((ch = fgetc (file)) != EOF) {
if (ch == '\n')
lines++;
if (match == 0) {
if (ch == string[0])
match = 1;
else
match = 0;
}
else {
if (ch == string[match])
match++;
else
match = 0;
if (match == length) {
found = 1;
break;
}
}
}
return found;
}
/*
* FILE URIをシステムパスに変換
*/
static char* convert_file_uri_to_system_path (char* uri)
{
char work[MAXPATHLEN];
if (strncmp (FILE_URI_HEADER1, uri, FILE_URI_HEADERL) == 0
|| strncmp (FILE_URI_HEADER2, uri, FILE_URI_HEADERL) == 0) {
int len = strlen (uri) - FILE_URI_HEADERL;
char* p;
memcpy (work, uri+FILE_URI_HEADERL, len);
work[len] = '\0';
#ifdef WIN32
p = work;
while (*p) {
if (*p == '/')
*p = '\\';
p++;
}
#endif
memcpy (uri, work, len); /* もどすだけ */
uri[len] = '\0';
}
return uri;
}
/*
* XMLファイルからDTDパスを検索
*/
char* lookupDTDPath (FILE* file)
{
static char dtdpath[MAXPATHLEN];
int ch, match = 0;
int line = 0;
lines = 0; /* fixed bug 10/18/01! */
if (lookup_string (file, DOCTYPE, DOCTYPE_LENGTH)) {
if (5 < lines)
return NULL; /* not found */
if (lookup_string (file, SYSTEM, SYSTEM_LENGTH)) {
char* p = dtdpath;
while ((ch = fgetc (file)) != '"')
;
while ((ch = fgetc (file)) != '"')
*p++ = ch;
*p = '\0';
}
convert_file_uri_to_system_path (dtdpath);
return dtdpath;
}
return NULL;
}
/*
* DTDをパースする
*
*/
int parseDTD (char* dtdpath) {
FILE* f;
int r = -1;
ENTER ("parseDTD");
if (f = fopen (dtdpath, "r")) {
r = parseXml (f); /* DTDをパースする */
fclose (f);
}
else
warn3 ("Specified DTD file \"", dtdpath, "\" does not exist.");
LEAVE ("parseDTD");
return r;
}
/*
main (int ac, char* av[])
{
dtdparser (av[1]);
}
*/