1 |
frodo |
2 |
/* |
2 |
|
|
parse_aux.c - Part of psiconv, a PSION 5 file formats converter |
3 |
|
|
Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> |
4 |
|
|
|
5 |
|
|
This program is free software; you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include "config.h" |
21 |
|
|
#include <stdarg.h> |
22 |
|
|
#include <stdio.h> |
23 |
|
|
#include <stdlib.h> |
24 |
|
|
|
25 |
|
|
#include "parse.h" |
26 |
|
|
#include "parse_routines.h" |
27 |
|
|
#include "data.h" |
28 |
|
|
|
29 |
frodo |
61 |
static void psiconv_default_error_handler(int kind, psiconv_u32 off, |
30 |
|
|
const char *message) |
31 |
|
|
{ |
32 |
|
|
fprintf(stderr,"%s\n",message); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
int psiconv_verbosity = PSICONV_VERB_WARN; |
36 |
|
|
|
37 |
|
|
psiconv_error_handler_t psiconv_error_handler = psiconv_default_error_handler; |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
#define MAX_MESSAGE 160 |
41 |
|
|
|
42 |
frodo |
2 |
void psiconv_fatal(int level, psiconv_u32 off, const char *format,...) |
43 |
|
|
{ |
44 |
frodo |
61 |
char buffer[MAX_MESSAGE]; |
45 |
frodo |
2 |
va_list ap; |
46 |
frodo |
61 |
size_t curlen; |
47 |
|
|
|
48 |
frodo |
2 |
va_start(ap,format); |
49 |
frodo |
61 |
snprintf(buffer,MAX_MESSAGE,"Fatal error (offset %08x): ",off); |
50 |
|
|
curlen = strlen(buffer); |
51 |
|
|
|
52 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
53 |
|
|
psiconv_error_handler(PSICONV_VERB_FATAL,off,buffer); |
54 |
frodo |
2 |
va_end(ap); |
55 |
frodo |
61 |
|
56 |
frodo |
2 |
exit(1); |
57 |
|
|
} |
58 |
|
|
|
59 |
frodo |
61 |
void psiconv_warn(int level, psiconv_u32 off, const char *format,...) |
60 |
frodo |
2 |
{ |
61 |
frodo |
61 |
char buffer[MAX_MESSAGE]; |
62 |
frodo |
2 |
va_list ap; |
63 |
frodo |
61 |
size_t curlen; |
64 |
|
|
|
65 |
frodo |
2 |
va_start(ap,format); |
66 |
frodo |
61 |
|
67 |
|
|
if (psiconv_verbosity >= PSICONV_VERB_WARN) { |
68 |
|
|
snprintf(buffer,MAX_MESSAGE,"WARNING (offset %08x): ",off); |
69 |
|
|
curlen = strlen(buffer); |
70 |
|
|
|
71 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
72 |
|
|
psiconv_error_handler(PSICONV_VERB_WARN,off,buffer); |
73 |
frodo |
2 |
} |
74 |
|
|
va_end(ap); |
75 |
|
|
} |
76 |
|
|
|
77 |
frodo |
61 |
void psiconv_progress(int level, psiconv_u32 off, const char *format,...) |
78 |
frodo |
2 |
{ |
79 |
frodo |
61 |
char buffer[MAX_MESSAGE]; |
80 |
frodo |
2 |
va_list ap; |
81 |
frodo |
61 |
size_t curlen; |
82 |
|
|
int i; |
83 |
|
|
|
84 |
frodo |
2 |
va_start(ap,format); |
85 |
frodo |
61 |
if (psiconv_verbosity >= PSICONV_VERB_PROGRESS) { |
86 |
|
|
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
87 |
|
|
curlen = strlen(buffer); |
88 |
|
|
|
89 |
|
|
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
90 |
|
|
buffer[i+curlen] = '='; |
91 |
|
|
curlen += i; |
92 |
|
|
|
93 |
|
|
buffer[curlen] = '>'; |
94 |
|
|
buffer[curlen+1] = ' '; |
95 |
|
|
buffer[curlen+2] = '\0'; |
96 |
|
|
curlen += 2; |
97 |
|
|
|
98 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
99 |
|
|
|
100 |
|
|
psiconv_error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
101 |
frodo |
2 |
} |
102 |
|
|
va_end(ap); |
103 |
|
|
} |
104 |
|
|
|
105 |
frodo |
61 |
|
106 |
frodo |
2 |
void psiconv_debug(int level, psiconv_u32 off, const char *format,...) |
107 |
|
|
{ |
108 |
frodo |
61 |
char buffer[MAX_MESSAGE]; |
109 |
|
|
va_list ap; |
110 |
|
|
size_t curlen; |
111 |
frodo |
2 |
int i; |
112 |
frodo |
61 |
|
113 |
frodo |
2 |
va_start(ap,format); |
114 |
|
|
if (psiconv_verbosity >= PSICONV_VERB_DEBUG) { |
115 |
frodo |
61 |
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
116 |
|
|
curlen = strlen(buffer); |
117 |
|
|
|
118 |
|
|
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
119 |
|
|
buffer[i+curlen] = '-'; |
120 |
|
|
curlen += i; |
121 |
|
|
|
122 |
|
|
buffer[curlen] = '>'; |
123 |
|
|
buffer[curlen+1] = ' '; |
124 |
|
|
buffer[curlen+2] = '\0'; |
125 |
|
|
curlen += 2; |
126 |
|
|
|
127 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
128 |
|
|
|
129 |
|
|
psiconv_error_handler(PSICONV_VERB_DEBUG,off,buffer); |
130 |
frodo |
2 |
} |
131 |
|
|
va_end(ap); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
char *psiconv_make_printable(const char *s) |
135 |
|
|
{ |
136 |
|
|
int i; |
137 |
|
|
char *res = malloc(strlen(s) + 1); |
138 |
|
|
for (i = 0; i < strlen(s); i ++) |
139 |
|
|
if (s[i] < 0x20 || s[i] >= 0x7f) |
140 |
|
|
res[i] = '.'; |
141 |
|
|
else |
142 |
|
|
res[i] = s[i]; |
143 |
|
|
res[strlen(s)] = 0; |
144 |
|
|
return res; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
|