root/library/bdm/osutils.cpp @ 497

Revision 497, 2.1 kB (checked in by vbarta, 15 years ago)

moved cross-platform directory scanning support from tests to core (osutils)

  • 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 <iostream>
15#include <stdexcept>
16#include <string>
17
18#ifdef WIN32
19#include "dirent.h"
20#include <direct.h>
21#define rmdir _rmdir
22#define unlink _unlink
23#else
24#include <dirent.h>
25#include <unistd.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#endif
29
30#include <fcntl.h>
31#include <errno.h>
32
33
34void get_fname ( char* filename, string &dirname, string &f ) {
35#ifdef WIN32
36        sprintf ( filename, "%s\\%s", dirname.c_str(), f.c_str() );
37#else
38        sprintf ( filename, "%s/%s", dirname.c_str() , f.c_str() );
39#endif
40}
41
42void makedir ( string &dirname, bool rewrite ) {
43#ifdef WIN32
44        if ( _mkdir ( dirname.c_str() ) == -1 )   // Create the directory
45#else
46        if ( mkdir ( dirname.c_str(), 00755 ) == -1 )   // Create the directory
47#endif
48        {
49                if ( ( rewrite ) && ( errno == EEXIST ) ) it_warning ( "rewriting directory" );
50                else it_error ( "dirfilelog:: cannot create directory" );
51        }
52}
53
54bool remove_all ( const char *path ) {
55        DIR *dir;
56        dirent *de;
57
58        bool rv = true;
59        if ( ( dir = opendir ( path ) ) != 0 ) {
60                try {
61                        std::string top ( path );
62                        top += "/";
63
64                        while ( ( de = readdir ( dir ) ) != 0 ) {
65                                if ( strcmp ( de->d_name, "." ) && strcmp ( de->d_name, ".." ) ) {
66                                        std::string subpath ( top );
67                                        subpath += de->d_name;
68                                        remove_all ( subpath.c_str() );
69                                }
70                        }
71                } catch ( ... ) {
72                        closedir ( dir );
73                        throw;
74                }
75
76                closedir ( dir );
77
78                if ( rmdir ( path ) ) {
79                        std::string msg = "can't remove dir ";
80                        msg += path;
81                        throw std::runtime_error ( msg );
82                }
83        } else {
84                if ( ( errno == ENOTDIR ) || // Linux
85                     ( errno == EINVAL ) ) { // Windows
86                        if ( unlink ( path ) ) {
87                                std::string msg = "can't remove file ";
88                                msg += path;
89                                throw std::runtime_error ( msg );
90                        }
91                } else {
92                        if ( errno != ENOENT ) {
93                                std::string msg = "can't remove ";
94                                msg += path;
95                                throw std::runtime_error ( msg );
96                        } else {
97                                // it wasn't there in the first place
98                                rv = false;
99                        }
100                }
101        }
102
103        return rv;
104}
Note: See TracBrowser for help on using the browser.