amath  1.8.5
Simple command line calculator
StandardFilesystem Class Reference

#include <filesystem_stdc.h>

Inheritance diagram for StandardFilesystem:
Collaboration diagram for StandardFilesystem:

Public Member Functions

CharBufferListDirectory (const char *path)
 
CharBufferLoadTextFile (const char *name)
 
bool SaveTextFile (const char *name, const char *text)
 
- Public Member Functions inherited from FilesystemBase
virtual ~FilesystemBase ()
 

Static Private Attributes

static const int blocksize = 512
 

Detailed Description

Definition at line 46 of file filesystem_stdc.h.

Member Function Documentation

◆ ListDirectory()

CharBuffer * StandardFilesystem::ListDirectory ( const char *  path)
virtual

Implements FilesystemBase.

Definition at line 47 of file filesystem_stdc.cpp.

References CharBuffer::Append(), CharBuffer::CharBuffer(), CharBuffer::Empty(), CharBuffer::EnsureGrowth(), CharBuffer::EnsureSize(), CharBuffer::GetString(), StrIsEqual(), and StrLen().

48 {
49 #if defined(UNIX) || defined(HAIKU)
50  CharBuffer *pathbuf = new CharBuffer();
51  pathbuf->Empty();
52  if (path == nullptr) {
53  pathbuf->Append(".");
54  } else {
55  pathbuf->EnsureSize(StrLen(path) + 1);
56  pathbuf->Append(path);
57  }
58 
59  DIR *dir;
60  if ((dir = opendir(pathbuf->GetString())) == nullptr) {
61  CharBuffer *res = new CharBuffer();
62  const char *msg = MSGNODIR;
63  res->EnsureSize(StrLen(msg) + StrLen(pathbuf->GetString()) + StrLen(NEWLINE) + 1);
64  res->Empty();
65  res->Append(msg);
66  res->Append(pathbuf->GetString());
67  res->Append(NEWLINE);
68 
69  delete pathbuf;
70  return res;
71  }
72 
73  CharBuffer *lines = new CharBuffer();
74  lines->Empty();
75 
76  bool first = true;
77  struct dirent *entry;
78  while ((entry = readdir(dir)) != nullptr) {
79  if (StrIsEqual(entry->d_name, ".") || StrIsEqual(entry->d_name, "..")) {
80  continue;
81  }
82 
83  if (first) {
84  const char *header = TXTLISTDIRHEADER;
85  lines->EnsureSize(StrLen(header) + 1);
86  lines->Empty();
87  lines->Append(header);
88  first = false;
89  }
90 
91  const char *type;
92 
93 #if defined(HAIKU)
94  type = TXTLISTDIRTUNKNOWN;
95 #else
96  switch (entry->d_type) {
97  case DT_REG:
98  type = TXTLISTDIRTFILE;
99  break;
100  case DT_DIR:
101  type = TXTLISTDIRTDIR;
102  break;
103  default:
104  type = TXTLISTDIRTUNKNOWN;
105  }
106 #endif
107 
108  const unsigned short colsize = 12;
109  unsigned int a = StrLen(type) > colsize ? colsize : StrLen(type);
110  unsigned int b = colsize - a;
111 
112  lines->EnsureGrowth(colsize + StrLen(entry->d_name) + StrLen(NEWLINE) + 1);
113  lines->Append(type);
114  lines->Append(' ', b);
115  lines->Append(entry->d_name);
116  lines->Append(NEWLINE);
117  }
118 
119  closedir(dir);
120  delete pathbuf;
121  return lines;
122 #else
123  CharBuffer* lines = new CharBuffer();
124  lines->Empty();
125  return lines;
126 #endif
127 }
#define NEWLINE
Definition: amath.h:222
#define MSGNODIR
Definition: text.h:101
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
#define TXTLISTDIRHEADER
Definition: text.h:70
void Append(const char *source)
Definition: charbuf.cpp:262
#define TXTLISTDIRTFILE
Definition: text.h:71
#define TXTLISTDIRTDIR
Definition: text.h:72
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
void EnsureGrowth(unsigned int size)
Definition: charbuf.cpp:169
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
#define TXTLISTDIRTUNKNOWN
Definition: text.h:73
Here is the call graph for this function:

◆ LoadTextFile()

CharBuffer * StandardFilesystem::LoadTextFile ( const char *  name)
virtual

Implements FilesystemBase.

Definition at line 129 of file filesystem_stdc.cpp.

References CharBuffer::Append(), blocksize, CharBuffer::CharBuffer(), CharBuffer::Empty(), and CharBuffer::EnsureSize().

130 {
131  FILE* file;
132 
133 #if !defined(WINDOWS)
134  file = fopen(name, "r");
135 #else
136  fopen_s(&file, name, "r");
137 #endif
138 
139  if (!file)
140  return nullptr;
141 
142  CharBuffer* text = new CharBuffer();
143  text->Empty();
144 
145  int blocks = 0;
146  bool eof = false;
147 
148  while (!eof)
149  {
150  blocks++;
151  text->EnsureSize(blocksize, blocks);
152  int count = 0;
153 
154  do
155  {
156  int c = fgetc(file);
157  eof = c == EOF;
158 
159  if (!eof)
160  {
161  text->Append(static_cast<char>(c));
162  count++;
163  }
164  }
165  while (!eof && count < blocksize);
166  }
167 
168  fclose(file);
169  return text;
170 }
void Empty()
Definition: charbuf.cpp:218
void Append(const char *source)
Definition: charbuf.cpp:262
static const int blocksize
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44
void EnsureSize(unsigned int size)
Ensure a memory block of specified size is allocated.
Definition: charbuf.cpp:114
Here is the call graph for this function:

◆ SaveTextFile()

bool StandardFilesystem::SaveTextFile ( const char *  name,
const char *  text 
)
virtual

Implements FilesystemBase.

Definition at line 172 of file filesystem_stdc.cpp.

173 {
174  FILE* file;
175 
176 #if !defined(WINDOWS)
177  file = fopen(name, "w");
178 #else
179  fopen_s(&file, name, "w");
180 #endif
181 
182  if (!file)
183  {
184  return false;
185  }
186 
187  char* i = const_cast<char*>(text);
188  int r = EOF + 11;
189  while (r != EOF && *i)
190  {
191  r = fputc(*i++, file);
192  }
193 
194  fclose(file);
195  return true;
196 }

Member Data Documentation

◆ blocksize

const int StandardFilesystem::blocksize = 512
staticprivate

Definition at line 54 of file filesystem_stdc.h.

Referenced by LoadTextFile().


The documentation for this class was generated from the following files: