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

Last change on this file since 1011 was 1011, checked in by sjboddie, 24 years ago

tidied up w32server

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