-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLManager_utils.c
408 lines (371 loc) · 15.1 KB
/
SQLManager_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/******************************************************************/
/* Support utilities file for SQL Manager */
/* $VER: SQLManager_utils.c 1.0 (02.07.2010) ©2010 Andrea Palmatè */
/******************************************************************/
#include <proto/exec.h>
#include <proto/chooser.h>
#include <proto/listbrowser.h>
#include <proto/dos.h>
#include <proto/asl.h>
#include <proto/intuition.h>
#include <proto/texteditor.h>
#include <gadgets/texteditor.h>
#include <classes/requester.h>
#include <string.h>
#include "SQLManager.h"
#include "SQLManager_utils.h"
#include "SQLManager_glue.h"
#include "SQLManager_cat.h"
#include "debug.h"
extern char winTitle[255];
/******************************************************************************/
/* */
/* This function remove all nodes from a chooser list */
/* */
/* Parameters: */
/* struct List *chooserlist - Pointer to the list to be freed */
/* */
/* Return Value: */
/* NONE */
/* */
/* */
/******************************************************************************/
void FreeChooserLabels( struct List *chooserlist )
{
if (chooserlist)
{
struct Node *node;
while(( node = IExec->RemHead( chooserlist )))
{
IChooser->FreeChooserNode( node );
node = NULL;
}
chooserlist = NULL;
}
}
/******************************************************************************/
/* */
/* This function remove all allocated nodes from a ListBrowser */
/* */
/* Parameters: */
/* struct List *browserlist - Pointer to the list to be freed */
/* */
/* Return Value: */
/* NONE */
/* */
/* */
/******************************************************************************/
void FreeBrowserNodes( struct List *browserlist )
{
if (browserlist)
{
struct Node *node;
while(( node = IExec->RemHead( browserlist )))
{
IListBrowser->FreeListBrowserNode( node );
node = NULL;
}
IListBrowser->FreeListBrowserList( browserlist );
browserlist = NULL;
}
}
/******************************************************************************/
/* */
/* This function Sleep/Wake a window */
/* */
/* Parameters: */
/* struct Window *tempWindow - Pointer to the window to sleep/awake */
/* BOOL state - the new state of the window */
/* TRUE - Put the window asleep */
/* FALSE - Put the window awake */
/* */
/* Return Value: */
/* NONE */
/* */
/* */
/******************************************************************************/
void SleepWindow(struct Window *tempWindow, BOOL state)
{
struct Requester dummyRequester;
if (state == TRUE)
{
IIntuition->InitRequester(&dummyRequester);
IIntuition->Request(&dummyRequester, tempWindow);
IIntuition->SetWindowPointer(tempWindow, WA_BusyPointer, TRUE, WA_PointerDelay, TRUE, TAG_DONE);
}
else
{
IIntuition->SetWindowPointer(tempWindow, WA_BusyPointer, FALSE, TAG_DONE);
IIntuition->EndRequest(&dummyRequester, tempWindow);
}
}
/******************************************************************************/
/* */
/* This function test if a file exists */
/* */
/* Parameters: */
/* CONST_STRPTR filename - The file to check */
/* */
/* Return Value: */
/* TRUE if the file exists */
/* FALSE if not */
/* */
/******************************************************************************/
BOOL FileExists(CONST_STRPTR filename)
{
BPTR f;
f = IDOS->Lock(filename, SHARED_LOCK);
if (f)
{
IDOS->UnLock(f);
return TRUE;
}
else
return FALSE;
}
/******************************************************************************/
/* */
/* This function load a file from the disk and show it in a texteditor.gadget */
/* */
/* Parameters: */
/* struct Gadget *TE - The texteditor.gadget */
/* struct Window *win - The window where the gadget is */
/* CONST_STRPTR filename - The file to load into the texteditor */
/* */
/* Return Value: */
/* 0 if success */
/* a non 0 value if an error occurs */
/* */
/******************************************************************************/
LONG ReadTEFile(struct Gadget *TE, struct Window *win, CONST_STRPTR filename)
{
BPTR f;
LONG filesize;
ULONG len;
char *buffer;
LONG error = 0;
struct ExamineData *edata = NULL;
/* Open the file */
f = IDOS->Open(filename, MODE_OLDFILE);
if (f)
{
/* Get the file size */
if (( edata = IDOS->ExamineObjectTags(EX_FileHandle, f, TAG_DONE) ))
{
/* if the file is less than 2GB.. */
if (edata->FileSize < 0x7ffffffeLL)
{
filesize = (LONG) (0xFFFFFFFFLL & edata->FileSize);
D(bug("Filesize: %ld\n", filesize));
/* Allocate the memory for the file */
buffer = IExec->AllocVecTags(filesize + 1,
AVT_Type, MEMF_SHARED,
TAG_DONE);
if (buffer)
{
/* Read the file */
len = IDOS->Read(f, buffer, filesize);
D(bug("len=%ld\n",len));
if (len!=filesize)
{
error = IDOS->IoErr();
D(bug("error:%ld\n",error));
}
/*
if (len>filesize);
{
len = 0L;
D(bug("error: len=%ld\n",len));
}
*/
buffer[len] = '\0';
/* Put the file content into the texteditor */
if (IIntuition->SetGadgetAttrs(TE, win, NULL,
GA_TEXTEDITOR_Contents, (ULONG) buffer,
GA_TEXTEDITOR_Length, len))
{
IIntuition->RefreshGList(TE, win, NULL, 1L);
}
IExec->FreeVec(buffer); /* Free the buffer */
buffer = NULL;
/* Let's go to the first texteditor character */
IIntuition->SetGadgetAttrs(TE, win, NULL, GA_TEXTEDITOR_CursorX, 0, GA_TEXTEDITOR_CursorY, 0, TAG_END);
}
else
{
error = SQLMGR_ERROR_NOMEM;
D(bug("Cannot allocate memory for the file!\n"));
}
}
else
{
error = SQLMGR_ERROR_FILETOOLARGE;
D(bug("File is greater than 2GB!\n"));
}
IDOS->FreeDosObject(DOS_EXAMINEDATA, edata);
}
else
D(bug("File is empty!\n"));
IDOS->Close(f);
}
return error;
}
/******************************************************************************/
/* */
/* This function replace all occurence of a pattern in a string */
/* */
/* Parameters: */
/* STRPTR st - The original string */
/* CONST_STRPTR orig - The pattern to change */
/* CONST_STRPTR repl - The new pattern */
/* */
/* Return Value: */
/* the new string */
/* */
/******************************************************************************/
STRPTR Replace(STRPTR st, CONST_STRPTR orig, CONST_STRPTR repl)
{
char *ret, *sr;
size_t i, count = 0;
size_t newlen = strlen(repl);
size_t oldlen = strlen(orig);
if (newlen != oldlen)
{
for (i = 0; st[i] != '\0'; )
{
if (memcmp(&st[i], orig, oldlen) == 0)
count++, i += oldlen;
else
i++;
}
}
else
i = strlen(st);
ret = malloc(i + 1 + count * (newlen - oldlen));
if (ret == NULL)
return NULL;
sr = ret;
while (*st)
{
if (memcmp(st, orig, oldlen) == 0)
{
memcpy(sr, repl, newlen);
sr += newlen;
st += oldlen;
}
else
*sr++ = *st++;
}
*sr = '\0';
return ret;
}
/******************************************************************************/
/* */
/* This function replace some patter in a XML string */
/* */
/* Parameters: */
/* STRPTR st - The string to be parsed */
/* */
/* Return Value: */
/* The new string */
/* */
/******************************************************************************/
STRPTR ParseXML(STRPTR st)
{
st = Replace(st,"&","&");
st = Replace(st,"<","<");
st = Replace(st,">",">");
return strdup(st);
}
/******************************************************************************/
/* */
/* This function save a file to disk */
/* */
/* Parameters: */
/* struct Gadget *TE - The texteditor.gadget */
/* struct Window *win - The window where the gadget is */
/* CONST_STRPTR filename - The filename of the file being saved */
/* */
/* Return Value: */
/* 0 if success */
/* a non 0 value if an error occurs */
/* */
/******************************************************************************/
LONG SaveTEFile(struct Gadget *TE, struct Window *win, CONST_STRPTR filename)
{
BPTR f;
ULONG error = 0;
/* Open the file */
f = IDOS->Open(filename, MODE_NEWFILE);
if (f)
{
STRPTR text = (STRPTR) IIntuition->DoGadgetMethod( TE, win, NULL, GM_TEXTEDITOR_ExportText, NULL);
if (text)
{
ULONG bytes = strlen(text);
if (IDOS->Write(f, text, bytes)!=bytes)
{
error = IDOS->IoErr();
}
IExec->FreeVec(text);
text = NULL;
}
else
error = -1;
IDOS->Close(f);
}
else
error = IDOS->IoErr();
return error;
}
/******************************************************************************/
/* */
/* This function open an ASL requester and returns a file */
/* */
/* Parameters: */
/* CONST_STRPTR fileext - The ASL file pattern */
/* */
/* Return Value: */
/* The file name if the user click on Open */
/* NULL if the user click on Cancel */
/* */
/******************************************************************************/
STRPTR ChooseFilename(CONST_STRPTR fileext)
{
struct FileRequester *FR = NULL;
char *newfile = NULL;
FR = IAsl->AllocAslRequest(ASL_FileRequest, NULL);
if (FR)
{
if ( (IAsl->AslRequestTags(FR,
ASLFR_TitleText, (ULONG) GetString(MSG_RESULTS_FILENAME),
ASLFR_RejectIcons, TRUE,
ASLFR_DoPatterns, TRUE,
ASLFR_SleepWindow, TRUE,
ASLFR_InitialPattern, (ULONG) fileext,
ASLFR_DoSaveMode, TRUE,
TAG_DONE) ) == FALSE)
{
D(bug("User has click on Cancel\n"));
}
else
{
newfile = IExec->AllocVecTags(strlen(FR->fr_Drawer) + strlen(FR->fr_File) + 1, AVT_Type, MEMF_SHARED, AVT_ClearWithValue, 0, TAG_DONE);
if (newfile)
{
D(bug("file:%s%s\n",FR->fr_Drawer,FR->fr_File));
snprintf(newfile, strlen(FR->fr_Drawer) + strlen(FR->fr_File) + 1, "%s%s",FR->fr_Drawer, FR->fr_File);
if (FileExists(newfile) == TRUE)
{
if (Requester(REQIMAGE_QUESTION, PROGRAM_TITLE, GetString(MSG_ASL_FILEEXISTS), GetString(MSG_YESNO), NULL) == 1)
{
return newfile;
}
}
else
return newfile;
}
}
}
return NULL;
}