home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_02 / 8n02031a < prev    next >
Text File  |  1990-03-01  |  2KB  |  104 lines

  1. LISTING 4
  2.  
  3. #include <stdio.h>
  4. #include <stat.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <Mrm/MrmAppl.h>
  8.  
  9. extern    char    *malloc();
  10. extern    void    set_window_title();
  11.  
  12. #define    MAX_FILEN_LEN    128
  13. char    filename[MAX_FILEN_LEN];
  14. Widget    textwidget;
  15.  
  16. void
  17. create_text_widget(w, client_data, call_data)
  18.     Widget    w;
  19.     caddr_t client_data;
  20.     caddr_t call_data;
  21. {
  22.     textwidget = w;
  23. }
  24.  
  25. void
  26. load_file(w, client_data, call_data)
  27.     Widget    w;
  28.     caddr_t client_data;
  29.     XmFileSelectionBoxCallbackStruct *call_data;
  30. {
  31.     FILE    *fp;
  32.     char    *charptr;
  33.     char    *textbuf;
  34.     struct stat statbuf;
  35.  
  36.     XmStringGetLtoR(call_data->value, XmSTRING_DEFAULT_CHARSET, &charptr);
  37.     XtDestroyWidget(w);
  38.     strcpy(filename, charptr);
  39.     fp = fopen(filename, "r");
  40.     fstat(fileno(fp), &statbuf);
  41.     textbuf = malloc(statbuf.st_size);
  42.     fread(textbuf, statbuf.st_size, 1, fp);
  43.     XmTextSetString(textwidget, textbuf);
  44.     free(textbuf);
  45.     fclose(fp);
  46.     set_window_title(filename);
  47. }
  48.  
  49. void
  50. save_file(w, client_data, call_data)
  51.     Widget    w;
  52.     caddr_t client_data;
  53.     XmFileSelectionBoxCallbackStruct *call_data;
  54. {
  55.     FILE    *fp;
  56.     char    *textbuf;
  57.  
  58.     textbuf = XmTextGetString(textwidget);
  59.     fp = fopen(filename, "w");
  60.     fwrite(textbuf, strlen(textbuf), 1, fp);
  61.     fclose(fp);
  62.     XtFree(textbuf);
  63. }
  64.  
  65. void
  66. saveas_file(w, client_data, call_data)
  67.     Widget    w;
  68.     caddr_t client_data;
  69.     XmSelectionBoxCallbackStruct *call_data;
  70. {
  71.     FILE    *fp;
  72.     char    *textbuf;
  73.     char    *charptr;
  74.  
  75.     textbuf = XmTextGetString(textwidget);
  76.     XmStringGetLtoR(call_data->value, XmSTRING_DEFAULT_CHARSET, &charptr);
  77.     strcpy(filename, charptr);
  78.     fp = fopen(filename, "w");
  79.     fwrite(textbuf, strlen(textbuf), 1, fp);
  80.     fclose(fp);
  81.     XtFree(textbuf);
  82.     set_window_title(filename);
  83. }
  84.  
  85. void
  86. new_buffer(w, client_data, call_data)
  87.     Widget    w;
  88.     caddr_t client_data;
  89.     caddr_t    call_data;
  90. {
  91.     strcpy(filename, "");
  92.     XmTextSetString(textwidget, "");
  93.     set_window_title("Untitled");
  94. }
  95.  
  96. void
  97. exit_editor(w, client_data, call_data)
  98.     Widget    w;
  99.     caddr_t client_data;
  100.     caddr_t    call_data;
  101. {
  102.     exit(0);
  103. }
  104.