Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/filesys/directory.cc @ 10585

Last change on this file since 10585 was 9876, checked in by bensch, 18 years ago

compiles better on Windows

File size: 4.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14
15   Borrowed the Main code from 'Bart Vanhauwaert' (license below)
16   and adopted it to my likings.
17*/
18
19/**
20 * Copyright (C) 2002 Bart Vanhauwaert
21 *
22 * Permission to use, copy, modify, distribute and sell this software
23 * for any purpose is hereby granted without fee. This license
24 * includes (but is not limited to) standalone compilation or as part
25 * of a larger project.
26 *
27 * This software is provided "as is" without express or implied warranty.
28 *
29 * For a full statement on warranty and terms and conditions for
30 * copying, distribution and modification, please see the comment block
31 * at the end of this file.
32 *
33 * Version 1
34 *
35 */
36
37#include "directory.h"
38
39#if not defined (__WIN32__)
40#include <sys/types.h>
41#include <sys/stat.h>
42#include <dirent.h>
43const char Directory::delimiter = '/';
44#else
45#include <windows.h>
46#include <winbase.h>
47const char Directory::delimiter = '\\';
48#endif
49
50#include <iostream>
51
52/**
53 * @brief Constructs a Directory handler.
54 * @param directoryName the name of the Directory to access.
55 */
56Directory::Directory(const std::string& directoryName)
57    : File(directoryName)
58{
59  this->_opened = false;
60}
61
62/**
63 * @brief construct a Copy of directory.
64 * @param directory the Directory to copy.
65 */
66Directory::Directory(const Directory& directory)
67    : File(directory)
68{
69  this->_opened = directory._opened;
70  this->_fileNames = directory._fileNames;
71}
72
73
74/**
75 * @brief destructs the Directory.
76 */
77Directory::~Directory()
78{
79  this->close();
80}
81
82
83/**
84 * @brief openes the Directory
85 * @returns true on success, false on error. (does not exist, no rights -> test with functions of File)
86 *
87 * Fills the List of Files, and sets the Directory to open state
88 */
89bool Directory::open()
90{
91  if (this->_opened)
92    this->close();
93
94  // Openes the Directory for reading:
95#if not defined(__WIN32__)
96  DIR* handle;
97  handle = opendir(this->name().c_str());
98  if (!handle)
99  {
100    std::cerr << "could not open directory " << this->name() << " for reading" << std::endl;
101    return false;
102  }
103
104  dirent* entry;
105  while ((entry = readdir(handle)) != NULL)
106    this->_fileNames.push_back(entry->d_name);
107  closedir(handle);
108
109#else
110  HANDLE handle;
111
112  // First check the attributes trying to access a non-Directory with
113  // FindFirstFile takes ages
114  DWORD attrs = GetFileAttributes(this->name().c_str());
115  if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )
116  {
117    return false;
118  }
119  std::string Full(this->name());
120  // Circumvent a problem in FindFirstFile with c:\\* as parameter
121  if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )
122    Full += "\\";
123  WIN32_FIND_DATA entry;
124  handle = FindFirstFile( (Full+"*").c_str(), &entry);
125  if (handle == INVALID_HANDLE_VALUE)
126  {
127    std::cerr << "could not open directory " << this->name() << " for reading" << std::endl;
128    return false;
129  }
130  else
131  {
132    this->_fileNames.push_back(entry.cFileName);
133  }
134  int ok;
135  while ((ok = FindNextFile(handle, &entry)) != 0)
136    this->_fileNames.push_back(entry.cFileName);
137  FindClose(handle);
138#endif /* __WIN_32__ */
139
140  this->_opened = true;
141  return true;
142}
143
144/**
145 * @brief closes the directory
146 * @returns true.
147 *
148 * Clears the List of Files in the Directory.
149 */
150bool Directory::close()
151{
152  this->_opened = false;
153  this->_fileNames.clear();
154  return true;
155}
156
157
158/**
159 * @brief creates the directory
160 * @returns true on success, false on error
161 */
162bool Directory::create()
163{
164#if not defined (__WIN32__)
165  return (!mkdir(this->name().c_str(), 0777));
166#else
167  return (!CreateDirectory(this->name().c_str(), NULL));
168#endif
169}
170
171
172Directory Directory::operator+(const Directory& dir) const
173{
174  return Directory(*this) += dir;
175}
176
177/**
178 * @param dir the Directory to append to this one (say this one is "/var", then dir can be "log")
179 * @returns The Directory appended by dir.
180 *
181 * @note the Directoy will again be closed even if it was opened previously!
182 */
183Directory& Directory::operator+=(const Directory& dir)
184{
185  this->setFileName(this->name() + Directory::delimiter + dir.name());
186  return *this;
187}
188
189/**
190 * @brief Traverses the Directory tree one step up. (Parent Directory)
191 * @returns a Reference to the Directory.
192 */
193Directory& Directory::operator--()
194{
195}
196
197
198/**
199 * @brief Traverses the Directory tree one step up. (Parent Directory)
200 * @param int the PostFix iterator
201 * @returns a Reference to the Directory.
202 */
203Directory& Directory::operator--(int)
204{
205}
206
207/**
208 * @returns The Parent Directory.
209 */
210Directory Directory::parentDir() const
211{
212
213}
214
215File operator+(const Directory& dir, const File& file)
216{
217  return File(dir.name() + Directory::delimiter + file.name());
218}
Note: See TracBrowser for help on using the repository browser.