Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9370 was 8523, checked in by bensch, 19 years ago

trunk: compiles on Windows: DANGER: THE LINUX PORT MAY BE COMPROMISED

File size: 3.8 KB
RevLine 
[7610]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"
[7624]38
39#if not defined (__WIN32__)
[8276]40#include <sys/types.h>
41#include <sys/stat.h>
42#include <dirent.h>
43#else
44#include <windows.h>
45#include <winbase.h>
[7624]46#endif
[8276]47
48#include <iostream>
49
50/**
51 * @brief Constructs a Directory handler.
52 * @param directoryName the name of the Directory to access.
53 */
[7610]54Directory::Directory(const std::string& directoryName)
[8276]55    : File(directoryName)
[7610]56{
[8276]57  this->_opened = false;
[7610]58}
59
[8276]60/**
[8332]61 * @brief construct a Copy of directory.
62 * @param directory the Directory to copy.
63 */
64Directory::Directory(const Directory& directory)
65  : File(directory)
66{
67  this->_opened = directory._opened;
68  this->_fileNames = directory._fileNames;
69}
70
71
72/**
[8276]73 * @brief destructs the Directory.
74 */
[7610]75Directory::~Directory()
76{
[8276]77  this->close();
[7610]78}
79
[8276]80
81/**
82 * @brief openes the Directory
83 * @returns true on success, false on error. (does not exist, no rights -> test with functions of File)
84 *
85 * Fills the List of Files, and sets the Directory to open state
86 */
[7625]87bool Directory::open()
[7610]88{
[8276]89  if (this->_opened)
90    this->close();
91
92  // Openes the Directory for reading:
[7624]93#if not defined(__WIN32__)
[8276]94  DIR* handle;
95  handle = opendir(this->name().c_str());
[7610]96  if (!handle)
97  {
[8276]98    std::cerr << "could not open directory " << this->name() << " for reading" << std::endl;
99    return false;
[7610]100  }
[8523]101
102  dirent* entry;
103  while ((entry = readdir(handle)) != NULL)
104    this->_fileNames.push_back(entry->d_name);
105  closedir(handle);
106
[7624]107#else
[8276]108  HANDLE handle;
[7610]109
[8276]110  // First check the attributes trying to access a non-Directory with
111  // FindFirstFile takes ages
112  DWORD attrs = GetFileAttributes(this->name().c_str());
113  if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )
114  {
115    return false;
116  }
117  std::string Full(this->name());
118  // Circumvent a problem in FindFirstFile with c:\\* as parameter
119  if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )
120    Full += "\\";
121  WIN32_FIND_DATA entry;
122  handle = FindFirstFile( (Full+"*").c_str(), &entry);
123  if (handle == INVALID_HANDLE_VALUE)
124  {
125    std::cerr << "could not open directory " << this->name() << " for reading" << std::endl;
126    return false;
127  }
128  else
129  {
130    this->_fileNames.push_back(entry.cFileName);
131  }
[8523]132  int ok;
133  while ((ok = FindNextFile(handle, &entry)) != 0)
[8276]134    this->_fileNames.push_back(entry.cFileName);
135  FindClose(handle);
[8523]136#endif /* __WIN_32__ */
137
[8276]138  this->_opened = true;
[7624]139  return true;
[7610]140}
141
[8276]142/**
143 * @brief closes the directory
144 * @returns true.
145 *
146 * Clears the List of Files in the Directory.
147 */
148bool Directory::close()
[7610]149{
[8276]150  this->_opened = false;
151  this->_fileNames.clear();
152  return true;
[7610]153}
[7624]154
155
[8276]156/**
157 * @brief creates the directory
158 * @returns true on success, false on error
159 */
[7624]160bool Directory::create()
161{
162#if not defined (__WIN32__)
163  return (!mkdir(this->name().c_str(), 0777));
164#else
165  return (!CreateDirectory(this->name().c_str(), NULL));
166#endif
167}
Note: See TracBrowser for help on using the repository browser.