root/library/bdm/osutils.cpp @ 1409

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
Line 
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"
14#include "bdmerror.h"
15#include <iostream>
16#include <stdexcept>
17#include <string>
18
19#ifdef WIN32
20#include "dirent.h"
21#include <direct.h>
22#define rmdir _rmdir
23#define unlink _unlink
24#else
25#include <dirent.h>
26#include <unistd.h>
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
37    sprintf ( filename, "%s\\%s", dirname.c_str(), f.c_str() );
38#else
39    sprintf ( filename, "%s/%s", dirname.c_str() , f.c_str() );
40#endif
41}
42
43void makedir ( string &dirname, bool rewrite ) {
44#ifdef WIN32
45    if ( _mkdir ( dirname.c_str() ) == -1 )   // Create the directory
46#else
47    if ( mkdir ( dirname.c_str(), 00755 ) == -1 )   // Create the directory
48#endif
49    {
50        if ( ( rewrite ) && ( errno == EEXIST ) ) bdm_warning ( "rewriting directory" );
51        else bdm_error ( "dirfilelog:: cannot create directory" );
52    }
53}
54
55bool remove_all ( const char *path ) {
56    DIR *dir;
57    dirent *de;
58
59    bool rv = true;
60    if ( ( dir = opendir ( path ) ) != 0 ) {
61        try {
62            std::string top ( path );
63            top += "/";
64
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        }
76
77        closedir ( dir );
78
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    }
103
104    return rv;
105}
Note: See TracBrowser for help on using the browser.