* ./vlsisapd/src/utilities:

- New: Path module to provide a ligthweight alternative to boost::filesystem.
        The compatibility change in boost was giving more and more touble and was
        not worth it. Path is aimed to provides the same services, but with a
        better portability.
This commit is contained in:
Jean-Paul Chaput 2013-04-21 15:35:11 +00:00
parent b553e276cc
commit 9a12f43e6b
17 changed files with 504 additions and 160 deletions

View File

@ -8,7 +8,7 @@
list(INSERT CMAKE_MODULE_PATH 0 "${VLSISAPD_SOURCE_DIR}/cmake_modules/")
set_cmake_policies()
setup_boost(program_options filesystem python regex)
setup_boost(program_options python regex)
find_package(LibXml2 REQUIRED)
find_package(PythonSitePackages REQUIRED)

View File

@ -35,6 +35,12 @@ SETUP_SEARCH_DIR(VLSISAPD)
IF(VLSISAPD_DIR_SEARCH)
#MESSAGE("-- VLSISAPD_DIR_SEARCH: ${VLSISAPD_DIR_SEARCH}")
# Utilities
FIND_PATH (UTILITIES_INCLUDE_DIR NAMES vlsisapd/utilities/Path.h PATHS ${VLSISAPD_DIR_SEARCH} PATH_SUFFIXES include)
FIND_LIBRARY(UTILITIES_LIBRARY NAMES vlsisapdutils PATHS ${VLSISAPD_DIR_SEARCH} PATH_SUFFIXES lib${LIB_SUFFIX})
SET_FOUND (UTILITIES)
MESSAGE("-- Utilities: ${UTILITIES_LIBRARY}")
# AGDS
FIND_PATH (AGDS_INCLUDE_DIR NAMES vlsisapd/agds/GdsLibrary.h PATHS ${VLSISAPD_DIR_SEARCH} PATH_SUFFIXES include)
FIND_LIBRARY(AGDS_LIBRARY NAMES agds PATHS ${VLSISAPD_DIR_SEARCH} PATH_SUFFIXES lib${LIB_SUFFIX})

View File

@ -1,3 +1,4 @@
ADD_SUBDIRECTORY(utilities)
ADD_SUBDIRECTORY(agds)
ADD_SUBDIRECTORY(cif)
IF(IS_DIRECTORY ${VLSISAPD_SOURCE_DIR}/src/openChams)

View File

