diff options
Diffstat (limited to 'src/path.c')
-rw-r--r-- | src/path.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/path.c b/src/path.c new file mode 100644 index 0000000..faa9419 --- /dev/null +++ b/src/path.c @@ -0,0 +1,141 @@ +/** + * Copyright (C) 2014 Aaron Ball <nullspoon@iohq.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ +#include "path.h" + +path::path() {} + +path::path( string p ) { + value = p; + // Parse path into parent dir array and filename + long cur_dir_begin = 0; + + // Setting this to a variable so we can handle Windows in the future + delim = '/'; + + // Set the path type ( [r]elative or [a]bsolute ) based on the first char in + // the path. + // Also set the starting integer (we can skip 0 if it's a delimiter) + int i = 0; + if( p[0] == delim ) { + type = 'a'; + i = 1; + } else { + type = 'r'; + } + // Parse the path into a vector + for( i; i < p.length(); i++ ) { + // Parse the directories out of the path + if( p[i] == delim ) { + // Handle multiple consecutive delimiters. + if( p[i + 1] == delim ) { + continue; + } + // Check if we've hit a path delimiter (/ or \) + dirs.push_back( p.substr( cur_dir_begin, i - cur_dir_begin ) ); + // Skip the slash since we don't need it + cur_dir_begin = i; + } + } + // Annnnnd output the filename...for now + // Note the +1 so we don't catch the final delimiter thus making the filename + // an absolute path. + filename = p.substr( cur_dir_begin + 1, p.length() - cur_dir_begin ); + // Add the filename to the end of the array (might remove this later) + dirs.push_back( p.substr( cur_dir_begin, p.length() - cur_dir_begin ) ); +} + +/** + * Creates the current instance path recursively. + * Note that this does assume the final destination of the path to be a + * directory and not a file. + * + * @return int The status of the creation operation. + */ +int path::create() { + string cur_path = ""; + int status = 0; + for( int i = 0; i < dirs.size(); i++ ) { + cur_path += dirs[i]; + path p( cur_path ); + if( ! p.exists() ) { + cout << "Path does not exist... creating" << p.out() << endl; + status = mkdir( p.out().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ); + } + } + return status; +} + +/** + * Checks if a particular file exists by attempting to open a file handle. + * + * @return bool File exists (true) or not (false) + */ +bool path::exists() { + // Attempt to open a file handle + ifstream fs( value.c_str() ); + + // Verify the file handle opened sucessfully + if( ! fs.is_open() ) { + return false; + } + + // Looks like it opened, so close and return true + fs.close(); + return true; +} + +/** + * Detects if the instance is a directory + * + * @return int Directory or not + */ +int path::is_dir() { + int dir_sig = 0x41ed; + // Not needed since this is an else, but good information to have handy + // int file_sig = 0x81a4; + + // Stat the item + struct stat sb; + stat( value.c_str(), &sb ); + // Check if it's a file or a dir + if( sb.st_mode == dir_sig ) { + // Is dir + return 1; + } + // Is a file [or something else] + return 0; +} + +/** + * Appends the given directory(s) to the path instance. + * TODO + * + * @param const char* path Path to be appended + * + * @return int Error (1) or not (0) + */ +int path::append(string path) { + return 1; +} + +string path::basename() { + return dirs[dirs.size()]; +} + +string path::out() { + return value; +} |