source: trunk/gsdl/src/w32server/locate.cpp@ 2286

Last change on this file since 2286 was 2286, checked in by sjboddie, 23 years ago

Had a bit of a tidy up in the fnord webserver code. The main change of note
was the removal of our reliance on the MAX_URL_SIZE constant. URLs and post
data of any length should now work.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
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 */
35HWND GSDL_Window;
36int line_spacing;
37RECT text_rect;
38
39
40static FILE *log_file=NULL;
41
42static int location_found=0;
43char data_location[MAX_FILENAME_SIZE];
44
45void set_location(char *possible)
46{
47 if (location_found != 0) return;
48 if (possible != NULL && *possible != 0) {
49 strcpy(data_location, possible);
50 if (data_location[strlen(data_location)-1] != '\\')
51 strcat(data_location,"\\");
52 location_found = 1;
53 }
54}
55
56void find_location(void)
57{
58 char *send;
59 if (location_found != 0) return;
60 if (GetModuleFileName(NULL,data_location,MAX_FILENAME_SIZE) <= 0)
61 send = data_location;
62 else {
63 send = strrchr(data_location,'\\');
64 if (send != NULL)
65 send++;
66 else {
67 send = strchr(data_location,':');
68 if (send != NULL)
69 send++;
70 else
71 send == data_location;
72 }
73 }
74 *send = 0;
75 location_found = 1;
76}
77
78void open_log_file()
79{
80 if (log_file != NULL) fclose(log_file);
81 log_file = fopen(gsdl_log_name, "a");
82 if (log_file != NULL) {
83 fprintf (log_file, "\n\n-----------\n");
84 fprintf (log_file, "log started\n");
85 fprintf (log_file, "-----------\n\n");
86 }
87}
88
89void close_log_file()
90{
91 if (log_file != NULL) {
92 fclose(log_file);
93 log_file = NULL;
94 }
95}
96
97#define CONSOLE_BUFFER_SIZE 2048
98char console_buffer[CONSOLE_BUFFER_SIZE];
99char *next_console, *last_console;
100int console_top, console_y;
101
102void activate_console(void)
103{
104 gsdl_show_console = 1;
105 next_console = console_buffer;
106 *next_console = 0;
107 last_console = next_console;
108 console_y = console_top = text_rect.top +
109 (text_rect.bottom - text_rect.top + 1) % line_spacing / 2;
110 InvalidateRect(GSDL_Window,NULL,TRUE);
111}
112
113void deactivate_console(void)
114{
115 gsdl_show_console = 0;
116 InvalidateRect(GSDL_Window,NULL,TRUE);
117}
118
119void refresh_console(HDC dc)
120{
121 int loc; char *b, *e;
122 loc = console_top;
123 for (e = console_buffer;;) {
124 b = e;
125 while (*e >= ' ') e++;
126 TextOut(dc,0,loc,b,e-b);
127 loc += line_spacing;
128 if (*e == 0) break;
129 e++;
130 if (*e == 0) break;
131 }
132}
133
134void scroll(HDC dc, int nbits)
135{
136 RECT scroll, clip, update;
137
138 scroll.left = clip.left = text_rect.left;
139 scroll.right = clip.right = text_rect.right;
140 clip.top = console_top;
141 clip.bottom = text_rect.bottom;
142 scroll.top = clip.top + nbits;
143 scroll.bottom = text_rect.bottom;
144 ScrollDC(dc,0,-nbits,&scroll,&clip,NULL,&update);
145 FillRect(dc,&update, (HBRUSH)GetStockObject(WHITE_BRUSH));
146}
147
148int remove_line()
149{
150 char *bin, *bout;
151 bin = bout = console_buffer;
152 while (*bin >= ' ') bin++;
153 if (*bin != 0) {
154 bin++;
155 if (*bin != 0) {
156 while (*bin != 0) *bout++ = *bin++;
157 *bout = *bin;
158 last_console -= (bin - bout);
159 next_console = bout;
160 return line_spacing;
161 }
162 }
163 next_console = console_buffer;
164 *next_console = 0;
165 last_console = next_console;
166 return 0;
167}
168
169void display_line(HDC dc, char *line, int nch)
170{
171 int scroll_needed = 0;
172 while (next_console + nch + 1
173 > console_buffer + CONSOLE_BUFFER_SIZE)
174 scroll_needed += remove_line();
175 console_y -= scroll_needed;
176 if (*last_console == '\n') {
177 console_y += line_spacing;
178 if (console_y + line_spacing > text_rect.bottom) {
179 scroll_needed += remove_line();
180 console_y -= line_spacing;
181 }
182 last_console = next_console;
183 }
184 if (scroll_needed != 0) scroll(dc, scroll_needed);
185 while (nch-- > 0) *next_console++ = *line++;
186 TextOut(dc,0,console_y,last_console,next_console-last_console);
187 *next_console = *line;
188 if (*next_console != 0) {
189 last_console = next_console;
190 next_console++;
191 *next_console = 0;
192 }
193}
194
195void display_text(HDC dc, char *buffer)
196{
197 char *bl, *bp;
198 for (bl = buffer;;) {
199 for (bp = bl;;) {
200 if (*bp == '\t') *bp = ' ';
201 if (*bp < ' ') break;
202 bp++;
203 }
204 if (*bp != 0) *bp = '\n';
205 display_line(dc, bl, bp-bl);
206 if (*bp == 0) break;
207 bl = bp+1;
208 if (*bl == 0) break;
209 }
210}
211
212void log_message(char *msg)
213{
214 if (log_file != NULL) {
215 fprintf(log_file,"%s",msg);
216 fflush(log_file);
217 }
218 if (gsdl_show_console) {
219 char buffer[1024]; HDC dc;
220 sprintf(buffer,"%s",msg);
221 dc = GetDC(GSDL_Window);
222 display_text(dc,buffer);
223 ReleaseDC(GSDL_Window,dc);
224 }
225}
226
227void log_message_N(char*msg, int n)
228{
229 if (log_file != NULL) {
230 int len = 0;
231 while (len < n) {
232 fputc (msg[len], log_file);
233 len++;
234 }
235 fflush(log_file);
236 }
237
238 // these messages are not send to the console
239 if (gsdl_show_console) {
240 HDC dc;
241 dc = GetDC(GSDL_Window);
242 display_text(dc,"message only written to log file\n");
243 ReleaseDC(GSDL_Window,dc);
244 }
245}
246
247void LogCriticalError(char *cderr, char *Msg)
248{
249 char MsgBoxStr[200];
250
251 strcpy(MsgBoxStr, "Critical Error Number ");
252 strcat(MsgBoxStr, cderr);
253 strcat(MsgBoxStr, "\n");
254 strcat(MsgBoxStr, Msg);
255 strcat(MsgBoxStr, "\nGreenstone Digital Library software\nshutting down");
256 MessageBox(GSDL_Window, MsgBoxStr, "Greenstone Digital Library Software",
257 MB_OK | MB_ICONERROR);
258 PostMessage(GSDL_Window, WM_DESTROY, 0, 0);
259}
260
261
262/* returns second-first taking into account wrap around */
263DWORD DiffTickCounts (DWORD first, DWORD second)
264{
265 if (second >= first) return (second-first);
266 return (MAXDWORD-first+1+second);
267}
Note: See TracBrowser for help on using the repository browser.