source: golgotha/src/i4/file/mac/get_dir.cc

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 2.1 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "memory/malloc.hh"
10#include "dirent.hh"
11#include "file/get_dir.hh"
12
13class i4_dir_string : public i4_str
14//{{{
15{
16public:
17  i4_dir_string(StringPtr st) : i4_str(st[0])
18  {
19    len=st[0];
20    memcpy(ptr,st,len);
21  }
22
23  char *buffer() { return ptr; }
24  void set_len(w16 _len) { len = _len; }
25};
26//}}}
27
28
29extern char *i4_os_string(const i4_const_str &name);
30
31
32i4_bool i4_get_directory(const i4_const_str &path,
33                      i4_str **&files, w32 &tfiles,
34                      i4_str **&dirs, w32 &tdirs)
35//{{{
36{
37  struct dirent *de;
38  files=NULL;
39  dirs=NULL;
40  tfiles=0;
41  tdirs=0;
42  DIR *d=opendir(i4_os_string(path));
43  if (!d) return i4_F;
44
45  i4_dir_string **tlist=NULL;
46  sw32 t=0;
47  char curdir[256];
48  getwd(curdir);
49  if (chdir(i4_os_string(path))!=0)
50    return i4_F;
51
52  do
53  {
54    de=readdir(d);
55    if (de)
56    {
57      t++;
58      tlist=(i4_dir_string **)i4_realloc(tlist,sizeof(i4_dir_string *)*t,"tmp file list");
59      tlist[t-1]=new i4_dir_string(de->d_name);
60    }
61  } while (de);
62  closedir(d);
63
64  for (int i=0;i<t;i++)
65  {
66    d=opendir(((i4_dir_string *)tlist[i])->buffer() );   // see if the file is a directory
67    if (d)
68    {
69      tdirs++;
70      dirs=(i4_str **)i4_realloc(dirs,sizeof(i4_str *)*tdirs,"dir list");
71      dirs[tdirs-1]=tlist[i];
72      closedir(d);
73    } else
74    {
75      tfiles++;
76      files=(i4_str **)i4_realloc(files,sizeof(i4_str *)*tfiles,"dir list");
77      files[tfiles-1]=tlist[i];
78    }
79  }
80  if (t)
81    i4_free(tlist);
82  chdir(curdir);
83  return i4_T;
84}
85//}}}
86
87
88//{{{ Emacs Locals
89// Local Variables:
90// folded-file: t
91// End:
92//}}}
93
94
95
96
97
98
99
Note: See TracBrowser for help on using the repository browser.