/** * Copyright (C) 2021 Aaron Ball * * Noteless is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Noteless is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with noteless. If not, see . */ #include "note.h" /** * Creates a new empty note object since no filename was specified. */ void note_new(note* n, char* path) { strcpy(n->path, path); strcpy(n->name, basename(path)); // Get file stats struct stat s; stat(path, &s); // Last modified time n->mtime = s.st_mtime; } /** * Opens a new note using the specified editor. * * @param string editor Path to the editor binary to use for editing. * * @return int Success or failure of the command */ int note_create(note* n, char* editor) { return file_edit(n->path, editor); } /** * Performs a date comparison on the two note structs. Returns an integer * representing in seconds if struct b was modified more recently (positive * int) or less recently (negative int) than struct a. If the two were modified * on the same date and time, 0 is returned. * * @param a Note struct to compare last modified date * @param b Note struct to compare last modified date */ int note_date_cmp(const void* a, const void* b) { note* note_a = (note*) a; note* note_b = (note*) b; return (int)(note_a->mtime - note_b->mtime); } /** * Copies the contents of one note struct into the pointer to the second note * struct. * * @param dest Destination note struct * @param src Source note struct */ void note_cpy(note* dest, note* src) { strcpy(dest->extension, src->extension); strcpy(dest->path, src->path); strcpy(dest->name, src->name); dest->mtime = src->mtime; } /** * Prints the contents of the specified note. * * @return int Status of the cat operation. * 0 Success * -1 File could not be opened */ int note_cat(note* n) { FILE* f = fopen(n->path, "r"); // Return early if the file could not be opened if(!f) { return -1; } char c; // Print the contents of the note while((c = fgetc(f)) != EOF) { printf("%c", c); } // Close up shop fclose(f); printf("\n"); return 0; } /** * Deletes the current note instance from the filesystem. * * @return int Success or failure */ int note_rm(note* n) { // Verify file exists. FILE* f = fopen(n->path, "r"); if(f) { return remove(n->path); fclose(f); } else { return 1; } } /** * TODO */ int note_search(note* n, char* term, int case_insensitive) { FILE* f = fopen(n->path, "r"); char buf[file_width]; int line_num = 1; while((fgets(buf, file_width, f)) != NULL) { if(str_contains(buf, term, case_insensitive) == 1) { printf("%s: % 4d: %s", n->name, line_num, buf); } line_num++; } fclose(f); return 0; }