summaryrefslogtreecommitdiff
path: root/src/note.c
blob: 372f64bda4c7e81d655a69e107e903a0aa8fbf5a (plain)
    1 /**
    2  * Copyright (C) 2014 Aaron Ball <nullspoon@iohq.net>
    3  *
    4  * Noteless is free software: you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation, either version 3 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * Noteless is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with noteless.  If not, see <http://www.gnu.org/licenses/>.
   16  */
   17 #include "note.h"
   18 
   19 /**
   20  * Creates a new empty note object since no filename was specified.
   21  */
   22 void note_new(note_t* note, char* path) {
   23   strcpy(note->path, path);
   24 }
   25 
   26 
   27 /**
   28  * Opens a new note using the specified editor.
   29  *
   30  * @param string editor Path to the editor binary to use for editing.
   31  *
   32  * @return int Success or failure of the command
   33  */
   34 int note_create(note_t* note, char* editor) {
   35   return note_edit(note, editor);
   36 }
   37 
   38 
   39 /**
   40  * Opens a note using the specified editor
   41  *
   42  * @param string editor Path to the editor binary to use for editing.
   43  *
   44  * @return int Success or failure of the command
   45  */
   46 int note_edit(note_t* note, char* editor) {
   47   char cmd[strlen(editor) + 3 + strlen(note->path)];
   48   strcpy(cmd, editor);
   49   strcat(cmd, " ");
   50   strcat(cmd, note->path);
   51   return system(cmd);
   52 }
   53 
   54 
   55 /**
   56  * Prints the contents of the specified note.
   57  *
   58  * @return int Status of the cat operation.
   59  *          0  Success
   60  *         -1  File could not be opened
   61  */
   62 int note_cat(note_t* note) {
   63   FILE* f = fopen(note->path, "r");
   64   // Return early if the file could not be opened
   65   if(!f) { return -1; }
   66 
   67   char c;
   68   // Print the contents of the note
   69   while((c = fgetc(f)) != EOF) {
   70     printf("%c", c);
   71   }
   72 
   73   // Close up shop
   74   fclose(f);
   75 
   76   printf("\n");
   77 
   78   return 0;
   79 }
   80 
   81 
   82 /**
   83  * Deletes the current note instance from the filesystem.
   84  *
   85  * @return int Success or failure
   86  */
   87 int note_rm(note_t* note) {
   88   // Verify file exists.
   89   FILE* f = fopen(note->path, "r");
   90   if(f) {
   91     return remove(note->path);
   92     fclose(f);
   93   } else {
   94     return 1;
   95   }
   96 }
   97 
   98 /**
   99  * Gets the filename of the current note instance without its extension.
  100  *
  101  * @return string Note filename without extension
  102  */
  103 // string note::friendly_name() {
  104 //   return name.substr( 0, name.length() - extension.length() - 1 ); 
  105 // }
  106 
  107 /**
  108  * TODO
  109  */
  110 int note_search(note_t* note, char* term, int case_insensitive) {
  111   FILE* f = fopen(note->path, "r");
  112 
  113   char buf[file_width];
  114   int line_num = 0;
  115   while((fgets(buf, file_width, f)) != NULL) {
  116     if(str_contains(buf, term, case_insensitive) == 1) {
  117       printf("%s: %02d: %s\n", note->name, line_num, buf);
  118     }
  119     line_num++;
  120   }
  121   fclose(f);
  122   return 0;
  123 }

Generated by cgit