summaryrefslogtreecommitdiff
path: root/src/path.c
blob: faa9419b7e0cf2094c11ec353125ee2f537847f6 (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 "path.h"
   18 
   19 path::path() {}
   20 
   21 path::path( string p ) {
   22   value = p;
   23   // Parse path into parent dir array and filename
   24   long cur_dir_begin = 0;
   25 
   26   // Setting this to a variable so we can handle Windows in the future
   27   delim = '/';
   28 
   29   // Set the path type ( [r]elative or [a]bsolute ) based on the first char in
   30   // the path.
   31   // Also set the starting integer (we can skip 0 if it's a delimiter)
   32   int i = 0;
   33   if( p[0] == delim ) {
   34     type = 'a';
   35     i = 1;
   36   } else {
   37     type = 'r';
   38   }
   39   // Parse the path into a vector
   40   for( i; i < p.length(); i++ ) {
   41     // Parse the directories out of the path
   42     if( p[i] == delim ) {
   43       // Handle multiple consecutive delimiters.
   44       if( p[i + 1] == delim ) {
   45         continue;
   46       }
   47       // Check if we've hit a path delimiter (/ or \)
   48       dirs.push_back( p.substr( cur_dir_begin, i - cur_dir_begin ) );
   49       // Skip the slash since we don't need it
   50       cur_dir_begin = i;
   51     }
   52   }
   53   // Annnnnd output the filename...for now
   54   // Note the +1 so we don't catch the final delimiter thus making the filename
   55   // an absolute path.
   56   filename = p.substr( cur_dir_begin + 1, p.length() - cur_dir_begin );
   57   // Add the filename to the end of the array (might remove this later)
   58   dirs.push_back( p.substr( cur_dir_begin, p.length() - cur_dir_begin ) );
   59 }
   60  
   61 /**
   62  * Creates the current instance path recursively.
   63  * Note that this does assume the final destination of the path to be a
   64  * directory and not a file.
   65  *
   66  * @return int The status of the creation operation.
   67  */
   68 int path::create() {
   69   string cur_path = "";
   70   int status = 0;
   71   for( int i = 0; i < dirs.size(); i++ ) {
   72     cur_path += dirs[i];
   73     path p( cur_path );
   74     if( ! p.exists() ) {
   75       cout << "Path does not exist... creating" << p.out() << endl;
   76       status = mkdir( p.out().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
   77     }
   78   }
   79   return status;
   80 }
   81 
   82 /**
   83  * Checks if a particular file exists by attempting to open a file handle.
   84  *
   85  * @return bool File exists (true) or not (false)
   86  */
   87 bool path::exists() {
   88   // Attempt to open a file handle
   89   ifstream fs( value.c_str() );
   90 
   91   // Verify the file handle opened sucessfully
   92   if( ! fs.is_open() ) {
   93     return false;
   94   }
   95 
   96   // Looks like it opened, so close and return true
   97   fs.close();
   98   return true;
   99 }
  100 
  101 /**
  102  * Detects if the instance is a directory
  103  *
  104  * @return int Directory or not
  105  */
  106 int path::is_dir() {
  107   int dir_sig = 0x41ed;
  108   // Not needed since this is an else, but good information to have handy
  109   // int file_sig = 0x81a4;
  110 
  111   // Stat the item
  112   struct stat sb;
  113   stat( value.c_str(), &sb );
  114   // Check if it's a file or a dir
  115   if( sb.st_mode == dir_sig ) {
  116     // Is dir
  117     return 1;
  118   }
  119   // Is a file [or something else]
  120   return 0;
  121 }
  122 
  123 /**
  124  * Appends the given directory(s) to the path instance.
  125  * TODO
  126  *
  127  * @param const char* path Path to be appended
  128  *
  129  * @return int Error (1) or not (0)
  130  */
  131 int path::append(string path) {
  132   return 1;
  133 }
  134 
  135 string path::basename() {
  136   return dirs[dirs.size()];
  137 }
  138 
  139 string path::out() {
  140   return value;
  141 }

Generated by cgit