aboutsummaryrefslogblamecommitdiffstats
path: root/io.c
blob: d14a65d43269ab999f65b278ac971ed77d05cd91 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                                                        
                  



                    





                            

                                                




                                


                                                           




                          
 
    
                                     
 

                                         

         

                                


                                 

                                                                             


                                            
                       




                        
                     
                                                      
                                                               
                                                 
                                


                  
                           
                        
                                           
               

 




                          

                                                     

                                  
                                                            



                                                    
                                                         




                                         



                                     


                  

                                           




                                
                          


                  
   
                                  
 



                                                   
                                                   
         
                          

                 
 
   
                                           
 




                                                                
         
                          

                 






                                          

    
                      
 
                                              





                                                               

                                        
         
                    

                
/* Copyright (c) 2021 nytpu <alex@nytpu.com>
 * SPDX-License-Identifier: GPL-3.0-only
 * For more license details, see LICENSE or <https://www.gnu.org/licenses/gpl-3.0.html>.
 */

#include "io.h"
#include "buffer.h"
#include "common.h"

#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static char *
rows_to_string(long *buflen)
{
	long len = 0;
	for (RowNum i = 0; i < E.numrows; ++i) {
		len += E.rows[i].size + 1;
	}
	*buflen = len;

	char *buf = malloc(len);
	char *h = buf;
	for (RowNum i = 0; i < E.numrows; ++i) {
		memcpy(h, E.rows[i].chars, E.rows[i].size);
		h += E.rows[i].size;
		*h = '\n';
		++h;
	}
	return buf;
}

void
open_file(const char *fn, bool force)
{
	if (E.dirty && !force) {
		error("Buffer modified");
	}

	free(E.filename);
	E.filename = strdup(fn);

	FILE *f = fopen(fn, "r");
	if (!f) {
		// filename is already saved so we could potentially write in
		// the future
		if (errno != ENOENT) {
			die("Opening file");
		}
		return;
	}

	char *l = NULL;
	size_t lcap = 0;
	ssize_t llen;
	long tot = 0;
	while ((llen = getline(&l, &lcap, f)) != -1) {
		while (llen > 0 && (l[llen-1] == '\n')) --llen;
		insert_row(E.numrows+1, l, llen);
		tot += llen + 1;
	}
	free(l);
	fclose(f);
	E.crow = E.numrows;
	E.dirty = false;
	if (!E.quiet) printf("%ld\n", tot);
	return;
}

int
write_file(const char *fn)
{
	int fd;
	if (fn == NULL) {
		if (E.filename == NULL) {
			error("No current filename");
			return -1;
		}
		fd = open(E.filename, O_RDWR|O_CREAT, 0644);
	} else {
		fd = open(fn, O_RDWR|O_CREAT, 0644);
	}
	if (fd == -1) {
		error("Failed to open file for writing");
		return -1;
	}

	long <