00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://svn.east.isi.edu/torc/trunk/src/torc/common/DirectoryTree.cpp $ 00003 // $Id: DirectoryTree.cpp 450 2011-04-29 18:47:50Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 /// \file 00017 /// \brief Source for the DirectoryTree class. 00018 00019 #include "torc/common/DirectoryTree.hpp" 00020 #include <boost/filesystem/convenience.hpp> 00021 #include <cstdio> 00022 00023 namespace torc { 00024 namespace common { 00025 00026 boost::filesystem::path DirectoryTree::sRelativePath; 00027 boost::filesystem::path DirectoryTree::sWorkingPath; 00028 boost::filesystem::path DirectoryTree::sExecutablePath; 00029 boost::filesystem::path DirectoryTree::sDevicesPath; 00030 boost::filesystem::path DirectoryTree::sLogPath; 00031 00032 /// \brief Name of the devices directory. 00033 /// \todo This should be obtained from a configuration file. 00034 const std::string cDevicesNameConst("devices"); 00035 00036 /// \brief Name of the torc directory. 00037 /// \todo This should be obtained from a configuration file. 00038 const std::string cTorcNameConst("torc"); 00039 00040 DirectoryTree::DirectoryTree(const char* argv0) { 00041 // resolve symbolic links for the executable if necessary 00042 boost::filesystem::path argvPath(argv0); 00043 char buffer[FILENAME_MAX]; 00044 ssize_t count = readlink(argv0, buffer, sizeof(buffer)); 00045 if(count != -1) { 00046 buffer[count] = 0; 00047 argvPath = buffer; 00048 //std::cout << "Resolving symbolic link " << argv[0] << " to executable name " << path 00049 // << std::endl; 00050 } 00051 //std::cout << ">>>> argvPath: " << argvPath << std::endl; 00052 // get the working directory and the relative executable path 00053 sWorkingPath = boost::filesystem::initial_path(); 00054 //std::cout << ">>>> sWorkingPath: " << sWorkingPath << std::endl; 00055 sRelativePath = argvPath.parent_path(); // so much for relative paths ... 00056 //std::cout << ">>>> sRelativePath: " << sRelativePath << std::endl; 00057 // get the executable path 00058 sExecutablePath = sRelativePath; 00059 // build the eda and log paths 00060 sLogPath = sExecutablePath; 00061 // build the database path 00062 sDevicesPath = sExecutablePath / cDevicesNameConst; 00063 if(!exists(sDevicesPath)) 00064 sDevicesPath = sExecutablePath / cTorcNameConst / cDevicesNameConst; 00065 00066 // normalize each of the paths 00067 sRelativePath.normalize(); 00068 sWorkingPath.normalize(); 00069 sExecutablePath.normalize(); 00070 sDevicesPath.normalize(); 00071 sLogPath.normalize(); 00072 } 00073 00074 } // namespace common 00075 } // namespace torc