amath  1.8.5
Simple command line calculator
filesystem_amiga.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 #include "amath.h"
31 #include "amathc.h"
32 #include "program.h"
33 #include "filesystem_amiga.h"
34 #include "lib/charbuf.h"
35 #include "loc/text.h"
36 
37 #if defined(AMIGA)
38 #include <clib/dos_protos.h>
39 
40 /**
41  * @brief
42  * http://en.wikibooks.org/wiki/Aros/Developer/Docs/Libraries/DOS#Examine_Files_or_Directories
43  */
44 CharBuffer* AmigaFilesystem::ListDirectory(const char* path)
45 {
46  static const long int pathsize = 256;
47  CharBuffer* pathbuf = new CharBuffer(pathsize);
48 
49  if (path == nullptr)
50  {
51  if (GetCurrentDirName(pathbuf->GetString(), pathsize))
52  {
53  pathbuf->Append(path);
54  }
55  else
56  {
57  delete pathbuf;
58  return nullptr;
59  }
60  }
61  else
62  {
63  pathbuf->EnsureSize(StrLen(path) + 1);
64  pathbuf->Append(path);
65  }
66 
67  BPTR lock;
68  BOOL success;
69 
70  lock = Lock((STRPTR)pathbuf->GetString(), ACCESS_READ);
71 
72  if (!lock)
73  {
74  CharBuffer* res = new CharBuffer();
75  const char* msg = MSGNODIR;
76  res->EnsureSize(StrLen(msg) + StrLen(pathbuf->GetString()) + StrLen(NEWLINE) + 1);
77  res->Empty();
78  res->Append(msg);
79  res->Append(pathbuf->GetString());
80  res->Append(NEWLINE);
81 
82  delete pathbuf;
83  return res;
84  }
85 
86  CharBuffer* lines = new CharBuffer();
87  lines->Empty();
88 
89  bool first = true;
90  FileInfoBlock* info = new FileInfoBlock();
91  success = Examine(lock, info);
92  success = ExNext(lock, info);
93  while (success)
94  {
95  if (first)
96  {
97  const char* header = TXTLISTDIRHEADER;
98  lines->EnsureSize(StrLen(header) + 1);
99  lines->Empty();
100  lines->Append(header);
101  first = false;
102  }
103 
104  const char* type;
105  if (info->fib_DirEntryType < 0)
106  {
107  type = TXTLISTDIRTFILE;
108  }
109  else
110  {
111  type = TXTLISTDIRTDIR;
112  }
113 
114  const unsigned short colsize = 12;
115  unsigned int a = StrLen(type) > colsize ? colsize : StrLen(type);
116  unsigned int b = colsize - a;
117 
118  lines->EnsureGrowth(colsize + StrLen((char*)info->fib_FileName) + StrLen(NEWLINE) + 1);
119  lines->Append(type);
120  lines->Append(' ', b);
121  lines->Append((char*)info->fib_FileName);
122  lines->Append(NEWLINE);
123 
124  success = ExNext(lock, info);
125  }
126 
127  UnLock(lock);
128  delete pathbuf;
129  return lines;
130 }
131 
132 CharBuffer* AmigaFilesystem::LoadTextFile(const char* name)
133 {
134  BPTR file = Open(const_cast<char*>(name), MODE_OLDFILE);
135  if (!file)
136  {
137  return nullptr;
138  }
139 
140  CharBuffer* text = new CharBuffer();
141  text->Empty();
142 
143  int blocks = 0;
144  bool eof = false;
145  LONG c;
146 
147  while (!eof)
148  {
149  blocks++;
150  text->EnsureSize(blocksize, blocks);
151  int count = 0;
152 
153  do
154  {
155  c = FGetC(file);
156  eof = (c <= 0);
157 
158  if (!eof)
159  {
160  text->Append(static_cast<char>(c));
161  count++;
162  }
163  }
164  while (!eof && count < blocksize);
165  }
166 
167  Close(file);
168  return text;
169 }
170 
171 bool AmigaFilesystem::SaveTextFile(const char* name, const char* text)
172 {
173  BPTR file = Open(const_cast<char*>(name), MODE_NEWFILE);
174 
175  if (!file)
176  {
177  return false;
178  }
179 
180  char* i = const_cast<char*>(text);
181  LONG r = 1;
182  while (r > 0 && *i)
183  {
184  r = FPutC(file, *i++);
185  }
186 
187  Close(file);
188  return true;
189 }
190 
191 #endif