Changeset 860

Show
Ignore:
Timestamp:
01/26/08 13:25:16
Author:
robert
Message:

Added mkpath convinience function

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/include/vpb/FileUtils

    r858 r860  
    6565extern VPB_EXPORT int gethostname(char *name, size_t namelen); 
    6666extern VPB_EXPORT int getdtablesize(); 
    67 extern VPB_EXPORT int mkdir(const char *__path, int mode); 
     67extern VPB_EXPORT int mkdir(const char *path, int mode); 
     68 
     69extern VPB_EXPORT int mkpath(const char *path, int mode); 
    6870 
    6971}; 
  • trunk/src/vpb/DataSet.cpp

    r859 r860  
    3535#include <vpb/TaskManager> 
    3636#include <vpb/System> 
     37#include <vpb/FileUtils> 
    3738 
    3839#include <vpb/ShapeFilePlacer> 
     
    27902791            } 
    27912792        } 
    2792          
     2793 
     2794        int result = 0; 
     2795        osgDB::FileType type = osgDB::fileType(getDirectory()); 
     2796        if (type==osgDB::DIRECTORY) 
     2797        { 
     2798            log(osg::NOTICE,"   Base Directory already created"); 
     2799        }  
     2800        else if (type==osgDB::REGULAR_FILE) 
     2801        { 
     2802            log(osg::NOTICE,"   Error cannot create directory as a conventional file already exists with that name"); 
     2803            return 1; 
     2804        } 
     2805        else // FILE_NOT_FOUND 
     2806        { 
     2807            // need to create directory. 
     2808            result = vpb::mkpath(getDirectory().c_str(), S_IRWXU | S_IRWXG | S_IRWXO); 
     2809        } 
     2810 
     2811        if (result) 
     2812        { 
     2813            log(osg::NOTICE,"Error: could not create directory %i",errno); 
     2814            return 1; 
     2815        } 
     2816 
     2817 
    27932818        if (getOutputTaskDirectories()) 
    27942819        { 
    27952820            _taskOutputDirectory = getDirectory() + getTaskName(getSubtileLevel(), getSubtileX(), getSubtileY()); 
    27962821            log(osg::NOTICE,"Need to create output task directory = %s", _taskOutputDirectory.c_str()); 
    2797             int result = 0; 
    2798             osgDB::FileType type = osgDB::fileType(_taskOutputDirectory); 
     2822            result = 0; 
     2823            type = osgDB::fileType(_taskOutputDirectory); 
    27992824            if (type==osgDB::DIRECTORY) 
    28002825            { 
     
    28042829            { 
    28052830                log(osg::NOTICE,"   Error cannot create directory as a conventional file already exists with that name"); 
    2806                 result = 1; 
     2831                return 1; 
    28072832            } 
    28082833            else // FILE_NOT_FOUND 
    28092834            { 
    28102835                // need to create directory. 
    2811                 result = mkdir(_taskOutputDirectory.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); 
     2836                result = vpb::mkpath(_taskOutputDirectory.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); 
    28122837            } 
    28132838             
  • trunk/src/vpb/FileUtils.cpp

    r858 r860  
    11#include <vpb/FileUtils> 
     2#include <vpb/BuildLog> 
     3#include <osgDB/FileUtils> 
    24 
    35#ifdef WIN32 
     
    6264 
    6365#endif  // WIN32 
     66 
     67 
     68int vpb::mkpath(const char *path, int mode) 
     69{ 
     70    if (path==0) return 0; 
     71     
     72    std::string fullpath(path); 
     73    typedef std::list<std::string> Directories; 
     74    Directories directories; 
     75    int pos_start = 0; 
     76    for(int pos_current = 0; pos_current<fullpath.size(); ++pos_current) 
     77    { 
     78        if (fullpath[pos_current]=='\\' || fullpath[pos_current]=='/') 
     79        { 
     80            int size = pos_current-pos_start; 
     81            if (size>1) 
     82            { 
     83                directories.push_back(std::string(fullpath,0, pos_current)); 
     84            } 
     85            pos_start = pos_current+1; 
     86        } 
     87    } 
     88    int size = fullpath.size()-pos_start; 
     89    if (size>1) 
     90    { 
     91        directories.push_back(fullpath); 
     92    } 
     93     
     94    vpb::log(osg::NOTICE,"mkpath(%s)",path); 
     95    for(Directories::iterator itr = directories.begin(); 
     96        itr != directories.end(); 
     97        ++itr) 
     98    { 
     99        vpb::log(osg::NOTICE,"  directory %s",(*itr).c_str()); 
     100 
     101        int result = 0; 
     102        osgDB::FileType type = osgDB::fileType((*itr)); 
     103        if (type==osgDB::DIRECTORY) 
     104        { 
     105            log(osg::NOTICE,"      Base Directory already created"); 
     106        }  
     107        else if (type==osgDB::REGULAR_FILE) 
     108        { 
     109            log(osg::NOTICE,"      Error cannot create directory as a conventional file already exists with that name"); 
     110            return 1; 
     111        } 
     112        else // FILE_NOT_FOUND 
     113        { 
     114            // need to create directory. 
     115            result = vpb::mkdir((*itr).c_str(), mode); 
     116            if (result) return result; 
     117        } 
     118    } 
     119     
     120    return 0; 
     121     
     122}