~ubuntu-branches/ubuntu/quantal/openmotif/quantal

« back to all changes in this revision

Viewing changes to demos/programs/todo/io.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Bauer
  • Date: 2010-06-23 12:12:31 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100623121231-u89gxdp51sg9wjj2
Tags: 2.3.0-1
* New Maintainer (Closes: #379258) 
* Acknowledge NMU changes
* New upstream release (Closes: #494375)
* Get rid of security patches as they are already part of new upstream
  release (00-xpmvuln.openmotif.patch, 342092-CVE-2005-3964.patch)
* Bump Standards to 3.8.4
* Added {misc:Depends} to make the package lintian cleaner
* Fix weak-library-dev-dependency by adding ${binary:Version}) for the
  -dev Package of openmotif
* Let package depend on autotools-dev to use newer autotools-helper-files
* Work around an autoconf-bug (Gentoo-Bug #1475)
* Added Client-side anti-aliased fonts support via XFT
* Added UTF-8 and UTF8_STRING atom support
* Ability to show text and pixmaps in Label, LabelGadget and all
  derived widgets
* Support of PNG/JPEG image formats in the same way as XPM is supported
* Increase FILE_OFFSET_BITS to 64 to show files >2GB in file-selector
  Idea taken from Magne Oestlyngen (Closes: #288537)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 *  @OPENGROUP_COPYRIGHT@
 
3
 *  COPYRIGHT NOTICE
 
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.
 
8
 *  
 
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.
 
14
 *  
 
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
 
18
 *  this agreement.
 
19
 *  
 
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
 
25
 *  
 
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.
 
35
 */
 
36
/* 
 
37
 * HISTORY
 
38
 */
 
39
 
 
40
#ifdef REV_INFO
 
41
#ifndef lint
 
42
static char *rcsid = "$XConsortium: io.c /main/6 1995/07/14 09:46:23 drk $";
 
43
#endif
 
44
#endif
 
45
 
 
46
#include <stdlib.h>
 
47
#include <unistd.h>
 
48
#include <Xm/Xm.h>
 
49
#include <Xm/MessageB.h>
 
50
#include <Xm/Notebook.h>
 
51
#include <Xm/Text.h>
 
52
#include <Exm/TabB.h>
 
53
#include <stdio.h>
 
54
#include "page.h"
 
55
 
 
56
Widget ioerror = (Widget) NULL;
 
57
 
 
58
extern Widget notebook, textw, shell;
 
59
Page pages[MAXPAGES];
 
60
 
 
61
extern int currentPage;
 
62
extern int modified;
 
63
int maxpages;
 
64
Page AllocPage();
 
65
 
 
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 . 
 
74
 */
 
75
 
 
76
void ParseNewLines(label)
 
77
char * label ;
 
78
{
 
79
    /* look for "\n" and change in '\n' and compact */
 
80
    
 
81
    char * s ;
 
82
 
 
83
    while (*label) {
 
84
        if ((*label == '\\') && (*(label+1) == 'n')) {
 
85
            *label = '\n' ;
 
86
            s = label+1 ;
 
87
            while (*s) {
 
88
                *s = *(s+1) ;
 
89
                s++ ;
 
90
            }
 
91
        }
 
92
        label ++ ;
 
93
    }
 
94
 
 
95
}
 
96
 
 
97
static void PrintWithNewLines(output, label)
 
98
FILE *output ;
 
99
char * label ;
 
100
{
 
101
    /* look for '\n' and print "\n" */
 
102
    
 
103
    while (*label) {
 
104
        if (*label == '\n') {
 
105
            fprintf(output,"\\n"); 
 
106
        } else  
 
107
            fprintf(output,"%c", *label);
 
108
        label ++ ;
 
109
    }
 
110
    fprintf(output,"\n"); label ++ ;
 
111
}
 
112
 
 
113
void
 
114
ReadDB(char* filename) 
 
115
{
 
116
  FILE *input;
 
117
  int i, number, first = 1;
 
118
  char *buffer;
 
119
  int max, current;
 
120
  char line[1024];
 
121
  Arg args[5];
 
122
  Widget tab;
 
123
  
 
124
  input = fopen(filename, "r");
 
125
 
 
126
  if (ioerror == (Widget) NULL) {
 
127
    ioerror = XmCreateInformationDialog(shell, "IO_Error_Dialog", NULL, 0);
 
128
  }
 
129
 
 
130
  if (input == NULL && strncmp(filename,"untitled",8) != 0) {
 
131
    XmString str;
 
132
    char buf[256];
 
133
 
 
134
    sprintf(buf, "Can't access (%s) for reading", filename);
 
135
    str = XmStringCreateLocalized(buf);
 
136
    XtVaSetValues(ioerror, XmNmessageString, str, NULL, NULL);
 
137
    XtManageChild(ioerror);
 
138
  }
 
139
 
 
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);
 
150
  }
 
151
 
 
152
  number = 0;
 
153
 
 
154
 
 
155
  if (input != NULL) {
 
156
    max = MAXINIT;
 
157
    buffer = (char*) XtMalloc(max);
 
158
    buffer[0] = 0; /* Reset page buffer */
 
159
    current = 0;
 
160
    pages[0] = AllocPage();
 
161
    
 
162
    while(fgets(line, 1024, input) != NULL) {
 
163
      if (line[0] == '*') /* Special */
 
164
        {
 
165
          if (line[1] == 'P') /* New Page */
 
166
            {
 
167
              if (first == 1) {
 
168
                first = 0;
 
169
              }
 
170
              else {
 
171
                pages[number] -> page = buffer;
 
172
                current = 0;
 
173
                max = MAXINIT;
 
174
                buffer = (char*) XtMalloc(max);
 
175
                buffer[0] = 0; /* Reset page buffer */
 
176
                number++;
 
177
                pages[number] = AllocPage();
 
178
              }
 
179
              if (strlen(line) > 3) {
 
180
                line[strlen(line) - 1] = 0; /* Remove newline */
 
181
                pages[number] -> label = XtNewString(&line[2]);
 
182
              }
 
183
            }
 
184
          else if (line[1] == 'T') /* Tab */
 
185
            {
 
186
              XmString name;
 
187
              line[strlen(line) - 1] = 0; /* Remove newline */
 
188
              if (strlen(line) > 3) {
 
189
                pages[number] -> majorTab = XtNewString(&line[2]);
 
190
                i = 0;
 
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);
 
199
                XtManageChild(tab);
 
200
                pages[number] -> majorPB = tab;
 
201
                XmStringFree(name);
 
202
              }
 
203
            }
 
204
          else if (line[1] == 'M') /* Minor Tab */
 
205
            {
 
206
              XmString name;
 
207
              line[strlen(line) - 1] = 0; /* Remove newline */
 
208
              if (strlen(line) > 3) {
 
209
                pages[number] -> minorTab = XtNewString(&line[2]);
 
210
                i = 0;
 
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;
 
220
                XtManageChild(tab);
 
221
                XmStringFree(name);
 
222
              }
 
223
            }
 
224
          else if (line[1] == 'C') /* Cursor position */
 
225
            {
 
226
              pages[number] -> lastcursorpos = strtol(&line[2], NULL, 0);
 
227
            }
 
228
          else if (line[1] == 'L') /* Top line position */
 
229
            {
 
230
              pages[number] -> lasttoppos = strtol(&line[2], NULL, 0);
 
231
            }
 
232
        }
 
233
      else /* Regular line.  "Remove" . and append */
 
234
        {
 
235
          current += strlen(&line[1]);
 
236
          if ((current - 2) > max) {
 
237
            max = 2 * max;
 
238
            buffer=(char*) XtRealloc(buffer, max);
 
239
          }
 
240
          strcat(buffer, &line[1]);
 
241
        }
 
242
    }
 
243
  }
 
244
 
 
245
  /* If we didn't have a file to read,  we need to setup a page */
 
246
  if (input == NULL) {
 
247
    number = 0;
 
248
    pages[0] = AllocPage();
 
249
    pages[0] -> page = XtMalloc(2);
 
250
    pages[0] -> page[0] = 0;
 
251
  } else {
 
252
    pages[number] -> page = buffer;
 
253
  }
 
254
 
 
255
  maxpages = number;
 
256
 
 
257
  i = 0;
 
258
  XtSetArg(args[i], XmNlastPageNumber, maxpages + 1); i++;
 
259
  XtSetArg(args[i], XmNcurrentPageNumber, 1); i++;
 
260
  XtSetValues(notebook, args, i);
 
261
 
 
262
  if (input) fclose(input);
 
263
}
 
