1 | #include <windows.h>
|
---|
2 | #include <stdio.h>
|
---|
3 | #include "locateP.h"
|
---|
4 | #include "locate.h"
|
---|
5 | #include "settings.h"
|
---|
6 |
|
---|
7 | /* The following values are inserted or used by FNORD.CPP */
|
---|
8 | HWND GSDL_Window;
|
---|
9 | int line_spacing;
|
---|
10 | RECT text_rect;
|
---|
11 |
|
---|
12 |
|
---|
13 | static FILE *log_file=NULL;
|
---|
14 |
|
---|
15 | static int location_found=0;
|
---|
16 | char data_location[MAX_URL_SIZE];
|
---|
17 |
|
---|
18 | void set_location(char *possible)
|
---|
19 | {
|
---|
20 | if (location_found != 0) return;
|
---|
21 | if (possible != NULL && *possible != 0) {
|
---|
22 | strcpy(data_location, possible);
|
---|
23 | if (data_location[strlen(data_location)-1] != '\\')
|
---|
24 | strcat(data_location,"\\");
|
---|
25 | location_found = 1;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | void find_location(void)
|
---|
30 | {
|
---|
31 | char *send;
|
---|
32 | if (location_found != 0) return;
|
---|
33 | if (GetModuleFileName(NULL,data_location,MAX_URL_SIZE) <= 0)
|
---|
34 | send = data_location;
|
---|
35 | else {
|
---|
36 | send = strrchr(data_location,'\\');
|
---|
37 | if (send != NULL)
|
---|
38 | send++;
|
---|
39 | else {
|
---|
40 | send = strchr(data_location,':');
|
---|
41 | if (send != NULL)
|
---|
42 | send++;
|
---|
43 | else
|
---|
44 | send == data_location;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | *send = 0;
|
---|
48 | location_found = 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | void open_log_file()
|
---|
52 | {
|
---|
53 | if (log_file != NULL) fclose(log_file);
|
---|
54 | log_file = fopen(gsdl_log_name, "a");
|
---|
55 | if (log_file != NULL) {
|
---|
56 | fprintf (log_file, "\n\n-----------\n");
|
---|
57 | fprintf (log_file, "log started\n");
|
---|
58 | fprintf (log_file, "-----------\n\n");
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | void close_log_file()
|
---|
63 | {
|
---|
64 | if (log_file != NULL) {
|
---|
65 | fclose(log_file);
|
---|
66 | log_file = NULL;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | #define CONSOLE_BUFFER_SIZE 2048
|
---|
71 | char console_buffer[CONSOLE_BUFFER_SIZE];
|
---|
72 | char *next_console, *last_console;
|
---|
73 | int console_top, console_y;
|
---|
74 |
|
---|
75 | void activate_console(void)
|
---|
76 | {
|
---|
77 | gsdl_show_console = 1;
|
---|
78 | next_console = console_buffer;
|
---|
79 | *next_console = 0;
|
---|
80 | last_console = next_console;
|
---|
81 | console_y = console_top = text_rect.top +
|
---|
82 | (text_rect.bottom - text_rect.top + 1) % line_spacing / 2;
|
---|
83 | InvalidateRect(GSDL_Window,NULL,TRUE);
|
---|
84 | }
|
---|
85 |
|
---|
86 | void deactivate_console(void)
|
---|
87 | {
|
---|
88 | gsdl_show_console = 0;
|
---|
89 | InvalidateRect(GSDL_Window,NULL,TRUE);
|
---|
90 | }
|
---|
91 |
|
---|
92 | void refresh_console(HDC dc)
|
---|
93 | {
|
---|
94 | int loc; char *b, *e;
|
---|
95 | loc = console_top;
|
---|
96 | for (e = console_buffer;;) {
|
---|
97 | b = e;
|
---|
98 | while (*e >= ' ') e++;
|
---|
99 | TextOut(dc,0,loc,b,e-b);
|
---|
100 | loc += line_spacing;
|
---|
101 | if (*e == 0) break;
|
---|
102 | e++;
|
---|
103 | if (*e == 0) break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | void scroll(HDC dc, int nbits)
|
---|
108 | {
|
---|
109 | RECT scroll, clip, update;
|
---|
110 |
|
---|
111 | scroll.left = clip.left = text_rect.left;
|
---|
112 | scroll.right = clip.right = text_rect.right;
|
---|
113 | clip.top = console_top;
|
---|
114 | clip.bottom = text_rect.bottom;
|
---|
115 | scroll.top = clip.top + nbits;
|
---|
116 | scroll.bottom = text_rect.bottom;
|
---|
117 | ScrollDC(dc,0,-nbits,&scroll,&clip,NULL,&update);
|
---|
118 | FillRect(dc,&update, GetStockObject(WHITE_BRUSH));
|
---|
119 | }
|
---|
120 |
|
---|
121 | int remove_line()
|
---|
122 | {
|
---|
123 | char *bin, *bout;
|
---|
124 | bin = bout = console_buffer;
|
---|
125 | while (*bin >= ' ') bin++;
|
---|
126 | if (*bin != 0) {
|
---|
127 | bin++;
|
---|
128 | if (*bin != 0) {
|
---|
129 | while (*bin != 0) *bout++ = *bin++;
|
---|
130 | *bout = *bin;
|
---|
131 | last_console -= (bin - bout);
|
---|
132 | next_console = bout;
|
---|
133 | return line_spacing;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | next_console = console_buffer;
|
---|
137 | *next_console = 0;
|
---|
138 | last_console = next_console;
|
---|
139 | return 0;
|
---|
140 | }
|
---|
141 |
|
---|
142 | void display_line(HDC dc, char *line, int nch)
|
---|
143 | {
|
---|
144 | int scroll_needed = 0;
|
---|
145 | while (next_console + nch + 1
|
---|
146 | > console_buffer + CONSOLE_BUFFER_SIZE)
|
---|
147 | scroll_needed += remove_line();
|
---|
148 | console_y -= scroll_needed;
|
---|
149 | if (*last_console == '\n') {
|
---|
150 | console_y += line_spacing;
|
---|
151 | if (console_y + line_spacing > text_rect.bottom) {
|
---|
152 | scroll_needed += remove_line();
|
---|
153 | console_y -= line_spacing;
|
---|
154 | }
|
---|
155 | last_console = next_console;
|
---|
156 | }
|
---|
157 | if (scroll_needed != 0) scroll(dc, scroll_needed);
|
---|
158 | while (nch-- > 0) *next_console++ = *line++;
|
---|
159 | TextOut(dc,0,console_y,last_console,next_console-last_console);
|
---|
160 | *next_console = *line;
|
---|
161 | if (*next_console != 0) {
|
---|
162 | last_console = next_console;
|
---|
163 | next_console++;
|
---|
164 | *next_console = 0;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | void display_text(HDC dc, char *buffer)
|
---|
169 | {
|
---|
170 | char *bl, *bp;
|
---|
171 | for (bl = buffer;;) {
|
---|
172 | for (bp = bl;;) {
|
---|
173 | if (*bp == '\t') *bp = ' ';
|
---|
174 | if (*bp < ' ') break;
|
---|
175 | bp++;
|
---|
176 | }
|
---|
177 | if (*bp != 0) *bp = '\n';
|
---|
178 | display_line(dc, bl, bp-bl);
|
---|
179 | if (*bp == 0) break;
|
---|
180 | bl = bp+1;
|
---|
181 | if (*bl == 0) break;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | void log_message(char *msg)
|
---|
186 | {
|
---|
187 | if (log_file != NULL) {
|
---|
188 | fprintf(log_file,"%s",msg);
|
---|
189 | fflush(log_file);
|
---|
190 | }
|
---|
191 | if (gsdl_show_console) {
|
---|
192 | char buffer[1024]; HDC dc;
|
---|
193 | sprintf(buffer,"%s",msg);
|
---|
194 | dc = GetDC(GSDL_Window);
|
---|
195 | display_text(dc,buffer);
|
---|
196 | ReleaseDC(GSDL_Window,dc);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | void log_message_N(char*msg, int n)
|
---|
201 | {
|
---|
202 | if (log_file != NULL) {
|
---|
203 | int len = 0;
|
---|
204 | while (len < n) {
|
---|
205 | fputc (msg[len], log_file);
|
---|
206 | len++;
|
---|
207 | }
|
---|
208 | fflush(log_file);
|
---|
209 | }
|
---|
210 |
|
---|
211 | // these messages are not send to the console
|
---|
212 | if (gsdl_show_console) {
|
---|
213 | HDC dc;
|
---|
214 | dc = GetDC(GSDL_Window);
|
---|
215 | display_text(dc,"message only written to log file\n");
|
---|
216 | ReleaseDC(GSDL_Window,dc);
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | void LogCriticalError(char *cderr, char *Msg)
|
---|
221 | {
|
---|
222 | char MsgBoxStr[200];
|
---|
223 |
|
---|
224 | strcpy(MsgBoxStr, "Critical Error Number ");
|
---|
225 | strcat(MsgBoxStr, cderr);
|
---|
226 | strcat(MsgBoxStr, "\n");
|
---|
227 | strcat(MsgBoxStr, Msg);
|
---|
228 | strcat(MsgBoxStr, "\nGreenstone Digital Library software\nshutting down");
|
---|
229 | MessageBox(GSDL_Window, MsgBoxStr, "Greenstone Digital Library Software",
|
---|
230 | MB_OK | MB_ICONERROR);
|
---|
231 | PostMessage(GSDL_Window, WM_DESTROY, 0, 0);
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | /* returns second-first taking into account wrap around */
|
---|
236 | DWORD DiffTickCounts (DWORD first, DWORD second)
|
---|
237 | {
|
---|
238 | if (second >= first) return (second-first);
|
---|
239 | return (MAXDWORD-first+1+second);
|
---|
240 | }
|
---|