root/library/bdm/osutils.cpp @ 1064

Revision 1064, 2.5 kB (checked in by mido, 14 years ago)

astyle applied all over the library

  • Property svn:eol-style set to native
RevLine 
[93]1//
2// C++ Implementation: osutils
3//
4// Description:
5//
6//
7// Author: smidl <smidl@utia.cas.cz>, (C) 2008
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12
13#include "osutils.h"
[565]14#include "bdmerror.h"
[93]15#include <iostream>
[497]16#include <stdexcept>
17#include <string>
[93]18
19#ifdef WIN32
[497]20#include "dirent.h"
[93]21#include <direct.h>
[497]22#define rmdir _rmdir
23#define unlink _unlink
[93]24#else
[497]25#include <dirent.h>
26#include <unistd.h>
[93]27#include <sys/types.h>
28#include <sys/stat.h>
29#endif
30
31#include <fcntl.h>
32#include <errno.h>
33
34
35void get_fname ( char* filename, string &dirname, string &f ) {
36#ifdef WIN32
[1064]37    sprintf ( filename, "%s\\%s", dirname.c_str(), f.c_str() );
[93]38#else
[1064]39    sprintf ( filename, "%s/%s", dirname.c_str() , f.c_str() );
[93]40#endif
41}
42
[408]43void makedir ( string &dirname, bool rewrite ) {
[93]44#ifdef WIN32
[1064]45    if ( _mkdir ( dirname.c_str() ) == -1 )   // Create the directory
[93]46#else
[1064]47    if ( mkdir ( dirname.c_str(), 00755 ) == -1 )   // Create the directory
[93]48#endif
[1064]49    {
50        if ( ( rewrite ) && ( errno == EEXIST ) ) bdm_warning ( "rewriting directory" );
51        else bdm_error ( "dirfilelog:: cannot create directory" );
52    }
[93]53}
[497]54
55bool remove_all ( const char *path ) {
[1064]56    DIR *dir;
57    dirent *de;
[497]58
[1064]59    bool rv = true;
60    if ( ( dir = opendir ( path ) ) != 0 ) {
61        try {
62            std::string top ( path );
63            top += "/";
[497]64
[1064]65            while ( ( de = readdir ( dir ) ) != 0 ) {
66                if ( strcmp ( de->d_name, "." ) && strcmp ( de->d_name, ".." ) ) {
67                    std::string subpath ( top );
68                    subpath += de->d_name;
69                    remove_all ( subpath.c_str() );
70                }
71            }
72        } catch ( ... ) {
73            closedir ( dir );
74            throw;
75        }
[497]76
[1064]77        closedir ( dir );
[497]78
[1064]79        if ( rmdir ( path ) ) {
80            std::string msg = "can't remove dir ";
81            msg += path;
82            bdm_error ( msg );
83        }
84    } else {
85        if ( ( errno == ENOTDIR ) || // Linux
86                ( errno == EINVAL ) ) { // Windows
87            if ( unlink ( path ) ) {
88                std::string msg = "can't remove file ";
89                msg += path;
90                bdm_error ( msg );
91            }
92        } else {
93            if ( errno != ENOENT ) {
94                std::string msg = "can't remove ";
95                msg += path;
96                bdm_error ( msg );
97            } else {
98                // it wasn't there in the first place
99                rv = false;
100            }
101        }
102    }
[497]103
[1064]104    return rv;
[497]105}
Note: See TracBrowser for help on using the browser.