Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/qt_gui/src/lib/util/directory.cc @ 7620

Last change on this file since 7620 was 7610, checked in by bensch, 18 years ago

orxonox/qt_gui: directory splitted into declaration and definition

File size: 3.2 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"
38Directory::Directory(const std::string& directoryName)
39    : willfail(false)
40{
41  this->handle = 0;
42  this->willfail = true;
43  this->open(directoryName);
44}
45
46Directory::~Directory()
47{
48  this->close();
49}
50
51bool Directory::open(const std::string& directoryName)
52{
53#if defined(OSLINK_OSDIR_POSIX)
54  if (this->handle != NULL)
55    this->close();
56  this->handle = opendir(directoryName.c_str());
57  if (!handle)
58    willfail = true;
59  else
60  {
61    willfail = false;
62    dirent* entry = readdir(handle);
63    if (entry)
64      current = entry->d_name;
65    else
66      willfail = true;
67  }
68#elif defined(OSLINK_OSDIR_WINDOWS)
69    if (handle != INVALID_HANDLE_VALUE)
70      this->close();
71
72    // First check the attributes trying to access a non-Directory with
73    // FindFirstFile takes ages
74    DWORD attrs = GetFileAttributes(directoryName.c_str());
75    if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )
76    {
77      willfail = true;
78      return false;
79    }
80    std::string Full(directoryName);
81    // Circumvent a problem in FindFirstFile with c:\\* as parameter
82    if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )
83      Full += "\\";
84    WIN32_FIND_DATA entry;
85    handle = FindFirstFile( (Full+"*").c_str(), &entry);
86    if (handle == INVALID_HANDLE_VALUE)
87    {
88      willfail = true;
89      return false;
90    }
91    else
92    {
93      willfail = false;
94      current = entry.cFileName;
95      return true;
96    }
97#endif
98
99}
100
101void Directory::close()
102{
103#if defined(OSLINK_OSDIR_POSIX)
104  if (this->handle != NULL)
105    closedir(handle);
106  this->handle = NULL;
107#elif defined(OSLINK_OSDIR_WINDOWS)
108  if (handle != INVALID_HANDLE_VALUE)
109    FindClose(handle);
110    handle = 0;
111#endif
112  this->willfail = true;
113  this->current = "";
114}
115
116
117
118std::string Directory::next()
119{
120#if defined(OSLINK_OSDIR_POSIX)
121  std::string prev(current);
122  dirent* entry = readdir(handle);
123  if (entry)
124    current = entry->d_name;
125  else
126    willfail = true;
127  return prev;
128
129#elif defined(OSLINK_OSDIR_WINDOWS)
130  std::string prev = current;
131  WIN32_FIND_DATA entry;
132  int ok = FindNextFile(handle, &entry);
133  if (!ok)
134    willfail = true;
135  else
136    current = entry.cFileName;
137  return current;
138#endif
139}
Note: See TracBrowser for help on using the repository browser.