2
* @OPENGROUP_COPYRIGHT@
4
* Copyright (c) 1990, 1991, 1992, 1993 Open Software Foundation, Inc.
5
* Copyright (c) 1996, 1997, 1998, 1999, 2000 The Open Group
6
* ALL RIGHTS RESERVED (MOTIF). See the file named COPYRIGHT.MOTIF for
7
* the full copyright text.
9
* This software is subject to an open license. It may only be
10
* used on, with or for operating systems which are themselves open
11
* source systems. You must contact The Open Group for a license
12
* allowing distribution and sublicensing of this software on, with,
13
* or for operating systems which are not Open Source programs.
15
* See http://www.opengroup.org/openmotif/license for full
16
* details of the license agreement. Any use, reproduction, or
17
* distribution of the program constitutes recipient's acceptance of
20
* EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS
21
* PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22
* KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
23
* WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY
24
* OR FITNESS FOR A PARTICULAR PURPOSE
26
* EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT
27
* NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT,
28
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED
30
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32
* ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE
33
* EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
34
* POSSIBILITY OF SUCH DAMAGES.
42
static char *rcsid = "$XConsortium: io.c /main/6 1995/07/14 09:46:23 drk $";
49
#include <Xm/MessageB.h>
50
#include <Xm/Notebook.h>
56
Widget ioerror = (Widget) NULL;
58
extern Widget notebook, textw, shell;
61
extern int currentPage;
66
/* Pages are stored pretty simply:
67
* each page starts with "*PLabel"
68
* the next line has "*TLabel" if there is a major tab
69
* or with *MLabel for a minor tab
70
* *Cnnnn has the cursor position
71
* *Lnnnn has the top line position
72
* the page's text continues to the next page start.
73
* regular lines start with .
76
void ParseNewLines(label)
79
/* look for "\n" and change in '\n' and compact */
84
if ((*label == '\\') && (*(label+1) == 'n')) {
97
static void PrintWithNewLines(output, label)
101
/* look for '\n' and print "\n" */
104
if (*label == '\n') {
105
fprintf(output,"\\n");
107
fprintf(output,"%c", *label);
110
fprintf(output,"\n"); label ++ ;
114
ReadDB(char* filename)
117
int i, number, first = 1;
124
input = fopen(filename, "r");
126
if (ioerror == (Widget) NULL) {
127
ioerror = XmCreateInformationDialog(shell, "IO_Error_Dialog", NULL, 0);
130
if (input == NULL && strncmp(filename,"untitled",8) != 0) {
134
sprintf(buf, "Can't access (%s) for reading", filename);
135
str = XmStringCreateLocalized(buf);
136
XtVaSetValues(ioerror, XmNmessageString, str, NULL, NULL);
137
XtManageChild(ioerror);
140
/* Destroy current pages on reread */
141
for(i = 0; i < maxpages; i++) {
142
XtFree(pages[i] -> page);
143
XtFree(pages[i] -> majorTab);
144
XtFree(pages[i] -> minorTab);
145
XtFree(pages[i] -> label);
146
if (pages[i] -> majorPB)
147
XtDestroyWidget(pages[i] -> majorPB);
148
if (pages[i] -> minorPB)
149
XtDestroyWidget(pages[i] -> minorPB);
157
buffer = (char*) XtMalloc(max);
158
buffer[0] = 0; /* Reset page buffer */
160
pages[0] = AllocPage();
162
while(fgets(line, 1024, input) != NULL) {
163
if (line[0] == '*') /* Special */
165
if (line[1] == 'P') /* New Page */
171
pages[number] -> page = buffer;
174
buffer = (char*) XtMalloc(max);
175
buffer[0] = 0; /* Reset page buffer */
177
pages[number] = AllocPage();
179
if (strlen(line) > 3) {
180
line[strlen(line) - 1] = 0; /* Remove newline */
181
pages[number] -> label = XtNewString(&line[2]);
184
else if (line[1] == 'T') /* Tab */
187
line[strlen(line) - 1] = 0; /* Remove newline */
188
if (strlen(line) > 3) {
189
pages[number] -> majorTab = XtNewString(&line[2]);
191
ParseNewLines(pages[number] -> majorTab);
192
name = XmStringGenerate(pages[number] -> majorTab, NULL,
193
XmCHARSET_TEXT, NULL);
194
XtSetArg(args[i], XmNnotebookChildType, XmMAJOR_TAB); i++;
195
XtSetArg(args[i], XmNpageNumber, (number + 1)); i++;
196
XtSetArg(args[i], ExmNcompoundString, name); i++;
197
XtSetArg(args[i], XmNshadowThickness, 1); i++;
198
tab = ExmCreateTabButton(notebook, "atab", args, i);
200
pages[number] -> majorPB = tab;
204
else if (line[1] == 'M') /* Minor Tab */
207
line[strlen(line) - 1] = 0; /* Remove newline */
208
if (strlen(line) > 3) {
209
pages[number] -> minorTab = XtNewString(&line[2]);
211
ParseNewLines(pages[number] -> minorTab);
212
name = XmStringGenerate(pages[number] -> minorTab, NULL,
213
XmCHARSET_TEXT, NULL);
214
XtSetArg(args[i], XmNnotebookChildType, XmMINOR_TAB); i++;
215
XtSetArg(args[i], XmNpageNumber, (number + 1)); i++;
216
XtSetArg(args[i], ExmNcompoundString, name); i++;
217
XtSetArg(args[i], XmNshadowThickness, 1); i++;
218
tab = ExmCreateTabButton(notebook, "atab", args, i);
219
pages[number] -> minorPB = tab;
224
else if (line[1] == 'C') /* Cursor position */
226
pages[number] -> lastcursorpos = strtol(&line[2], NULL, 0);
228
else if (line[1] == 'L') /* Top line position */
230
pages[number] -> lasttoppos = strtol(&line[2], NULL, 0);
233
else /* Regular line. "Remove" . and append */
235
current += strlen(&line[1]);
236
if ((current - 2) > max) {
238
buffer=(char*) XtRealloc(buffer, max);
240
strcat(buffer, &line[1]);
245
/* If we didn't have a file to read, we need to setup a page */
248
pages[0] = AllocPage();
249
pages[0] -> page = XtMalloc(2);
250
pages[0] -> page[0] = 0;
252
pages[number] -> page = buffer;
258
XtSetArg(args[i], XmNlastPageNumber, maxpages + 1); i++;
259
XtSetArg(args[i], XmNcurrentPageNumber, 1); i++;
260
XtSetValues(notebook, args, i);
262
if (input) fclose(input);
266
SaveDB(char* filename)
271
char oldfilename[256];
273
if (ioerror == (Widget) NULL) {
274
ioerror = XmCreateInformationDialog(shell, "IO_Error_Dialog", NULL, 0);
277
if (access(filename, F_OK) == 0 &&
278
access(filename, W_OK) != 0) {
282
sprintf(buf, "Can't access (%s) for writing", filename);
283
str = XmStringCreateLocalized(buf);
284
XtVaSetValues(ioerror, XmNmessageString, str, NULL, NULL);
285
XtManageChild(ioerror);
289
/* Append a ~ to make the old filename */
290
if (access(filename, F_OK) == 0) {
291
strcpy(oldfilename, filename);
292
strcat(oldfilename, "~");
293
rename(filename, oldfilename);
296
/* Make sure to grab current page */
298
if (pages[currentPage] -> page != NULL)
299
XtFree(pages[currentPage] -> page);
300
pages[currentPage] -> page = XmTextGetString(textw);
303
output = fopen(filename, "w");
304
for(number = 0; number <= maxpages; number++) {
305
if (pages[number] -> label != NULL)
306
fprintf(output, "*P%s\n", pages[number] -> label);
308
fprintf(output, "*P\n");
309
if (pages[number] -> majorTab != NULL) {
310
fprintf(output, "*T");
311
PrintWithNewLines(output, pages[number] -> majorTab);
313
if (pages[number] -> minorTab != NULL) {
314
fprintf(output, "*M");
315
PrintWithNewLines(output, pages[number] -> minorTab);
317
fprintf(output, "*C%d\n", pages[number] -> lastcursorpos);
318
fprintf(output, "*L%d\n", pages[number] -> lasttoppos);
320
if (pages[number] -> page != NULL) {
321
for(i = 0; pages[number] -> page[i] != 0; i++) {
322
fputc(pages[number] -> page[i], output);
323
if (pages[number] -> page[i] == '\n')
336
p = (Page) XtMalloc(sizeof(PageRec));
339
p -> minorTab = NULL;
340
p -> majorTab = NULL;
341
p -> minorPB = (Widget) 0;
342
p -> majorPB = (Widget) 0;
344
p -> lastcursorpos = 0;