Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8290 was 8276, checked in by bensch, 19 years ago

merged new GUI

File size: 3.7 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/**
61 * @brief destructs the Directory.
62 */
[7610]63Directory::~Directory()
64{
[8276]65  this->close();
[7610]66}
67
[8276]68
69/**
70 * @brief openes the Directory
71 * @returns true on success, false on error. (does not exist, no rights -> test with functions of File)
72 *
73 * Fills the List of Files, and sets the Directory to open state
74 */
[7625]75bool Directory::open()
[7610]76{
[8276]77  if (this->_opened)
78    this->close();
79
80  // Openes the Directory for reading:
[7624]81#if not defined(__WIN32__)
[8276]82  DIR* handle;
83  handle = opendir(this->name().c_str());
[7610]84  if (!handle)
85  {
[8276]86    std::cerr << "could not open directory " << this->name() << " for reading" << std::endl;
87    return false;
[7610]88  }
[7624]89#else
[8276]90  HANDLE handle;
[7610]91
[8276]92  // First check the attributes trying to access a non-Directory with
93  // FindFirstFile takes ages
94  DWORD attrs = GetFileAttributes(this->name().c_str());
95  if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )
96  {
97    return false;
98  }
99  std::string Full(this->name());
100  // Circumvent a problem in FindFirstFile with c:\\* as parameter
101  if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )
102    Full += "\\";
103  WIN32_FIND_DATA entry;
104  handle = FindFirstFile( (Full+"*").c_str(), &entry);
105  if (handle == INVALID_HANDLE_VALUE)
106  {
107    std::cerr << "could not open directory " << this->name() << " for reading" << std::endl;
108    return false;
109  }
110  else
111  {
112    this->_fileNames.push_back(entry.cFileName);
113  }
[7610]114#endif
115
[8276]116  // BUILDING the list of contained Files. (only the names)
[7624]117#if not defined(__WIN32__)
[8276]118  dirent* entry;
119  while ((entry = readdir(handle)) != NULL)
120    this->_fileNames.push_back(entry->d_name);
121  closedir(handle);
[7624]122#else
[8276]123  WIN32_FIND_DATA entry;
124  while ((int ok = FindNextFile(handle, &entry)) != 0)
125    this->_fileNames.push_back(entry.cFileName);
126  FindClose(handle);
[7610]127#endif
[8276]128  this->_opened = true;
[7624]129  return true;
[7610]130}
131
[8276]132/**
133 * @brief closes the directory
134 * @returns true.
135 *
136 * Clears the List of Files in the Directory.
137 */
138bool Directory::close()
[7610]139{
[8276]140  this->_opened = false;
141  this->_fileNames.clear();
142  return true;
[7610]143}
[7624]144
145
[8276]146/**
147 * @brief creates the directory
148 * @returns true on success, false on error
149 */
[7624]150bool Directory::create()
151{
152#if not defined (__WIN32__)
153  return (!mkdir(this->name().c_str(), 0777));
154#else
155  return (!CreateDirectory(this->name().c_str(), NULL));
156#endif
157}
Note: See TracBrowser for help on using the repository browser.