Syntax highlighting and comment
This commit is contained in:
+62
-2
@@ -1,3 +1,11 @@
|
||||
/**
|
||||
* @file file_io.c
|
||||
* @brief File I/O operations module for the Beluga text editor
|
||||
* @details Handles file loading, saving, searching, and buffer management.
|
||||
* Provides functionality for opening/closing files, persisting changes to disk,
|
||||
* and searching for text patterns within the document.
|
||||
*/
|
||||
|
||||
#include "../include/file_io.h"
|
||||
#include "../include/input.h"
|
||||
#include "../include/output.h"
|
||||
@@ -14,6 +22,15 @@ extern ssize_t getline(char **restrict lineptr, size_t *restrict n,
|
||||
extern int ftruncate(int fd, off_t length);
|
||||
extern struct editorConfig E;
|
||||
|
||||
/**
|
||||
* @brief Converts all editor rows to a single string buffer
|
||||
* @details Concatenates all row content into a single allocated buffer with
|
||||
* newlines between rows. Useful for file saving and buffer operations.
|
||||
* @param buffer_len Pointer to integer where total buffer length will be stored
|
||||
* @return Pointer to dynamically allocated buffer containing all row data.
|
||||
* Rows are separated by newline characters.
|
||||
* @note Caller is responsible for freeing the returned buffer
|
||||
*/
|
||||
char *editorRowsToString(int *buffer_len) {
|
||||
int tot_len = 0;
|
||||
int j;
|
||||
@@ -36,20 +53,42 @@ char *editorRowsToString(int *buffer_len) {
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Closes the current file and resets editor state
|
||||
* @details Clears all rows, resets cursor position, scroll offsets, and file metadata.
|
||||
* Does not prompt to save unsaved changes.
|
||||
* @note Updates global editor state E
|
||||
*/
|
||||
void editorCloseFile(void) {
|
||||
E.cursor_x = 0;
|
||||
E.cursor_y = 0;
|
||||
E.rx = 0;
|
||||
E.row_offset = 0;
|
||||
E.col_offset = 0;
|
||||
for (int i = 0; i < E.numrows; ++i) {
|
||||
free(E.row[i].chars);
|
||||
free(E.row[i].render);
|
||||
}
|
||||
E.numrows = 0;
|
||||
E.row = NULL;
|
||||
E.dirty = 0;
|
||||
free(E.filename);
|
||||
E.filename = NULL;
|
||||
E.status_msg[0] = '\0';
|
||||
E.status_msg_time = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Opens a file for editing
|
||||
* @details Loads file content into editor rows, one line per row. If another file
|
||||
* is already open, it is closed first (without saving). File is opened in a+
|
||||
* (read/append) mode to allow both reading and modification.
|
||||
* @param filename Path to the file to open (relative or absolute)
|
||||
* @note Updates global editor state E
|
||||
* @note Calls die() on file open failure
|
||||
* @note Newline characters are stripped from loaded lines
|
||||
* @see editorInsertRow()
|
||||
*/
|
||||
void editorOpen(char *filename) {
|
||||
FILE *fp;
|
||||
|
||||
@@ -59,7 +98,6 @@ void editorOpen(char *filename) {
|
||||
E.state = READ_AND_WRITE;
|
||||
}
|
||||
|
||||
free(E.filename);
|
||||
E.filename = strdup(filename);
|
||||
|
||||
fp = fopen(filename, "a+");
|
||||
@@ -76,12 +114,24 @@ void editorOpen(char *filename) {
|
||||
--line_len;
|
||||
}
|
||||
editorInsertRow(E.numrows, line, line_len);
|
||||
free(line);
|
||||
line = NULL;
|
||||
}
|
||||
free(line);
|
||||
fclose(fp);
|
||||
E.dirty = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Saves the current file to disk
|
||||
* @details Prompts for filename if not set, converts all rows to a buffer,
|
||||
* writes to disk using open/ftruncate/write, and updates dirty flag.
|
||||
* Displays status messages on success or failure.
|
||||
* @note Updates global editor state E (dirty flag)
|
||||
* @note If no filename is set, prompts user via editorPrompt()
|
||||
* @note Uses O_RDWR | O_CREAT with mode 0644
|
||||
* @see editorRowsToString()
|
||||
*/
|
||||
void editorSave() {
|
||||
int len;
|
||||
char *buf;
|
||||
@@ -111,7 +161,18 @@ void editorSave() {
|
||||
editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Searches for a string in the document
|
||||
* @details Prompts user for a search query, then searches forward from current
|
||||
* cursor position. Updates cursor position to the first match found.
|
||||
* @note Updates global editor state E (cursor position, row_offset)
|
||||
* @note Search is case-sensitive and operates on rendered line content
|
||||
* @note Searches begin from the line after current cursor position
|
||||
* @see editorPrompt()
|
||||
* @see editorRowRxToCx()
|
||||
*/
|
||||
void editorFind() {
|
||||
fprintf(stderr, "searching\n");
|
||||
char *query = editorPrompt("Search: %s (ESC to cancel)", "", 0);
|
||||
if (query == NULL) return;
|
||||
int i;
|
||||
@@ -127,4 +188,3 @@ void editorFind() {
|
||||
}
|
||||
free(query);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user