1//
2// MacDirectory.mm
3// celestia
4//
5// Created by Da Woon Jung on Sun Apr 24 2004.
6// Copyright (c) 2005 Chris Laurel. All rights reserved.
7//
8// This program is free software; you can redistribute it and/or
9// modify it under the terms of the GNU General Public License
10// as published by the Free Software Foundation; either version 2
11// of the License, or (at your option) any later version.
12
13#include <dirent.h>
14#import "directory.h"
15#import "NSString_ObjCPlusPlus.h"
16
17
18class MacDirectory : public Directory
19{
20public:
21    MacDirectory(NSString *dirName);
22    virtual ~MacDirectory();
23
24    bool nextFile(std::string &);
25    bool enumFiles(EnumFilesHandler &handler, bool deep);
26
27    enum {
28        DirGood = 0,
29        DirBad = 1
30    };
31
32private:
33    char *_dirstring;
34    int _status;
35    DIR *_dir;
36};
37
38
39MacDirectory::MacDirectory(NSString *dirName) :
40_status(DirGood), _dir(NULL)
41{
42    NSString *fullPath = [dirName stringByStandardizingPath];
43    const char *fullPathStr = [fullPath UTF8String];
44
45    unsigned dirNameLen = CFStringGetMaximumSizeForEncoding([fullPath length], kCFStringEncodingUTF8);
46
47    if (dirNameLen > 0)
48    {
49        _dirstring = (char *)calloc(dirNameLen+sizeof(char), 1);
50        strncpy(_dirstring, fullPathStr, dirNameLen/sizeof(char));
51    }
52    else
53        _dirstring = NULL;
54}
55
56
57MacDirectory::~MacDirectory()
58{
59    if (_dir != NULL)
60    {
61        closedir(_dir);
62        _dir = NULL;
63    }
64
65    if (_dirstring)
66        free(_dirstring);
67}
68
69
70bool MacDirectory::nextFile(std::string &filename)
71{
72    if (_status != DirGood)
73        return false;
74
75    if (_dir == NULL)
76    {
77        _dir = opendir(_dirstring);
78        if (_dir == NULL)
79        {
80            _status = DirBad;
81            return false;
82        }
83    }
84
85    struct dirent *ent = readdir(_dir);
86    if (ent == NULL)
87    {
88        _status = DirBad;
89        return false;
90    }
91    else
92    {
93        filename = ent->d_name;
94        return true;
95    }
96}
97
98bool MacDirectory::enumFiles(EnumFilesHandler &handler, bool deep)
99{
100    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
101    bool result = Directory::enumFiles(handler, deep);
102    [pool release];
103    return result;
104}
105
106
107std::string WordExp(const std::string &filename)
108{
109    NSString *expandedPath = [[NSString stringWithStdString: filename] stringByStandardizingPath];
110    return [expandedPath stdString];
111}
112
113Directory* OpenDirectory(const std::string &dirname)
114{
115    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
116    Directory *result = new MacDirectory([NSString stringWithStdString: dirname]);
117    [pool release];
118    return result;
119}
120
121bool IsDirectory(const std::string &filename)
122{
123    BOOL isDir = NO;
124    return [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithStdString: filename] isDirectory:&isDir] && isDir;
125}
126