amath  1.8.5
Simple command line calculator
language_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 #include "amath.h"
31 #include "amathc.h"
32 #include "program.h"
33 #include "language_stdc.h"
34 #include "filesystem_stdc.h"
35 #include "main/symbol.h"
36 #include "loc/help.h"
37 #include "loc/text.h"
38 #include "loc/ident.h"
39 #include "loc/kword.h"
40 
41 #if !defined(AMIGA)
42 
45 {
47 }
48 
50 {
51  if (textbase != nullptr)
52  {
53  delete textbase;
54  }
55 
56  if (helpbase != nullptr)
57  {
58  delete helpbase;
59  }
60 
61  if (identbase != nullptr)
62  {
63  delete identbase;
64  }
65 
66  if (kwordbase != nullptr)
67  {
68  delete kwordbase;
69  }
70 }
71 
73 {
74  if (textcatalog == nullptr)
75  return const_cast<char*>(def->text);
76 
77  return const_cast<char*>(textcatalog[def->id].text);
78 }
79 
81 {
82  if (helpcatalog == nullptr)
83  return const_cast<char*>(def->text);
84 
85  return const_cast<char*>(helpcatalog[def->id].text);
86 }
87 
89 {
90  if (identcatalog == nullptr)
91  return const_cast<char*>(def->text);
92 
93  return const_cast<char*>(identcatalog[def->id].text);
94 }
95 
97 {
98  return '.';
99 }
100 
101 bool StandardLanguage::CharIsAlNum(long unsigned int character)
102 {
103  return (character >= 'a' && character <= 'z') ||
104  (character >= 'A' && character <= 'Z') ||
105  (character >= '0' && character <= '9');
106 }
107 
108 bool StandardLanguage::CharIsAlpha(long unsigned int character)
109 {
110  return (character >= 'a' && character <= 'z') ||
111  (character >= 'A' && character <= 'Z');
112 }
113 
114 bool StandardLanguage::CharIsDigit(long unsigned int character)
115 {
116  return (character >= '0' && character <= '9');
117 }
118 
119 bool StandardLanguage::CharIsPunct(long unsigned int character)
120 {
121  return (character == '.');
122 }
123 
124 bool StandardLanguage::CharIsSpace(long unsigned int character)
125 {
126  return (character == 32);
127 }
128 
129 bool StandardLanguage::CharIsCntrl(long unsigned int character)
130 {
131  return (character < 32 || character > 125);
132 }
133 
134 bool StandardLanguage::StrIsEqualLoc(const char* s1, const char* s2)
135 {
136  return StrIsEqual(s1, s2);
137 }
138 
140 {
141  return (c >= 32 && c <= 126);
142 }
143 
145 {
146  /* Just use english for now
147 
148  const char* key;
149  const char* value;
150 
151  LoadCatalog(&textbase, "utext/dk-text.dict");
152  if (textbase != nullptr) {
153  textcatalog = new textdef[textcount];
154  for (unsigned int j = 0; j < textcount; j++) {
155  GetNextPair(&key, &value);
156  textcatalog[j].id = j;
157  textcatalog[j].text = value;
158  }
159  }
160 
161  LoadCatalog(&helpbase, "utext/dk-help.dict");
162  if (helpbase != nullptr) {
163  helpcatalog = new helptextdef[helpcount];
164  for (unsigned int j = 0; j < helpcount; j++) {
165  GetNextPair(&key, &value);
166  helpcatalog[j].id = j;
167  helpcatalog[j].symbol = helptexts[j].symbol;
168  helpcatalog[j].text = value;
169  }
170  }
171 
172  LoadCatalog(&identbase, "utext/dk-ident.dict");
173  if (identbase != nullptr) {
174  identcatalog = new identhelpdef[identcount];
175  for (unsigned int j = 0; j < identcount; j++) {
176  GetNextPair(&key, &value);
177  identcatalog[j].id = j;
178  identcatalog[j].ident = key;
179  identcatalog[j].text = value;
180  }
181  }
182 
183  LoadCatalog(&kwordbase, "utext/dk-keyword.dict");
184  if (kwordbase != nullptr) {
185  keywordsloc = new keyworddef[keywordcount];
186  for (unsigned int j = 0; j < keywordcount; j++) {
187  GetNextPair(&key, &value);
188  keywordsloc[j].id = j;
189  keywordsloc[j].name = value;
190  keywordsloc[j].symbol = keywords[j].symbol;
191  }
192  }
193  */
194 }
195 
196 void StandardLanguage::LoadCatalog(char** dest, const char* file)
197 {
198  FilesystemBase* filesystem = new StandardFilesystem();
199  CharBuffer* cbuf = filesystem->LoadTextFile(file);
200 
201  if (cbuf != nullptr)
202  {
204  ptr = *dest;
205  delete cbuf;
206  }
207  else
208  {
209  *dest = nullptr;
210  }
211 
212  delete filesystem;
213 }
214 
215 void StandardLanguage::GetNextPair(const char** key, const char** value)
216 {
218  *key = ptr;
221  *value = ptr;
223 }
224 
226 {
227  while ((*ptr) != '\0' && (*ptr) != '\n')
228  {
229  ptr++;
230  }
231 
232  if ((*ptr) == '\n')
233  {
234  *ptr++ = '\0';
235  }
236 }
237 
239 {
240  bool skipping;
241  do
242  {
243  if ((*ptr) == ';')
244  {
246  skipping = true;
247  }
248  else if ((*ptr) == '#' && *(ptr + sizeof(char)) == '#')
249  {
251  skipping = true;
252  }
253  else
254  {
255  skipping = false;
256  }
257  }
258  while (skipping);
259 }
260 
261 #endif
int id
Definition: amatht.h:47
const char * text
Definition: amatht.h:36
void LoadCatalog(char **dest, const char *file)
char * GetString() const
Definition: charbuf.cpp:306
bool StrIsEqualLoc(const char *s1, const char *s2)
bool CharIsAlpha(unsigned long character)
int id
Definition: help.h:50
bool CharIsPunct(unsigned long character)
bool Validate(char c)
Definition: amatht.h:33
bool StrIsEqual(const char *s1, const char *s2)
Compare two null terminated strings to each other.
Definition: strcmp.c:50
char * Translate(identhelpdef *def)
const char * text
Definition: help.h:52
const char * text
Definition: amatht.h:49
Language()
Definition: language.cpp:76
helptextdef * helpcatalog
Definition: language_stdc.h:75
virtual CharBuffer * LoadTextFile(const char *name)=0
textdef * textcatalog
Definition: language_stdc.h:74
char * Translate(textdef *def)
Abstract base class encapsulating file system calls.
Definition: filesystem.h:45
void GetNextPair(const char **key, const char **value)
bool CharIsAlNum(unsigned long character)
char * Translate(helptextdef *def)
bool CharIsCntrl(unsigned long character)
identhelpdef * identcatalog
Definition: language_stdc.h:76
int id
Definition: amatht.h:35
bool CharIsSpace(unsigned long character)
bool CharIsDigit(unsigned long character)
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
Encapsulate an character array which can be used as a string.
Definition: charbuf.h:44