jim

Simple, lightweight, modal, vim-inspired text editor
git clone git://git.janpasierb.com/jim.git
Log | Files | Refs | README | LICENSE

file.c (2115B)


      1 #include"file.h"
      2 #include"jim.h"
      3 #include"row.h"
      4 #include"output.h"
      5 #include"input.h"
      6 #include"term.h"
      7 #include"syntax.h"
      8 #include<stdlib.h>
      9 #include<string.h>
     10 #include<stdio.h>
     11 #include<errno.h>
     12 #include<fcntl.h>
     13 #include<unistd.h>
     14 
     15 char* editorRowsToString(int* buflen) {
     16     int totlen = 0;
     17     int j;
     18 
     19     for(j = 0; j < E.numrows; j++)
     20         totlen += E.row[j].size + 1;
     21     *buflen = totlen;
     22 
     23     char* buf = malloc(totlen);
     24     char* p = buf;
     25 
     26     for(j = 0; j < E.numrows; j++) {
     27         memcpy(p, E.row[j].chars, E.row[j].size);
     28         p += E.row[j].size;
     29         *p = '\n';
     30         p++;
     31     }
     32 
     33     return buf;
     34 }
     35 
     36 void editorOpen(char* filename) {
     37     free(E.filename);
     38     E.filename = strdup(filename);
     39 
     40     editorSelectSyntaxHighlight();
     41 
     42     FILE* fp = fopen(filename, "r");
     43     if (fp) {
     44 		char* line = NULL;
     45 		size_t linecap = 0;
     46 		ssize_t linelen;
     47 		while((linelen = getline(&line, &linecap, fp)) != -1) {
     48 			while(linelen > 0 && (line[linelen - 1] == '\n' || line[linelen - 1] == RETURN))
     49 				linelen--;
     50 			editorInsertRow(E.numrows, line, linelen);
     51 		}
     52 
     53 		free(line);
     54 		fclose(fp);
     55 	} else {
     56 		int fd = open(E.filename, O_RDONLY | O_CREAT, 0644);
     57 		if(fd == -1)
     58 			die("fopen");
     59 		close(fd);
     60 	}
     61 
     62     E.dirty = 0;
     63 }
     64 
     65 int editorSave() {
     66     if(!E.dirty) return 0;
     67     if(E.filename == NULL) {
     68         E.filename = editorPrompt("Save as: %s", NULL);
     69         if(E.filename == NULL) {
     70             return 1;
     71         }
     72         editorSelectSyntaxHighlight();
     73     }
     74 
     75     int len, bw;
     76     char* buf = editorRowsToString(&len);
     77 
     78     int fd = open(E.filename, O_RDWR | O_CREAT, 0644);
     79     if(fd != -1) {
     80         if(ftruncate(fd, len) != -1) {
     81             if((bw = write(fd, buf, len)) != -1) {
     82                 close(fd);
     83                 free(buf);
     84                 E.dirty = 0;
     85                 editorSetStatusMessage("\"%.20s\" %dL, %dB written", E.filename, E.numrows, bw);
     86                 return 0;
     87             }
     88         }
     89         close(fd);
     90     }
     91 
     92     free(buf);
     93     E.isbold = 1;
     94     E.isr = 0;
     95     editorSetStatusMessage("Failed to write file to disk! I/O error: %s", strerror(errno));
     96     return 1;
     97 }