amath  1.8.5
Simple command line calculator
filesystem_stdc.cpp
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 
31 #include "amath.h"
32 #include "amathc.h"
33 #include "program.h"
34 #include "filesystem.h"
35 #include "filesystem_stdc.h"
36 #include "lib/charbuf.h"
37 #include "loc/text.h"
38 
39 #if !defined(AMIGA)
40 #include <stdio.h>
41 #include <stdlib.h>
42 
43 #if defined(UNIX) || defined(HAIKU)
44 #include <dirent.h>
45 #endif
46 
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;
64  res->Empty();
65  res->Append(msg);
66  res->Append(pathbuf->GetString());
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 }
128 
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 }
171 
172 bool StandardFilesystem::SaveTextFile(const char* name, const char* text)
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 }
197 
198 #endif
void Append(const char c)
Definition: charbuf.cpp:245
#define NEWLINE
Definition: amath.h:222
CharBuffer * LoadTextFile(const char *name)
#define MSGNODIR
Definition: text.h:101
void Empty()
Definition: charbuf.cpp:218
CharBuffer * ListDirectory(const char *path)
char * GetString() const
Definition: charbuf.cpp:306
#define TXTLISTDIRHEADER
Definition: text.h:70
bool SaveTextFile(const char *name, const char *text)
void Append(const char *source)
Definition: charbuf.cpp:262
static const int blocksize
CharBuffer()
Initialize without allocating memory.
Definition: charbuf.cpp:38
#define TXTLISTDIRTFILE
Definition: text.h:71
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
#define TXTLISTDIRTDIR
Definition: text.h:72
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
void EnsureSize(unsigned int blocksize, unsigned int blocks)
Definition: charbuf.cpp:146
void Append(const char c, unsigned int count)
Definition: charbuf.cpp:250
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