summaryrefslogtreecommitdiff
path: root/src/note_list.c
blob: 19727bb403044fd4c398c1f54fb5775b026601f1 (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_list.h"
   18 
   19 /**
   20  * Constructor
   21  *
   22  * @param list List stream to be instantiated
   23  * @param path Path to the treasure... er... notes
   24  * @param ext Extension of the notes to pay attention to
   25  */
   26 int note_list_new(note_list* list, char* path, char* ext) {
   27   list->cursor = 0;
   28   // Open the dir fd
   29   list->dirp    = opendir(path);
   30   list->noteent = malloc(sizeof(note));
   31   strcpy(list->path, path);
   32   strcpy(list->ext, ext);
   33 
   34   // Skip the first two, as they are always . and ..
   35   readdir(list->dirp);
   36   readdir(list->dirp);
   37 
   38   return 0;
   39 }
   40 
   41 
   42 void note_list_free(note_list* list) {
   43   closedir(list->dirp);
   44 }
   45 
   46 
   47 note* note_list_read(note_list* list) {
   48   struct dirent* de;
   49   char fext[64];
   50 
   51   // Iterrate over dir entities
   52   while(1) {
   53     // Return null if readdir also returns null
   54     if((de = readdir(list->dirp)) == NULL) { return NULL; } 
   55     // Skip any non-regular files (DT_REG == 8)
   56     if(de->d_type != 8) { continue; }
   57     // Skip hidden files
   58     if(de->d_name[0] == '.') { continue; }
   59 
   60     // Skip files that don't match the specified extension
   61     get_extension(de->d_name, fext);
   62     if(strcmp(fext, list->ext) != 0) { continue; }
   63 
   64     break;
   65   }
   66 
   67   // Copy in the path
   68   sprintf(list->noteent->path, "%s/%s", list->path, de->d_name);
   69   // Copy in the extension
   70   strcpy(list->noteent->extension, fext);
   71   // Copy in the filename
   72   strcpy(list->noteent->name, de->d_name);
   73   // Overwrite . with null byte
   74   list->noteent->name[strlen(de->d_name) - strlen(fext) - 1] = '\0';
   75 
   76   // Increment the file counter
   77   ++list->cursor;
   78   return list->noteent;
   79 }
   80 
   81 
   82 /**
   83  * Opens the specified note for editing
   84  *
   85  * @param list   Note list the note-to-be-edited belongs to
   86  * @param editor Path to the editor to use for editing
   87  * @param term   Search term
   88  */
   89 int note_list_edit(note_list* list, char* editor, char* term) {
   90   note* n;
   91 
   92   while(1) {
   93     n = note_list_read(list);
   94     if(n == NULL) { break; }
   95 
   96     if(strncmp(n->name, term, strlen(term)) == 0) {
   97       file_edit(n->path, editor);
   98       return 0;
   99     }
  100   }
  101 
  102   printf("No notes exist that match the search term '%s'.\n", term);
  103   return 1;
  104 }
  105 
  106 
  107 /**
  108  * Opens a new note for editing
  109  *
  110  * @param string editor    Path to the editor to use for editing
  111  * @param string note_name Name of the note to be created and edited
  112  *
  113  * @return int Success or failure (always success for now)
  114  */
  115 int note_list_create_note(note_list* list, char* editor, char* name) {
  116   char fullname[128];
  117   char fullpath[256];
  118   note* n;
  119 
  120   // Create full file name
  121   sprintf(fullname, "%s.%s", name, list->ext);
  122 
  123   // Check if note already exists
  124   while((n = note_list_read(list)) != NULL) {
  125     if(strcmp(n->name, fullname) == 0) { return 1; }
  126   }
  127 
  128   // Construct full note path for the new note
  129   sprintf(fullpath, "%s/%s", list->path, fullname);
  130   file_edit(fullpath, editor);
  131   return 0;
  132 }
  133 
  134 
  135 /**
  136  * Returns the contents of the requested note
  137  *
  138  * @param string note_name Name of the note to get the contents for
  139  *
  140  * @return vector<string> Contents of the note, broken per line into a vector.
  141  */
  142 int note_list_cat_note(note_list* list, char* term) {
  143   note* n;
  144   // Check for the requested note
  145   while((n = note_list_read(list)) != NULL) {
  146     if(strncmp(n->name, term, strlen(term)) == 0) {
  147       note_cat(n);
  148       return 0;
  149     }
  150   }
  151   return 1;
  152 }
  153 
  154 
  155 /**
  156  * Searches each note in the list for the given search term.
  157  * 
  158  * @return int Number of instances found in all notes
  159  *             TODO: This needs to be written still
  160  */
  161 int note_list_search(note_list* list, char* term, int case_insensitive) {
  162   note* n;
  163 
  164   while((n = note_list_read(list)) != NULL) {
  165     note_search(n, term, case_insensitive);
  166   }
  167   return 0;
  168 }
  169 
  170 
  171 /**
  172  * Deletes the specified note
  173  *
  174  * @param string note_name Name of the note to be deleted
  175  *
  176  * @return int Exit code (1 = error, 0 = success)
  177  */
  178 int note_list_rm_note(note_list* list, char* name) {
  179   note* n;
  180 
  181   while((n = note_list_read(list)) != NULL) {
  182     if(strncmp(n->name, name, strlen(name)) == 0) {
  183       return note_rm(n);
  184     }
  185   }
  186   return 1;
  187 }

Generated by cgit