/* G-Cows - scripting language for creation of web sites Copyright (C) 2002, 2003, 2004, 2005, 2006 Andrea Sozzi This file is part of G-Cows. G-Cows 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 2, or (at your option) any later version. G-Cows 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 this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include "file_utils.H" #include "error.H" using namespace std; /* Remove unnecessary components in file names; e.g /foo/./bar/../foo is converted in /foo/foo. */ string opt_file_name (string file_name, bool full_opt) { //unsigned int slash, end; string::size_type slash, end; int parents = 0; string opt_file; end = slash = file_name.rfind ('/'); string file = file_name.substr (slash + 1, file_name.length ()); while ( (slash != string::npos) && (slash != 0) ) { slash = file_name.rfind ("/", slash - 1); string file_name_fragment = file_name.substr (slash + 1, end - slash); if ( ( (slash == string::npos) || (slash == 0) ) && ! full_opt && (file_name_fragment == "./") ) continue; if ( file_name_fragment == "../" ) parents++; else if ( file_name_fragment != "./" ) { if (parents > 0) parents--; else opt_file = file_name_fragment + opt_file; } end = slash; } string prepend = file_name.substr (0, end+1); while ( parents--) prepend += "../"; return prepend + opt_file + file; } char * get_w_dir () { int length = 100; char *w_dir = new char [length]; while ( ! (w_dir = getcwd (w_dir, length))) { if (errno != ERANGE) Msg::inst()->sys_fatal ("Can't get current dir"); else { delete [] w_dir; length *= 2; w_dir = new char [length]; } } return w_dir; } int set_f_w_dir (string file) { string dir_name = get_dir_from_file_name (file); if ( dir_name != "" ) if (chdir (dir_name.c_str ())) return EXIT_FAILURE; return EXIT_SUCCESS; } string get_dir_from_file_name (string file_name) { // unsigned int last_slash; string::size_type last_slash; if ( (last_slash = file_name.rfind ("/")) != string::npos ) return file_name.substr (0, last_slash) + "/"; return ""; } string strip_dir (string file_name) { // unsigned int last_slash; string::size_type last_slash; if ( (last_slash = file_name.rfind ("/")) != string::npos ) return file_name.substr (last_slash + 1); return file_name; } int get_dir_level () { int start = -1, level = 1; string w_dir; char *w_dir_char = get_w_dir (); if (w_dir_char) { w_dir = w_dir_char; while ((start=w_dir.find ("/", start + 1)) != (int)string::npos ) level++; return level; } else return -1; } int get_dir_level (string file_name) { int start = -1, level = 1; string target_file; if (*file_name.c_str() == '/') // absolute file name target_file = file_name; else { // relative file name .. convert to absolute char *w_dir_char = get_w_dir (); // if (! w_dir_char) // Msg::inst()->sys_fatal ("Can't get current dir"); target_file = string (w_dir_char) + string ("/") + file_name; } target_file = opt_file_name (target_file); while ((start=target_file.find ("/", start + 1)) != (int)string::npos ) level++; return level-1; } string to_absolute (string file) { if (*file.c_str () != '/') return string (get_w_dir ()) + "/" + file; else return file; } string add_slash (string file) { if ( (file == "") || ( *(file.c_str () + file.length () - 1) == '/') ) return file; else return file + "/"; } bool file_exists (string file) { ifstream input_stream; input_stream.open (file.c_str (), ios::in); if (input_stream.is_open() ) { input_stream.close(); return true; } else { return false; } }