summaryrefslogtreecommitdiff
path: root/src/note_list.c
blob: 327347d7774fc6f3aebb82bbe729872ad1fb0972 (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 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 void note_list_new(note_list_t* list, char* path, char* ext) {
   27   list->count = 0;
   28   strcpy(list->extension, ext);
   29   strcpy(list->path, path);
   30 
   31   DIR* d = opendir(path);
   32   struct dirent* ent;
   33 
   34   int ext_len = strlen(ext);
   35 
   36   // First iterration to get a matching file count
   37   while((ent = readdir(d))) {
   38     // The start index of the extension in the current filename
   39     int ext_start = strlen(ent->d_name) - ext_len;
   40     
   41     if(strncmp(&ent->d_name[ext_start], ext, ext_len) == 0) {
   42       list->count++;
   43     }
   44   }
   45 
   46   // Create name list of previously discovered size
   47   list->names = malloc(sizeof(char*) * list->count);
   48 
   49   rewinddir(d);
   50   int i = 0;
   51   // Second iterration for populating the file list
   52   while((ent = readdir(d))) {
   53     // The start index of the extension in the current filename
   54     int ext_start = strlen(ent->d_name) - ext_len;
   55 
   56     if(strncmp(&ent->d_name[ext_start], ext, ext_len) == 0) {
   57       //printf("%s\n", ent->d_name);
   58       list->names[i] = malloc(sizeof(char) * strlen(ent->d_name) - ext_len - 1);
   59       // Copy the temp filename into the struct
   60       strncpy(list->names[i], ent->d_name, strlen(ent->d_name) - ext_len - 1);
   61       i++;
   62     }
   63   }
   64 
   65   closedir(d);
   66 }
   67 
   68 
   69 void note_list_free(note_list_t* list) {
   70   for(int i = 0; i < list->count; i++) {
   71     free(list->names[i]);
   72   }
   73   free(list->names);
   74 }
   75 
   76 
   77 // note_list::note_list( string dirname, string ext ) {
   78 //   _dirname = dirname;
   79 //   _ext = ext;
   80 //   get_notes( _dirname, _ext, "" );
   81 // }
   82 
   83 // void note_list::get_notes( string base, string ext, string sub ) {
   84 //   // Dir pointer
   85 //   DIR* dp;
   86 // 
   87 //   string search_dir = base;
   88 //   // Append the subdir if necessary
   89 //   if( sub != "" ) {
   90 //     // Open the subdir if it's specified
   91 //     search_dir = base + "/" + sub;
   92 //   }
   93 //   dp = opendir( search_dir.c_str() );
   94 // 
   95 //   if( dp ) {
   96 //     // Prepare to iterrate through dir items
   97 //     struct dirent* item;
   98 //     while( item = readdir( dp ) ) {
   99 //       string name = item->d_name;
  100 // 
  101 //       path p( base + "/" + sub + "/" + name );
  102 // 
  103 //       if( name[0] != '.' ) {
  104 //         if( p.is_dir() == 1 ) {
  105 //           // TODO
  106 //           // I really hate the way I'm doing this repeatedly. Going to need a
  107 //           // better way.
  108 //           if( sub != "" ) {
  109 //             get_notes( base, ext, sub + "/" + name );
  110 //           } else {
  111 //             get_notes( base, ext, name );
  112 //           }
  113 //         } else {
  114 //           // This will be replaced later with path.join() when it's implemented
  115 //           if( sub != "" ) {
  116 //             add( base, sub + "/" + name );
  117 //           } else {
  118 //             add( base, name );
  119 //           }
  120 //         }
  121 //       }
  122 //     }
  123 //     closedir( dp );
  124 //   }
  125 // }
  126 
  127 
  128 /**
  129  * Adds a new note item to the list by path.
  130  *
  131  * @param string path Path to the note to be added to the list
  132  *
  133  * @return 0
  134  */
  135 // int note_list::add( string base, string name ) {
  136 //   note n( base, name  );
  137 //   notes.push_back( n ); 
  138 //   return 0;
  139 // }
  140 
  141 /**
  142  * Opens a new note for editing
  143  *
  144  * @param string editor Path to the editor to use for editing
  145  * @param string note_name Name of the note to be created and edited
  146  *
  147  * @return int Success or failure (always success for now)
  148  */
  149 // int note_list::create( string editor, string note_name ) {
  150 //   string spath = _dirname + '/' + note_name + '.' + _ext;
  151 //   path p( spath );
  152 //   note n( p );
  153 //   n.edit( editor );
  154 //   return 0;
  155 // }
  156 
  157 /**
  158  * Opens the specified note for editing
  159  *
  160  * @param string editor Path to the editor to use for editing
  161  * @param string note_name Name of the note to be edited
  162  */
  163 // int note_list::edit( string editor, string note_name ) {
  164 //   int id = find_note_id( note_name );
  165 //   if( id != -1 ) {
  166 //     notes[id].edit( editor );
  167 //   } else {
  168 //     cout << "Note \"" << note_name << "\" does not exist." << endl;
  169 //     return 1;
  170 //   }
  171 //   return 0;
  172 // }
  173 
  174 /**
  175  * Searches all note *names* (friendly and unfriendly) for the given name and
  176  * returns the note id (not necissarily persistent). If note does not exist,
  177  * returns -1. Can be used for a sort of "note_exists" method as such.
  178  *
  179  * @param string note_name Name of the note to search for
  180  *
  181  * @return int The integer of the note. -1 if note does not exist
  182  */
  183 // int note_list::find_note_id( string note_name ) {
  184 //   for( int i = 0; i < notes.size(); i++ ) {
  185 //     if( notes[i].name == note_name || notes[i].friendly_name() == note_name ) {
  186 //       return i;
  187 //     }
  188 //   }
  189 //   return -1;
  190 // }
  191 
  192 /**
  193  * Enumerates all note names.
  194  */
  195 // vector<string> note_list::enum_names() {
  196 //   vector<string> names;
  197 //   for( int i = 0; i < notes.size(); i++ ) {
  198 //     names.push_back( notes[i].friendly_name() );
  199 //   }
  200 //   return names;
  201 // }
  202 
  203 /**
  204  * Returns the contents of the requested note
  205  *
  206  * @param string note_name Name of the note to get the contents for
  207  *
  208  * @return vector<string> Contents of the note, broken per line into a vector.
  209  */
  210 // int note_list::cat_note( string note_name, vector<string>* pbody ) {
  211 //   // Check for the requested note
  212 //   int id = find_note_id( note_name );
  213 //   if( id != -1 ) {
  214 //     // Read in the note's contents
  215 //     notes[id].read();
  216 //     *pbody = notes[id].body;
  217 //   } else {
  218 //     return 1;
  219 //   }
  220 //   return 0;
  221 // }
  222 
  223 /**
  224  * Deletes the specified note
  225  *
  226  * @param string note_name Name of the note to be deleted
  227  *
  228  * @return int Exit code (1 = error, 0 = success)
  229  */
  230 // int note_list::rm( string note_name ) {
  231 //   int id = find_note_id( note_name );
  232 //   if( id == -1 ) {
  233 //     cout << "Note \"" << note_name << "\" does not exist." << endl;
  234 //     return 1;
  235 //   } else {
  236 //     notes[id].rm();
  237 //   }
  238 //   return 0;
  239 // }
  240 
  241 /**
  242  * TODO
  243  */
  244 // vector<string> note_list::find( bool case_sensitive, string term ) {
  245 //   vector<string> matches;
  246 //   for( int i = 0; i < notes.size(); i++ ) {
  247 //     vector<string> note_matches = notes[i].find( case_sensitive, term );
  248 //     if( note_matches.size() > 0 ) {
  249 //       for( int m = 0; m < note_matches.size(); m++ ) {
  250 //         matches.push_back( note_matches[m] );
  251 //       }
  252 //     }
  253 //   }
  254 //   return matches;
  255 // }

Generated by cgit