264
 
 
265
void 
 
266
SaveDB(char* filename)
 
267
{
 
268
  int number;
 
269
  FILE *output;
 
270
  int i;
 
271
  char oldfilename[256];
 
272
  
 
273
  if (ioerror == (Widget) NULL) {
 
274
    ioerror = XmCreateInformationDialog(shell, "IO_Error_Dialog", NULL, 0);
 
275
  }
 
276
 
 
277
  if (access(filename, F_OK) == 0 &&
 
278
      access(filename, W_OK) != 0) {
 
279
    XmString str;
 
280
    char buf[256];
 
281
 
 
282
    sprintf(buf, "Can't access (%s) for writing", filename);
 
283
    str = XmStringCreateLocalized(buf);
 
284
    XtVaSetValues(ioerror, XmNmessageString, str, NULL, NULL);
 
285
    XtManageChild(ioerror);
 
286
    return;
 
287
  }
 
288
 
 
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);
 
294
  }
 
295
 
 
296
  /* Make sure to grab current page */
 
297
  if (modified) {
 
298
    if (pages[currentPage] -> page != NULL) 
 
299
      XtFree(pages[currentPage] -> page);
 
300
    pages[currentPage] -> page = XmTextGetString(textw);
 
301
  }
 
302
 
 
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);
 
307
    else
 
308
      fprintf(output, "*P\n");
 
309
    if (pages[number] -> majorTab != NULL) {
 
310
        fprintf(output, "*T"); 
 
311
        PrintWithNewLines(output, pages[number] -> majorTab);
 
312
    }
 
313
    if (pages[number] -> minorTab != NULL) {
 
314
        fprintf(output, "*M"); 
 
315
        PrintWithNewLines(output, pages[number] -> minorTab);
 
316
    }
 
317
    fprintf(output, "*C%d\n", pages[number] -> lastcursorpos);
 
318
    fprintf(output, "*L%d\n", pages[number] -> lasttoppos);
 
319
    fputc('.', output);
 
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')
 
324
          fputc('.', output);
 
325
      }
 
326
    }
 
327
    fputc('\n', output);
 
328
  }
 
329
  fclose(output);
 
330
}
 
331
 
 
332
Page AllocPage() 
 
333
{
 
334
  Page p;
 
335
 
 
336
  p = (Page) XtMalloc(sizeof(PageRec));
 
337
  p -> page = NULL;
 
338
  p -> label = NULL;
 
339
  p -> minorTab = NULL;
 
340
  p -> majorTab = NULL;
 
341
  p -> minorPB = (Widget) 0;
 
342
  p -> majorPB = (Widget) 0;
 
343
  p -> lasttoppos = 0;
 
344
  p -> lastcursorpos = 0;
 
345
 
 
346
  return(p);
 
347
}