root/library/bdm/osutils.cpp @ 737

Revision 737, 2.1 kB (checked in by mido, 15 years ago)

ASTYLER RUN OVER THE WHOLE LIBRARY, JUPEE

  • 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
[408]37        sprintf ( filename, "%s\\%s", dirname.c_str(), f.c_str() );
[93]38#else
[408]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
[415]45        if ( _mkdir ( dirname.c_str() ) == -1 )   // Create the directory
[93]46#else
[408]47        if ( mkdir ( dirname.c_str(), 00755 ) == -1 )   // Create the directory
[93]48#endif
[408]49        {
[565]50                if ( ( rewrite ) && ( errno == EEXIST ) ) bdm_warning ( "rewriting directory" );
51                else bdm_error ( "dirfilelog:: cannot create directory" );
[408]52        }
[93]53}
[497]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;
[565]82                        bdm_error ( msg );
[497]83                }
84        } else {
[737]85                if ( ( errno == ENOTDIR ) || // Linux
86                        ( errno == EINVAL ) ) { // Windows
[497]87                        if ( unlink ( path ) ) {
88                                std::string msg = "can't remove file ";
89                                msg += path;
[565]90                                bdm_error ( msg );
[497]91                        }
92                } else {
93                        if ( errno != ENOENT ) {
94                                std::string msg = "can't remove ";
95                                msg += path;
[565]96                                bdm_error ( msg );
[497]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.