@ -2,31 +2,19 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC/LIP6 2008-2010, All Rights Reserved
// Copyright (c) UPMC 2008-2013, All Rights Reserved
//
// ===================================================================
//
// $Id$
//
// x-----------------------------------------------------------------x
// | |
// | C O R I O L I S |
// +-----------------------------------------------------------------+
// | V L S I Stand - Alone Parsers / Drivers |
// | B o o k s h e l f P a r s e r |
// | |
// | Author : Jean-Paul CHAPUT |
// | E-mail : Jean-Paul.Chaput@asim.lip6.fr |
// | =============================================================== |
// | C++ Module : "./Bookshelf.cpp" |
// | *************************************************************** |
// | U p d a t e s |
// | |
// x-----------------------------------------------------------------x
// +-----------------------------------------------------------------+
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
namespace bfs = boost::filesystem;
#include "vlsisapd/bookshelf/Bookshelf.h"
#include "vlsisapd/bookshelf/BookshelfException.h"
@ -40,8 +28,8 @@ namespace Bookshelf {
void BookshelfParser::readFromFile ( std::string auxFile )
{
bfs::path auxPath ( auxFile );
if ( not bfs::exists(auxPath) ) {
Utilities::Path auxPath ( auxFile );
if ( not auxPath.exists() ) {
throw BookshelfException ( "%s .aux file not found.", auxPath.string().c_str() );
}

View File

@ -2,25 +2,17 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC/LIP6 2008-2010, All Rights Reserved
// Copyright (c) UPMC 2008-2010, All Rights Reserved
//
// ===================================================================
//
// $Id$
//
// x-----------------------------------------------------------------x
// | |
// | C O R I O L I S |
// +-----------------------------------------------------------------+
// | V L S I Stand - Alone Parsers / Drivers |
// | B o o k s h e l f P a r s e r |
// | |
// | Author : Jean-Paul CHAPUT |
// | E-mail : Jean-Paul.Chaput@asim.lip6.fr |
// | =============================================================== |
// | C++ Module : "./Main.cpp" |
// | *************************************************************** |
// | U p d a t e s |
// | |
// x-----------------------------------------------------------------x
// | C++ Module : "./BookshelfTkMain.cpp" |
// +-----------------------------------------------------------------+
#include <cmath>
@ -31,9 +23,6 @@ using namespace std;
#include <boost/program_options.hpp>
namespace boptions = boost::program_options;
#include <boost/filesystem/operations.hpp>
namespace bfs = boost::filesystem;
#include "vlsisapd/bookshelf/Exception.h"
#include "vlsisapd/bookshelf/Node.h"
#include "vlsisapd/bookshelf/Pin.h"
@ -146,8 +135,6 @@ int main ( int argc, char* argv[] )
exit ( 0 );
}
bfs::path::default_name_check ( bfs::portable_posix_name );
if ( arguments.count("aux") ) {
auto_ptr<Circuit> circuit ( Circuit::parse(arguments["aux"].as<string>()) );

View File

@ -1,6 +1,7 @@
include_directories ( ${VLSISAPD_SOURCE_DIR}/src/bookshelf/src
include_directories ( ${VLSISAPD_SOURCE_DIR}/src/utilities/src
${VLSISAPD_SOURCE_DIR}/src/bookshelf/src
${Boost_INCLUDE_DIRS}
${PYTHON_INCLUDE_PATH}
)
@ -23,7 +24,7 @@
)
set ( testcpps BookshelfTkMain.cpp )
add_library ( bookshelf ${cpps} )
target_link_libraries ( bookshelf ${Boost_LIBRARIES} )
target_link_libraries ( bookshelf vlsisapdutils )
set_target_properties ( bookshelf PROPERTIES VERSION 1.0 SOVERSION 1 )
add_executable ( bookshelf-tk ${testcpps} )
target_link_libraries ( bookshelf-tk bookshelf ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

View File

@ -2,28 +2,22 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC/LIP6 2008-2010, All Rights Reserved
// Copyright (c) UPMC 2008-2013, All Rights Reserved
//
// ===================================================================
//
// $Id$
//
// x-----------------------------------------------------------------x
// | |
// | C O R I O L I S |
// +-----------------------------------------------------------------+
// | V L S I Stand - Alone Parsers / Drivers |
// | B o o k s h e l f P a r s e r |
// | |
// | Author : Jean-Paul CHAPUT |
// | E-mail : Jean-Paul.Chaput@asim.lip6.fr |
// | E-mail : Jean-Paul.Chaput@lip6.fr |
// | =============================================================== |
// | C++ Module : "./Circuit.cpp" |
// | *************************************************************** |
// | U p d a t e s |
// | |
// x-----------------------------------------------------------------x
// +-----------------------------------------------------------------+
#include <sstream>
#include <fstream>
#include "vlsisapd/utilities/Path.h"
#include "vlsisapd/bookshelf/Exception.h"
#include "vlsisapd/bookshelf/Node.h"
#include "vlsisapd/bookshelf/Net.h"
@ -222,55 +216,55 @@ namespace Bookshelf {
void Circuit::drive ( std::string directory, unsigned int flags )
{
bfs::path rootDirectory ( directory );
if ( not bfs::exists(rootDirectory) ) {
bfs::create_directory ( rootDirectory );
Utilities::Path rootDirectory ( directory );
if ( not rootDirectory.exists() ) {
rootDirectory.mkdir();
}
if ( flags & Nodes ) {
bfs::path nodesPath ( rootDirectory );
Utilities::Path nodesPath ( rootDirectory );
if ( getNodesName().empty() )
nodesPath /= getDesignName() + ".nodes";
else
nodesPath /= getNodesName();
bfs::ofstream ofnodes ( nodesPath );
std::ofstream ofnodes ( nodesPath.c_str() );
writeNodesToStream ( ofnodes );
ofnodes.close ();
}
if ( flags & Nets ) {
bfs::path netsPath ( rootDirectory );
Utilities::Path netsPath ( rootDirectory );
if ( getNetsName().empty() )
netsPath /= getDesignName() + ".nets";
else
netsPath /= getNetsName();
bfs::ofstream ofnets ( netsPath );
std::ofstream ofnets ( netsPath.c_str() );
writeNetsToStream ( ofnets );
ofnets.close ();
}
if ( (flags & Scl) and (hasScl()) ) {
bfs::path sclPath ( rootDirectory );
Utilities::Path sclPath ( rootDirectory );
if ( getSclName().empty() )
sclPath /= getDesignName() + ".scl";
else
sclPath /= getSclName();
bfs::ofstream ofscl ( sclPath );
std::ofstream ofscl ( sclPath.c_str() );
writeSclToStream ( ofscl );
ofscl.close ();
}
if ( (flags & Pl) and (hasPl()) ) {
bfs::path plPath ( rootDirectory );
Utilities::Path plPath ( rootDirectory );
if ( getPlName().empty() )
plPath /= getDesignName() + ".pl";
else
plPath /= getPlName();
bfs::ofstream ofpl ( plPath );
std::ofstream ofpl ( plPath.c_str() );
writePlToStream ( ofpl );
ofpl.close ();
}

View File

@ -2,25 +2,17 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC/LIP6 2008-2010, All Rights Reserved
// Copyright (c) UPMC 2008-2013, All Rights Reserved
//
// ===================================================================
//
// $Id$
//
// x-----------------------------------------------------------------x
// | |
// | C O R I O L I S |
// +-----------------------------------------------------------------+
// | V L S I Stand - Alone Parsers / Drivers |
// | B o o k s h e l f P a r s e r |
// | |
// | Author : Jean-Paul CHAPUT |
// | E-mail : Jean-Paul.Chaput@asim.lip6.fr |
// | =============================================================== |
// | C++ Module : "./Main.cpp" |
// | *************************************************************** |
// | U p d a t e s |
// | |
// x-----------------------------------------------------------------x
// +-----------------------------------------------------------------+
#include <iostream>
@ -30,9 +22,6 @@ using namespace std;
#include <boost/program_options.hpp>
namespace boptions = boost::program_options;
#include <boost/filesystem/operations.hpp>
namespace bfs = boost::filesystem;
#include "vlsisapd/bookshelf/Exception.h"
#include "vlsisapd/bookshelf/Node.h"
#include "vlsisapd/bookshelf/Pin.h"
@ -62,8 +51,6 @@ int main ( int argc, char* argv[] )
exit ( 0 );
}
bfs::path::default_name_check ( bfs::portable_posix_name );
if ( arguments.count("aux") ) {
auto_ptr<Circuit> circuit ( Circuit::parse(arguments["aux"].as<string>()) );

View File

@ -2,25 +2,17 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC/LIP6 2008-2010, All Rights Reserved
// Copyright (c) UPMC 2008-2013, All Rights Reserved
//
// ===================================================================
//
// $Id$
//
// x-----------------------------------------------------------------x
// | |
// | C O R I O L I S |
// +-----------------------------------------------------------------+
// | V L S I Stand - Alone Parsers / Drivers |
// | B o o k s h e l f P a r s e r |
// | |
// | Author : Jean-Paul CHAPUT |
// | E-mail : Jean-Paul.Chaput@asim.lip6.fr |
// | E-mail : Jean-Paul.Chaput@lip6.fr |
// | =============================================================== |
// | C++ Module : "./Parser.cpp" |
// | *************************************************************** |
// | U p d a t e s |
// | |
// x-----------------------------------------------------------------x
// +-----------------------------------------------------------------+
#include <sstream>
@ -87,10 +79,10 @@ namespace Bookshelf {
{ }
bool Parser::_openStream ( const bfs::path& filePath )
bool Parser::_openStream ( const Utilities::Path& filePath )
{
if ( _stream.is_open() ) _closeStream();
_stream.open ( filePath );
_stream.open ( filePath.string() );
_lineno = 0;
return _stream.is_open();
@ -229,7 +221,7 @@ namespace Bookshelf {
}
void Parser::_parseNodes ( const bfs::path& nodesPath )
void Parser::_parseNodes ( const Utilities::Path& nodesPath )
{
_circuit->setNodesName ( nodesPath.string());
@ -334,7 +326,7 @@ namespace Bookshelf {
}
void Parser::_parseNets ( const bfs::path& netsPath )
void Parser::_parseNets ( const Utilities::Path& netsPath )
{
_circuit->setNetsName ( netsPath.string());
@ -377,7 +369,7 @@ namespace Bookshelf {
}
void Parser::_parseWts ( const bfs::path& nodesPath )
void Parser::_parseWts ( const Utilities::Path& nodesPath )
{
//_circuit->setWtsName ( wtsPath.string());
@ -466,7 +458,7 @@ namespace Bookshelf {
}
void Parser::_parseScl ( const bfs::path& sclPath )
void Parser::_parseScl ( const Utilities::Path& sclPath )
{
_circuit->setSclName ( sclPath.string());
@ -565,7 +557,7 @@ namespace Bookshelf {
}
void Parser::_parsePl ( const bfs::path& plPath )
void Parser::_parsePl ( const Utilities::Path& plPath )
{
_circuit->setPlName ( plPath.string());
@ -590,8 +582,8 @@ namespace Bookshelf {
Circuit* Parser::parse ( std::string designName, unsigned int flags )
{
bfs::path auxPath ( designName+".aux" );
if ( not bfs::exists(auxPath) ) {
Utilities::Path auxPath ( designName+".aux" );
if ( not auxPath.exists() ) {
throw Exception ( "%s .aux file not found.", auxPath.string().c_str() );
}
@ -649,8 +641,8 @@ namespace Bookshelf {
for ( size_t iext=0 ; iext<5 ; ++iext ) {
if ( ordereds[iext].empty() ) continue;
bfs::path slotPath ( ordereds[iext] );
if ( bfs::exists(slotPath) ) {
Utilities::Path slotPath ( ordereds[iext] );
if ( slotPath.exists() ) {
std::cout << " o Reading <" << slotPath.string() << ">" << std::endl;
switch ( iext ) {

View File

@ -2,37 +2,27 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC/LIP6 2008-2010, All Rights Reserved
// Copyright (c) UPMC 2008-2013, All Rights Reserved
//
// ===================================================================
//
// $Id$
//
// x-----------------------------------------------------------------x
// | |
// | C O R I O L I S |
// +-----------------------------------------------------------------+
// | V L S I Stand - Alone Parsers / Drivers |
// | B o o k s h e l f P a r s e r |
// | |
// | Author : Jean-Paul CHAPUT |
// | E-mail : Jean-Paul.Chaput@asim.lip6.fr |
// | E-mail : Jean-Paul.Chaput@lip6.fr |
// | =============================================================== |
// | C++ Header : "./bookshelf/Parser.h" |
// | *************************************************************** |
// | U p d a t e s |
// | |
// x-----------------------------------------------------------------x
// | C++ Header : "./vlsisapd/bookshelf/Parser.cpp" |
// +-----------------------------------------------------------------+
#ifndef __VLSISAPD_BOOKSHELF_PARSER__
#define __VLSISAPD_BOOKSHELF_PARSER__
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
namespace bfs = boost::filesystem;
#ifndef VLSISAPD_BOOKSHELF_PARSER_H
#define VLSISAPD_BOOKSHELF_PARSER_H
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include "vlsisapd/utilities/Path.h"
namespace Bookshelf {
@ -45,26 +35,26 @@ namespace Bookshelf {
Parser ();
Circuit* parse ( std::string designName, unsigned int flags );
private:
bool _openStream ( const bfs::path& );
bool _openStream ( const Utilities::Path& );
void _closeStream ();
char* _readLine ();
void _tokenize ();
void _checkExtraDatas ( size_t maxtoken, std::vector<char*>& );
void _parseFormatRevision ( const std::string& slotName );
size_t _parseNum ( const std::string& slotName, const std::string& token );
void _parseNodes ( const bfs::path& );
void _parseNodes ( const Utilities::Path& );
void _parseNodesNode ();
void _parseNets ( const bfs::path& );
void _parseNets ( const Utilities::Path& );
void _parseNetsNetDegree ();
void _parseNetsPin ();
void _parseWts ( const bfs::path& );
void _parseScl ( const bfs::path& );
void _parseWts ( const Utilities::Path& );
void _parseScl ( const Utilities::Path& );
void _parseSclCoreRow ();
void _parseSclSiteorient ();
void _parseSclSitesymmetry ();
void _parseSclSubrowOrigin ();
void _parseSclCorerowEnd ();
void _parsePl ( const bfs::path& );
void _parsePl ( const Utilities::Path& );
void _parsePlNodePlace ();
inline bool _isComment () const;
inline bool _hasExtraDatas () const;
@ -72,7 +62,7 @@ namespace Bookshelf {
enum Misc { BufferSize=4096 };
private:
size_t _lineno;
bfs::ifstream _stream;
std::ifstream _stream;
char _buffer[BufferSize];
std::vector<char*> _tokens;
unsigned int _flags;
@ -87,7 +77,6 @@ namespace Bookshelf {
inline bool Parser::_hasExtraDatas () const { return _flags&ExtraDatas; }
} // End of Bookshelf namespace.
} // Bookshelf namespace.
#endif // __VLSISAPD_BOOKSHELF_PARSER__
#endif // VLSISAPD_BOOKSHELF_PARSER_H

View File

@ -1,7 +1,8 @@
include ( ${QT_USE_FILE} )
include_directories ( ${VLSISAPD_SOURCE_DIR}/src/configuration/src
include_directories ( ${VLSISAPD_SOURCE_DIR}/src/utilities/src
${VLSISAPD_SOURCE_DIR}/src/configuration/src
${VLSISAPD_SOURCE_DIR}/src/openChams/src
${Boost_INCLUDE_DIRS}
${PYTHON_INCLUDE_PATH}
@ -40,7 +41,8 @@
add_library ( configuration ${cpps} ${mocCpps} ${RCC_SRCS} )
set_target_properties ( configuration PROPERTIES VERSION 1.0 SOVERSION 1 )
target_link_libraries ( configuration ${QT_LIBRARIES}
target_link_libraries ( configuration vlsisapdutils
${QT_LIBRARIES}
${PYTHON_LIBRARIES}
${LIBXML2_LIBRARIES}
${Boost_LIBRARIES}

View File

@ -2,7 +2,7 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC/LIP6 2008-2012, All Rights Reserved
// Copyright (c) UPMC 2008-2013, All Rights Reserved
//
// +-----------------------------------------------------------------+
// | V L S I Stand - Alone Parsers / Drivers |
@ -22,13 +22,11 @@
#include <boost/program_options.hpp>
namespace boptions = boost::program_options;
#include <boost/filesystem/operations.hpp>
namespace bfs = boost::filesystem;
#include <QtGui>
#if (QT_VERSION >= QT_VERSION_CHECK(4,5,0)) and not defined (__APPLE__)
# include <QGtkStyle>
#endif
#include "vlsisapd/utilities/Path.h"
#include "vlsisapd/configuration/Configuration.h"
#include "vlsisapd/configuration/ConfigurationWidget.h"
#include "vlsisapd/configuration/ConfEditorWidget.h"
@ -67,13 +65,11 @@ int main ( int argc, char* argv[] )
if ( not disableGtkStyle ) qa->setStyle ( new QGtkStyle() );
#endif
bfs::path::default_name_check ( bfs::portable_posix_name );
Configuration* conf = Configuration::get ();
if ( arguments.count("conf") ) {
bfs::path confPath = ( arguments["conf"].as<string>() );
if ( bfs::exists(confPath) ) {
Utilities::Path confPath = ( arguments["conf"].as<string>() );
if ( confPath.exists() ) {
cout << "Reading configuration: <" << confPath.string() << ">." << endl;
conf->readFromFile ( confPath.string() );
} else {
@ -81,8 +77,8 @@ int main ( int argc, char* argv[] )
}
}
bfs::path dotConfPath ( "./.coriolis2.configuration.xml" );
if ( bfs::exists(dotConfPath) ) {
Utilities::Path dotConfPath ( "./.coriolis2.configuration.xml" );
if ( dotConfPath.exists() ) {
cout << "Reading dot configuration: <" << dotConfPath.string() << ">." << endl;
conf->readFromFile ( dotConfPath.string() );
}
@ -90,8 +86,8 @@ int main ( int argc, char* argv[] )
//cout << "misc.catchCore: " << conf->getParameter("misc.catchCore" )->asBool() << endl;
//cout << "kite.eventsLimit: " << conf->getParameter("kite.eventsLimit")->asInt () << endl;
bfs::path pyDotConfPath ( "./.coriolis2.configuration.py" );
if ( bfs::exists(pyDotConfPath) ) {
Utilities::Path pyDotConfPath ( "./.coriolis2.configuration.py" );
if ( pyDotConfPath.exists() ) {
cout << "Reading python dot configuration: <" << pyDotConfPath.string() << ">." << endl;
Py_Initialize ();
FILE* fd = fopen ( pyDotConfPath.string().c_str(), "r" );

View File

@ -2,11 +2,7 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC/LIP6 2008-2011, All Rights Reserved
//
// ===================================================================
//
// $Id$
// Copyright (c) UPMC 2008-2013, All Rights Reserved
//
// +-----------------------------------------------------------------+
// | C O R I O L I S |
@ -20,9 +16,7 @@
#include <iostream>
#include <boost/filesystem/operations.hpp>
namespace bfs = boost::filesystem;
#include "vlsisapd/utilities/Path.h"
#include "vlsisapd/configuration/Parameter.h"
#include "vlsisapd/configuration/Configuration.h"
@ -304,8 +298,8 @@ namespace Cfg {
if ( (flags & MustExist) and hasFlags(MustExist) ) {
if ( _type == String ) {
bfs::path filePath = ( svalue.str() );
if ( not bfs::exists(filePath) ) {
Utilities::Path filePath ( svalue.str() );
if ( not filePath.exists() ) {
//cerr << " needrestart" << _id << endl;
configuration->addLog ( Configuration::LogNeedExist, _id );
} else

View File

@ -0,0 +1 @@
ADD_SUBDIRECTORY(src)

View File

@ -0,0 +1,17 @@
include_directories ( ${VLSISAPD_SOURCE_DIR}/src/utilities/src
)
set ( includes vlsisapd/utilities/Path.h
)
set ( cpps Path.cpp
)
add_library ( vlsisapdutils ${cpps} )
set_target_properties ( vlsisapdutils PROPERTIES VERSION 1.0 SOVERSION 1 )
install ( TARGETS vlsisapdutils DESTINATION lib${LIB_SUFFIX} )
install ( FILES ${includes}
DESTINATION include/vlsisapd/utilities )

View File

@ -0,0 +1,265 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC 2013-2013, All Rights Reserved
//
// +-----------------------------------------------------------------+
// | V L S I Stand - Alone Parsers / Drivers |
// | M i s c e l l a n e o u s U t i l i t i e s |
// | |
// | Author : Jean-Paul CHAPUT |
// | E-mail : Jean-Paul.Chaput@lip6.fr |
// | =============================================================== |
// | C++ Module : "./Path.cpp" |
// +-----------------------------------------------------------------+
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <sstream>
using namespace std;
#include "vlsisapd/utilities/Path.h"
namespace Utilities {
int Path::_toUnistd ( unsigned int mode )
{
int unimode = 0;
if (mode & PermRead ) unimode |= R_OK;
if (mode & PermWrite ) unimode |= W_OK;
if (mode & PermExecute) unimode |= X_OK;
return unimode;
}
int Path::_toMode_T ( unsigned int mode )
{
int unimode = 0;
if (mode & s_isuid ) unimode |= S_ISUID;
if (mode & s_isgid ) unimode |= S_ISGID;
if (mode & s_isvtx ) unimode |= S_ISVTX;
if (mode & s_irusr ) unimode |= S_IRUSR;
if (mode & s_iwusr ) unimode |= S_IWUSR;
if (mode & s_ixusr ) unimode |= S_IXUSR;
if (mode & s_irgrp ) unimode |= S_IRGRP;
if (mode & s_iwgrp ) unimode |= S_IWGRP;
if (mode & s_ixgrp ) unimode |= S_IXGRP;
if (mode & s_iroth ) unimode |= S_IROTH;
if (mode & s_iwoth ) unimode |= S_IWOTH;
if (mode & s_ixoth ) unimode |= S_IXOTH;
return unimode;
}
void Path::_split ( const std::string& path, vector<std::string>& elements, unsigned int& flags )
{
if (path.empty()) return;
size_t begin = 0;
if (path[0] == '/') {
flags |= Absolute;
begin = 1;
}
size_t end = path.find_first_of( '/', begin );
while ( end != string::npos ) {
elements.push_back(path.substr(begin,end-begin));
begin = end+1;
end = path.find_first_of( '/', begin );
}
elements.push_back(path.substr(begin));
}
void Path::_normalize ()
{
if ( (size() == 1) and (_elements[0] == ".") ) return;
if (not _elements.empty() and (_elements[0] == "~")) {
char* home = getenv( "HOME" );
if (home) {
vector<std::string> vhome;
unsigned int vflags = 0;
_split( home, vhome, vflags );
_flags |= vflags;
_elements.insert( _elements.begin(), vhome.begin(), vhome.end() );
}
}
vector<std::string>::iterator ie = _elements.begin();
while ( ie != _elements.end() ) {
if ( ((*ie) == ".") or (*ie).empty()) {
_elements.erase(ie);
} else {
++ie;
}
}
ie = _elements.begin();
while ( ie != _elements.end() ) {
if ( ((*ie) == "..") and (ie != _elements.begin()) ) {
--ie;
_elements.erase(ie,ie+2);
} else {
++ie;
}
}
if (not _elements.empty() and (_elements.back().find_last_of('.') != std::string::npos))
_flags |= Extension;
// cout << "_normalize(" << path << ")" << endl;
// cout << " Absolute: " << boolalpha << (bool)(_flags & Absolute) << endl;
// for ( size_t i=0 ; i<_elements.size() ; ++i ) {
// cout << " " << i << ": <" << _elements[i] << '>' << endl;
// }
}
void Path::_normalize ( const std::string& path )
{
_split( path, _elements, _flags );
_normalize();
}
std::string Path::ext () const
{
if (not (_flags&Extension)) return "";
return _elements.back().substr(_elements.back().find_last_of('.')+1);
}
bool Path::mkdir ( unsigned int mode ) const
{
int status = ::mkdir( c_str(), _toMode_T(mode) );
return (status == 0);
}
const std::string& Path::string () const
{
_pathcache.clear();
for ( size_t i=0 ; i<size() ; ++i ) {
if ((i != 0) or (_flags & Absolute)) {
_pathcache += '/';
}
_pathcache += _elements[i];
}
return _pathcache;
}
Path Path::subpath ( size_t begin, size_t end ) const
{
if ((begin > size()) or (begin > end)) return Path();
if (end > size()) end = size()-1;
Path sub;
for ( size_t i=begin; i<end ; ++i )
sub._elements.push_back( _elements[i] );
if ((begin == 0) and (_flags & Absolute)) sub._flags |= Absolute;
return sub;
}
Path Path::stem () const
{
std::string dotext = ext();
if (not dotext.empty()) dotext.insert( 0, "." );
return basename(dotext);
}
Path Path::basename ( const std::string& ext ) const
{
if (empty()) return Path();
std::string basename = _elements.back();
if (not ext.empty() and (basename.substr(basename.size()-ext.size()) == ext))
basename = basename.substr(0,basename.size()-ext.size());
return Path(basename);
}
Path Path::dirname () const
{
if (empty()) return Path();
if (size() == 1) return Path(".");
return subpath( 0, size()-1 );
}
bool Path::exists () const
{
struct stat statinfos;
int status = 0;
status = stat( string().c_str(), &statinfos );
if (not status) return true;
return false;
}
bool Path::access ( unsigned int mode ) const
{
int status = 0;
status = ::access( string().c_str(), _toUnistd(mode) );
if (not status) return true;
return false;
}
Path Path::join ( const Path& tail ) const
{
Path result ( *this );
result._elements.insert( result._elements.end(), tail._elements.begin(), tail._elements.end() );
result._normalize();
return result;
}
Path& Path::operator/= ( const Path& tail )
{
_elements.insert( _elements.end(), tail._elements.begin(), tail._elements.end() );
_normalize();
return (*this);
}
Path& Path::operator= ( const Path& other )
{
vector<std::string>().swap( _elements );
_elements = other._elements;
_flags = other._flags;
_pathcache = other._pathcache;
return *this;
}
Path Path::cwd ()
{
static char curdir [4096];
if (::getcwd( curdir, 4096 ) == NULL) {
strcpy( curdir, "/cwd/has/failed");
}
return Path(curdir);
}
} // Utilities namespace.

View File

@ -0,0 +1,124 @@
// -*- C++ -*-
//
// This file is part of the VSLSI Stand-Alone Software.
// Copyright (c) UPMC 2013-2013, All Rights Reserved
//
// +-----------------------------------------------------------------+
// | V L S I Stand - Alone Parsers / Drivers |
// | M i s c e l l a n e o u s U t i l i t i e s |
// | |
// | Author : Jean-Paul CHAPUT |
// | E-mail : Jean-Paul.Chaput@lip6.fr |
// | =============================================================== |
// | C++ Header : "./utilities/Path.h" |
// +-----------------------------------------------------------------+
#ifndef VLSISAPD_UTILITIES_PATH_H
#define VLSISAPD_UTILITIES_PATH_H
#include <string>
#include <vector>
namespace Utilities {
class Path {
public:
enum Flag { NoFlag = 0x00000000
, Absolute = 0x00000001
, Extension = 0x00000002
, PermRead = 0x00000004
, PermWrite = 0x00000008
, PermExecute = 0x00000010
, PermAll = PermRead|PermWrite|PermExecute
};
enum Mode_T { s_ifmt = 0x00170000
, s_ifsock = 0x00140000
, s_iflnk = 0x00120000
, s_ifreg = 0x00100000
, s_ifblk = 0x00060000
, s_ifdir = 0x00040000
, s_ifchr = 0x00020000
, s_ififo = 0x00010000
, s_isuid = 0x00004000
, s_isgid = 0x00002000
, s_isvtx = 0x00001000
, s_irusr = 0x00000400
, s_iwusr = 0x00000200
, s_ixusr = 0x00000100
, s_irwxu = s_irusr|s_iwusr|s_ixusr
, s_irgrp = 0x00000040
, s_iwgrp = 0x00000020
, s_ixgrp = 0x00000010
, s_irwxg = s_irgrp|s_iwgrp|s_ixgrp
, s_iroth = 0x00000004
, s_iwoth = 0x00000002
, s_ixoth = 0x00000001
, s_irwxo = s_iroth|s_iwoth|s_ixoth
};
public:
static Path cwd ();
public:
inline Path ( const std::string& path="" );
inline Path ( const char* );
inline Path ( const Path& );
inline bool absolute () const;
inline bool extension () const;
bool exists () const;
bool access ( unsigned int mode ) const;
inline bool empty () const;
inline size_t size () const;
inline Path head () const;
inline Path tail () const;
Path basename ( const std::string& ext="" ) const;
Path dirname () const;
Path stem () const;
Path subpath ( size_t begin, size_t end ) const;
Path join ( const Path& tail ) const;
std::string ext () const;
bool mkdir ( unsigned int mode=s_irwxu|s_irwxg|s_irwxo ) const;
const std::string& string () const;
inline const char* c_str () const;
Path& operator= ( const Path& );
Path& operator/= ( const Path& );
inline Path& operator/= ( const char* );
inline Path& operator/= ( const std::string& );
private:
static int _toUnistd ( unsigned int mode );
static int _toMode_T ( unsigned int mode );
static void _split ( const std::string&, std::vector<std::string>&, unsigned int& );
void _normalize ();
void _normalize ( const std::string& );
private:
unsigned int _flags;
std::vector<std::string> _elements;
mutable std::string _pathcache;
};
inline Path::Path ( const char* path ) : _flags(NoFlag), _elements(), _pathcache() { _normalize(path); }
inline Path::Path ( const std::string& path ) : _flags(NoFlag), _elements(), _pathcache() { _normalize(path); }
inline Path::Path ( const Path& path ) : _flags(path._flags), _elements(path._elements), _pathcache(path._pathcache) {}
inline bool Path::absolute () const { return _flags&Absolute; }
inline bool Path::extension () const { return _flags&Extension; }
inline bool Path::empty () const { return _elements.empty(); }
inline size_t Path::size () const { return _elements.size(); }
inline Path Path::head () const { return (size() > 1) ? subpath(0,size()-1) : *this; }
inline Path Path::tail () const { return (size() > 1) ? subpath(1,size()) : *this; }
inline const char* Path::c_str () const { return Path::string().c_str(); }
inline Path& Path::operator/= ( const std::string& tail ) { (*this) /= Path(tail); return *this; }
inline Path& Path::operator/= ( const char* tail ) { (*this) /= Path(tail); return *this; }
inline Path operator/ ( const Path& rop, const Path& lop ) { return rop.join(lop); }
inline Path operator/ ( const std::string& rop, const Path& lop ) { return Path(rop) / lop; }
inline Path operator/ ( const Path& rop, const char* lop ) { return rop / Path(lop); }
inline Path operator/ ( const Path& rop, const std::string& lop ) { return rop / Path(lop); }
} // Utilities namespace.
#endif // VLSISAPD_UTILITIES_PATH_H