updated comments
[barrytmp.git] / gui / src / util.cc
blob3d9398cf6697d9dad84d50e527f44b04951dc6ed
1 ///
2 /// \file util.cc
3 /// GUI utility functions
4 ///
6 /*
7 Copyright (C) 2007-2008, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "util.h"
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
29 Glib::RefPtr<Gnome::Glade::Xml> LoadXml(const char *filename)
31 // try loading from global glade directory first
32 try {
33 std::string file = BARRYBACKUP_GLADEDIR;
34 file += filename;
36 Glib::RefPtr<Gnome::Glade::Xml> ref = Gnome::Glade::Xml::create(file);
37 return ref;
39 catch( Gnome::Glade::XmlError &e ) {
40 // oops... let's fall through, and try the local directory
43 // this will throw XmlError on failure, and we let the caller worry
44 // about that
45 return Gnome::Glade::Xml::create(filename);
48 /// Checks that the path in path exists, and if not, creates it.
49 /// Returns false if unable to create path, true if ok.
50 bool CheckPath(const std::string &path, std::string *perr)
52 if( path.size() == 0 ) {
53 if( perr )
54 *perr = "path is empty!";
55 return false;
58 if( access(path.c_str(), F_OK) == 0 )
59 return true;
61 std::string base;
62 std::string::size_type slash = 0;
63 while( (slash = path.find('/', slash + 1)) != std::string::npos ) {
64 base = path.substr(0, slash);
65 if( access(base.c_str(), F_OK) != 0 ) {
66 if( mkdir(base.c_str(), 0755) == -1 ) {
67 if( perr ) {
68 *perr = "mkdir(" + base + ") failed: ";
69 *perr += strerror(errno);
71 return false;
75 if( mkdir(path.c_str(), 0755) == -1 ) {
76 if( perr ) {
77 *perr = "last mkdir(" + path + ") failed: ";
78 *perr += strerror(errno);
80 return false;
82 return true;
85 std::string GetPath(const std::string &filename)
87 std::string path;
88 std::string::size_type pos = filename.rfind('/');
89 if( pos != std::string::npos ) {
90 path = filename.substr(0, pos);
92 return path;