Skip to content

Commit

Permalink
pound: add save file
Browse files Browse the repository at this point in the history
  • Loading branch information
ivop committed Sep 10, 2024
1 parent 0e83c76 commit c02f314
Showing 1 changed file with 74 additions and 2 deletions.
76 changes: 74 additions & 2 deletions apps/pound.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,80 @@ void editorInsertChar(uint8_t c) {
editorDrawStatusMsg(OUT_OF_MEMORY_STRING);
}

// -------------------- FILE INPUT --------------------
// -------------------- FILE INPUT/OUTPUT --------------------

static inline uint8_t write_byte(FCB *fcb, uint8_t c, uint8_t *opos) {
cpm_default_dma[*opos] = c;
(*opos)++;
if (*opos == 128) {
if (cpm_write_sequential(fcb)) return 0xff;
*opos = 0;
}
return 0;
}

uint8_t write_file(FCB *fcb) {
fcb->ex = fcb->s1 = fcb->s2 = fcb->rc = 0;
if (cpm_make_file(fcb)) return 0xff;
fcb->cr = 0;

cpm_set_dma(cpm_default_dma);

uint8_t opos = 0;

for (int j=0; j<E.numrows; j++) {
for (int i=0; i<E.row[j].size; i++) {
if (write_byte(fcb, E.row[j].chars[i], &opos)) return 0xff;
}
if (write_byte(fcb, '\r', &opos)) return 0xff;
if (write_byte(fcb, '\n', &opos)) return 0xff;
}
if (write_byte(fcb, CTRL('Z'), &opos)) return 0xff;
if (opos && cpm_write_sequential(fcb)) return 0xff;

return cpm_close_file(fcb);
}

void editorSave(void) {
cpm_fcb.ex = cpm_fcb.s1 = cpm_fcb.s2 = cpm_fcb.rc = 0;

if (cpm_open_file(&cpm_fcb)) {
editorDrawStatusMsg("Saving to new file...");
if (write_file(&cpm_fcb)) editorDrawStatusMsg("Failed to save file");
return;
}

editorDrawStatusMsg("Saving to temporary file...");

static FCB tempfcb;
strcpy((char *)tempfcb.f, "POUNDTMP$$$");
tempfcb.dr = cpm_fcb.dr;
if (write_file(&tempfcb)) {
editorDrawStatusMsg("Failed to create temporary file");
return;
}

editorDrawStatusMsg("Removing old file...");
if (cpm_delete_file(&cpm_fcb)) {
editorDrawStatusMsg("Unable to delete old file...");
return;
}

editorDrawStatusMsg("Renaming temporary file...");
memcpy(((uint8_t*)&tempfcb) + 16, &cpm_fcb, 16);
if (cpm_rename_file((RCB*)&tempfcb)) {
editorDrawStatusMsg("Renaming failed...");
return;
}

editorDrawStatusMsg("File saved");
}

char line[256];

void editorOpen(void) {
cpm_fcb.cr = 0;
if (cpm_open_file(&cpm_fcb)) die("Unable to read file", false);
if (cpm_open_file(&cpm_fcb)) return; // new file

cpm_set_dma(cpm_default_dma);

Expand Down Expand Up @@ -265,6 +332,11 @@ void editorProcessKeypress(void) {
case CTRL('Q'): // ^KQ Quit
die("Done.", true);
break;
case 's':
case 'S':
case CTRL('S'): // ^KS Save
editorSave();
break;
}
ctrlk = false;
return;
Expand Down

0 comments on commit c02f314

Please sign in to comment.