Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Path.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "Path.h"
00010 
00011 #include "Log.h"
00012 #include "OptionAgent.h"
00013 #include "FsPath.h"
00014 #include "PathException.h"
00015 
00016 #include <stdio.h>
00017 
00018 //-----------------------------------------------------------------
00019     Path::Path(const std::string &file)
00020 : m_path(file)
00021 {
00022     /* empty */
00023 }
00024 //-----------------------------------------------------------------
00025 /**
00026  * Try return user data path,
00027  * otherwise return system data path.
00028  * NOTE: OptionAgent must be initialized before this
00029  *
00030  * @param file path to file
00031  * @param writeable whether we want write to the file
00032  * @return path to user or system file
00033  */
00034     Path
00035 Path::dataPath(const std::string &file, bool writeable)
00036 {
00037     Path datapath = dataUserPath(file);
00038 
00039     if (!datapath.exists())  {
00040         FILE *try_open = NULL;
00041         if (writeable) {
00042             try {
00043                 LOG_INFO(ExInfo("creating path")
00044                         .addInfo("path", datapath.getNative()));
00045                 FsPath::createPath(datapath.getPosixName());
00046 
00047                 try_open = fopen(datapath.getNative().c_str(), "wb");
00048             }
00049             catch (PathException &e) {
00050                 LOG_WARNING(e.info());
00051             }
00052         }
00053 
00054         if (try_open) {
00055             fclose(try_open);
00056         }
00057         else {
00058             datapath = dataSystemPath(file);
00059         }
00060     }
00061 
00062     return datapath;
00063 }
00064 //-----------------------------------------------------------------
00065     Path
00066 Path::dataReadPath(const std::string &file)
00067 {
00068     return dataPath(file, false);
00069 }
00070 //-----------------------------------------------------------------
00071     Path
00072 Path::dataWritePath(const std::string &file)
00073 {
00074     return dataPath(file, true);
00075 }
00076 //-----------------------------------------------------------------
00077 /**
00078  * Return path to system file.
00079  * Path does not need to exist.
00080  */
00081     Path
00082 Path::dataSystemPath(const std::string &file)
00083 {
00084     std::string systemdir = OptionAgent::agent()->getParam("systemdir");
00085     return Path(FsPath::join(systemdir, file));
00086 }
00087 //-----------------------------------------------------------------
00088 /**
00089  * Return path to user file.
00090  * Path does not need to exist.
00091  */
00092     Path
00093 Path::dataUserPath(const std::string &file)
00094 {
00095     std::string userdir = OptionAgent::agent()->getParam("userdir");
00096     return Path(FsPath::join(userdir, file));
00097 }
00098 
00099 //-----------------------------------------------------------------
00100 std::string
00101 Path::getNative() const
00102 {
00103     return FsPath::getNative(m_path);
00104 }
00105 //-----------------------------------------------------------------
00106 bool
00107 Path::exists() const
00108 {
00109     return FsPath::exists(m_path);
00110 }

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2