edit-util: introduce EditFileContext

This is a rather large change which moves
the add and install logic into edit-util.

We store an EditFile array and the number of
elements, along with the edit markers used in
temporary files and whether to remove the parent
directories of the target files if they're empty
in an EditFileContext object.

Call edit_files_add() to add an file to edit,
and do_edit_files_and_install() to do the actual
editing (through create_edit_temp_file(),
run_editor() and trim_edit_markers()).
After that, edit_file_context_done() can be used
to destroy the object.
This commit is contained in:
Mike Yuan
2023-03-11 02:01:00 +08:00
parent fa2413dd57
commit 9a11b4f953
3 changed files with 181 additions and 114 deletions
+19 -12
View File
@@ -5,21 +5,28 @@
typedef struct EditFile {
char *path;
char *tmp;
char *original_path;
char **comment_paths;
char *temp;
unsigned line;
} EditFile;
void edit_file_free_all(EditFile **ef);
typedef struct EditFileContext {
EditFile *files;
size_t n_files;
const char *marker_start;
const char *marker_end;
bool remove_parent;
} EditFileContext;
int create_edit_temp_file(
const char *target_path,
void edit_file_context_done(EditFileContext *context);
bool edit_files_contains(const EditFileContext *context, const char *path);
int edit_files_add(
EditFileContext *context,
const char *path,
const char *original_path,
char * const *comment_paths,
const char *marker_start,
const char *marker_end,
char **ret_temp_filename,
unsigned *ret_edit_line);
char * const *comment_paths);
int run_editor(const EditFile *files);
int trim_edit_markers(const char *path, const char *marker_start, const char *marker_end);
int do_edit_files_and_install(EditFileContext *context);