next up previous
Next: Copying and printing Views Up: RECView.m Program Listing Previous: Initialization and termination

Opening and closing files

There is a fairly standard collection of methods which can be used to get files for a program, or to save them afterward. The main complication consists of nesting them within the operating system, but naming files and searching for them also requires some attention.

/* - - - - - - - -   F I L E   A N D   D I R E C T O R Y   - - - - - - - - */

- setFilename:(const char *)aFilename {
if (filename) free(filename);
filename = malloc(strlen(aFilename)+1);
strcpy(filename, aFilename);
[window setTitleAsFilename:aFilename];
return self;
}

- open:sender {
  char *types[2] = {"rec",0};
  int  i, fd;
  NXStream *theStream;

  [[OpenPanel new] allowMultipleFiles: NO];
  [[OpenPanel new] chooseDirectories: NO];

  if ([ [OpenPanel new] runModalForTypes:types]) {
    [self setFilename: [[OpenPanel new] filename]];
    fd = open(filename, O_RDONLY, 06666);
    theStream = NXOpenFile(fd, NX_READONLY);
//      NXScanf(theStream,"%s",cstr);
      i=0;
      while (NXScanf(theStream,"%c",&cstr[i])!=EOF) i++;
      phil=strlen(cstr);
    NXClose(theStream);
    close(fd);
    [window setTitleAsFilename: filename];
    [window setDocEdited:NO];
  [execWin setSel: 0: [execWin textLength]];
  [execWin replaceSel: cstr];
  [execWin scrollSelToVisible];
  [execWin display];
    }

return self;
}

- saveAs:sender {
  id panel;
  const char *dir;
  char *file;

/* prompt user for the file name and save to that file */

  if (filename==0) {
    /* no filename; set up defaults */ 
    dir  = NXHomeDirectory();
    file = (char *)[window title];
    } else {
    file=rindex(filename,'/');
    if (file) {
      dir   = filename;
      *file = 0;
      file++;
      } else {
      dir  = filename;
      file = (char *)[window title];
      }
    }
  panel = [SavePanel new]; 
  [panel setRequiredFileType: ""];
  if ([panel runModalForDirectory: dir file: file]) {
    [self setFilename: [panel filename]];
    return [self save: sender];
    }
    return nil;  /* didn't save */
  }
    
- save:sender {
  int      fd;
  NXStream *theStream;

  if (filename==0) return [self saveAs: sender];
  [window setTitle: "Saving ..."];

  fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 06666);
  if (fd < 0) {
    NXRunAlertPanel(0, "Cannot save file: %s",0,0,0,strerror(errno));
    return self;
    }
  theStream = NXOpenFile(fd, NX_WRITEONLY);
  [execWin writeText: theStream];
  NXClose(theStream);
  close(fd);
  [window setTitleAsFilename: filename];
  [window setDocEdited:NO];
  return self;
}

- close:sender {const char *fname;
int         q;
  if ([window isDocEdited])
//    fname=filename ? filename : [sender title];
//    if (rindex(fname,'/')) fname = rindex(fname,'/') + 1;
    q = NXRunAlertPanel(
      "Save",
      "Save changes to %s?",
      "Save",
      "Don't Save",
      "Cancel",
      filename);
    if (q==1) {if (![self save:nil]) return nil;}
    if(q==-1) return nil;
//  [sender setDelegate: nil];
//  [proc free];
//  [self free];
  return self;
}

- windowWillClose:sender {
[self close: self];
printf("windowWillClose\n");
return self;
}



Microcomputadoras
2000-09-28