1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * locate.cpp
|
---|
4 | * Copyright (C) 1996
|
---|
5 | *
|
---|
6 | * A component of the fnord webserver written by [email protected].
|
---|
7 | *
|
---|
8 | * Altered for use with the Greenstone digital library software by the
|
---|
9 | * New Zealand Digital Library Project at the University of Waikato,
|
---|
10 | * New Zealand.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | #include "text_t.h"
|
---|
29 | #include <windows.h>
|
---|
30 | #include <stdio.h>
|
---|
31 | #include "locate.h"
|
---|
32 | #include "settings.h"
|
---|
33 |
|
---|
34 | /* The following values are inserted or used by FNORD.CPP */
|
---|
35 | HWND GSDL_Window;
|
---|
36 | int line_spacing;
|
---|
37 | RECT text_rect;
|
---|
38 |
|
---|
39 |
|
---|
40 | static FILE *log_file=NULL;
|
---|
41 |
|
---|
42 | static int location_found=0;
|
---|
43 | text_t data_location;
|
---|
44 |
|
---|
45 | void set_location(text_t possible)
|
---|
46 | {
|
---|
47 | if (location_found != 0) return;
|
---|
48 | if (!possible.empty()) {
|
---|
49 | data_location = possible;
|
---|
50 | if (*(data_location.end()-1) != '\\') {
|
---|
51 | data_location.push_back('\\');
|
---|
52 | }
|
---|
53 | location_found = 1;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | void find_location(void)
|
---|
58 | {
|
---|
59 | if (location_found != 0) return;
|
---|
60 |
|
---|
61 | char here[MAX_FILENAME_SIZE];
|
---|
62 | char *send;
|
---|
63 |
|
---|
64 | if (GetModuleFileName(NULL,here,MAX_FILENAME_SIZE) <= 0) {
|
---|
65 | // failed
|
---|
66 | data_location.clear();
|
---|
67 | } else {
|
---|
68 | send = strrchr(here,'\\');
|
---|
69 | if (send != NULL) {
|
---|
70 | ++send;
|
---|
71 | } else {
|
---|
72 | send = strchr(here,':');
|
---|
73 | if (send != NULL) {
|
---|
74 | ++send;
|
---|
75 | } else {
|
---|
76 | send = here;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | *send = 0;
|
---|
81 | data_location.setcstr(here);
|
---|
82 | location_found = 1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | void open_log_file()
|
---|
86 | {
|
---|
87 | if (log_file != NULL) fclose(log_file);
|
---|
88 | log_file = fopen(gsdl_log_name, "a");
|
---|
89 | if (log_file != NULL) {
|
---|
90 | fprintf (log_file, "\n\n-----------\n");
|
---|
91 | fprintf (log_file, "log started\n");
|
---|
92 | fprintf (log_file, "-----------\n\n");
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | void close_log_file()
|
---|
97 | {
|
---|
98 | if (log_file != NULL) {
|
---|
99 | fclose(log_file);
|
---|
100 | log_file = NULL;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | #define CONSOLE_BUFFER_SIZE 2048
|
---|
105 | char console_buffer[CONSOLE_BUFFER_SIZE];
|
---|
106 | char *next_console, *last_console;
|
---|
107 | int console_top, console_y;
|
---|
108 |
|
---|
109 | void activate_console(void)
|
---|
110 | {
|
---|
111 | gsdl_show_console = 1;
|
---|
112 | next_console = console_buffer;
|
---|
113 | *next_console = 0;
|
---|
114 | last_console = next_console;
|
---|
115 | console_y = console_top = text_rect.top +
|
---|
116 | (text_rect.bottom - text_rect.top + 1) % line_spacing / 2;
|
---|
117 | InvalidateRect(GSDL_Window,NULL,TRUE);
|
---|
118 | }
|
---|
119 |
|
---|
120 | void deactivate_console(void)
|
---|
121 | {
|
---|
122 | gsdl_show_console = 0;
|
---|
123 | InvalidateRect(GSDL_Window,NULL,TRUE);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void refresh_console(HDC dc)
|
---|
127 | {
|
---|
128 | int loc; char *b, *e;
|
---|
129 | loc = console_top;
|
---|
130 | for (e = console_buffer;;) {
|
---|
131 | b = e;
|
---|
132 | while (*e >= ' ') ++e;
|
---|
133 | TextOut(dc,0,loc,b,e-b);
|
---|
134 | loc += line_spacing;
|
---|
135 | if (*e == 0) break;
|
---|
136 | ++e;
|
---|
137 | if (*e == 0) break;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | void scroll(HDC dc, int nbits)
|
---|
142 | {
|
---|
143 | RECT scroll, clip, update;
|
---|
144 |
|
---|
145 | scroll.left = clip.left = text_rect.left;
|
---|
146 | scroll.right = clip.right = text_rect.right;
|
---|
147 | clip.top = console_top;
|
---|
148 | clip.bottom = text_rect.bottom;
|
---|
149 | scroll.top = clip.top + nbits;
|
---|
150 | scroll.bottom = text_rect.bottom;
|
---|
151 | ScrollDC(dc,0,-nbits,&scroll,&clip,NULL,&update);
|
---|
152 | FillRect(dc,&update, (HBRUSH)GetStockObject(WHITE_BRUSH));
|
---|
153 | }
|
---|
154 |
|
---|
155 | int remove_line()
|
---|
156 | {
|
---|
157 | char *bin, *bout;
|
---|
158 | bin = bout = console_buffer;
|
---|
159 | while (*bin >= ' ') ++bin;
|
---|
160 | if (*bin != 0) {
|
---|
161 | ++bin;
|
---|
162 | if (*bin != 0) {
|
---|
163 | while (*bin != 0) *bout++ = *bin++;
|
---|
164 | *bout = *bin;
|
---|
165 | last_console -= (bin - bout);
|
---|
166 | next_console = bout;
|
---|
167 | return line_spacing;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | next_console = console_buffer;
|
---|
171 | *next_console = 0;
|
---|
172 | last_console = next_console;
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 | void display_line(HDC dc, char *line, int nch)
|
---|
177 | {
|
---|
178 | int scroll_needed = 0;
|
---|
179 | while (next_console + nch + 1
|
---|
180 | > console_buffer + CONSOLE_BUFFER_SIZE)
|
---|
181 | scroll_needed += remove_line();
|
---|
182 | console_y -= scroll_needed;
|
---|
183 | if (*last_console == '\n') {
|
---|
184 | console_y += line_spacing;
|
---|
185 | if (console_y + line_spacing > text_rect.bottom) {
|
---|
186 | scroll_needed += remove_line();
|
---|
187 | console_y -= line_spacing;
|
---|
188 | }
|
---|
189 | last_console = next_console;
|
---|
190 | }
|
---|
191 | if (scroll_needed != 0) scroll(dc, scroll_needed);
|
---|
192 | while (nch-- > 0) *next_console++ = *line++;
|
---|
193 | TextOut(dc,0,console_y,last_console,next_console-last_console);
|
---|
194 | *next_console = *line;
|
---|
195 | if (*next_console != 0) {
|
---|
196 | last_console = next_console;
|
---|
197 | ++next_console;
|
---|
198 | *next_console = 0;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | void display_text(HDC dc, char *buffer)
|
---|
203 | {
|
---|
204 | char *bl, *bp;
|
---|
205 | for (bl = buffer;;) {
|
---|
206 | for (bp = bl;;) {
|
---|
207 | if (*bp == '\t') *bp = ' ';
|
---|
208 | if (*bp < ' ') break;
|
---|
209 | ++bp;
|
---|
210 | }
|
---|
211 | if (*bp != 0) *bp = '\n';
|
---|
212 | display_line(dc, bl, bp-bl);
|
---|
213 | if (*bp == 0) break;
|
---|
214 | bl = bp+1;
|
---|
215 | if (*bl == 0) break;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | void log_message(char *msg)
|
---|
220 | {
|
---|
221 | if (log_file != NULL) {
|
---|
222 | fprintf(log_file,"%s",msg);
|
---|
223 | fflush(log_file);
|
---|
224 | }
|
---|
225 | if (gsdl_show_console) {
|
---|
226 | char buffer[1024]; HDC dc;
|
---|
227 | sprintf(buffer,"%s",msg);
|
---|
228 | dc = GetDC(GSDL_Window);
|
---|
229 | display_text(dc,buffer);
|
---|
230 | ReleaseDC(GSDL_Window,dc);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | void log_message_N(char*msg, int n)
|
---|
235 | {
|
---|
236 | if (log_file != NULL) {
|
---|
237 | int len = 0;
|
---|
238 | while (len < n) {
|
---|
239 | fputc (msg[len], log_file);
|
---|
240 | ++len;
|
---|
241 | }
|
---|
242 | fflush(log_file);
|
---|
243 | }
|
---|
244 |
|
---|
245 | // these messages are not send to the console
|
---|
246 | if (gsdl_show_console) {
|
---|
247 | HDC dc;
|
---|
248 | dc = GetDC(GSDL_Window);
|
---|
249 | display_text(dc,"message only written to log file\n");
|
---|
250 | ReleaseDC(GSDL_Window,dc);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | void LogCriticalError(char *cderr, char *Msg)
|
---|
255 | {
|
---|
256 | char MsgBoxStr[200];
|
---|
257 |
|
---|
258 | strcpy(MsgBoxStr, "Critical Error Number ");
|
---|
259 | strcat(MsgBoxStr, cderr);
|
---|
260 | strcat(MsgBoxStr, "\n");
|
---|
261 | strcat(MsgBoxStr, Msg);
|
---|
262 | strcat(MsgBoxStr, "\nGreenstone Digital Library software\nshutting down");
|
---|
263 | MessageBox(GSDL_Window, MsgBoxStr, "Greenstone Digital Library Software",
|
---|
264 | MB_OK | MB_ICONERROR);
|
---|
265 | PostMessage(GSDL_Window, WM_DESTROY, 0, 0);
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | /* returns second-first taking into account wrap around */
|
---|
270 | DWORD DiffTickCounts (DWORD first, DWORD second)
|
---|
271 | {
|
---|
272 | if (second >= first) return (second-first);
|
---|
273 | return (MAXDWORD-first+1+second);
|
---|
274 | }
|
---|