Added error messages displaying using source file name and line number
[zilutils.git] / zilasm / string_table.cpp
1 /*
2  * string_table.cpp -- part of ZilUtils/ZilAsm
3  *
4  * Copyright (C) 2019, 2020 Jason Self <j@jxself.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as
8  * published by the Free Software Foundation, either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>
18  *
19  * SPDX-License-Identifier: AGPL-3.0-or-later
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "header.h"
25 #include "string_table.h"
26 #include <string.h>
27 extern "C"
28 {
29 #include <strings.h>
30 }
31
32 unsigned
33 String_table::add_string_to_string_table (const char *str, ZMemblock * zmb)
34 {
35   encrypt_string (str, zmb);
36   return 0;
37 }
38
39
40 ZMemblock *
41 String_table::encrypt_string (const char *str, ZMemblock * zmb)
42 {
43   if (!zmb)
44     zmb = zmem_init (strlen (str) * 3);
45
46   int currentCharSet = 0;
47   Byte currentByte = 0;
48   int index = 0;
49   Word w = 0;
50   int numberOfSymbolsInWord = 0;
51   while (index < (int) strlen (str))
52     {
53       char c = str[index];
54       if (c >= 'a' && c <= 'z')
55         {
56           switch (currentCharSet)
57             {
58             case 0:
59               break;
60             case 1:
61               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5);      // permanently changes to the zero charset
62               currentCharSet = 0;
63               break;
64             case 2:
65               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4);      // permanently changes to the zero charset
66               currentCharSet = 0;
67               break;
68             }
69           write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord,
70                                           c - 'a' + 6);
71         }
72       else if (c >= 'A' && c <= 'Z')
73         {
74           switch (currentCharSet)
75             {
76             case 0:
77               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4);      // temporarily changes to the zero charset
78               break;
79             case 1:
80               break;
81             case 2:
82               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4);      // permanently changes to the zero charset
83               currentCharSet = 0;
84               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4);      // temporarily changes to the first charset
85               break;
86             }
87           write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord,
88                                           c - 'A' + 6);
89
90         }
91       else if (c == ' ')
92         {
93                   //if (numberOfSymbolsInWord != 0)
94                   //{
95                          // zmem_putbyte(zmb, (w >> 8) & 255);
96                          // zmem_putbyte(zmb, w & 255);
97                          // numberOfSymbolsInWord = 0;
98                          // w = 0;
99                   //}
100                   write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 0);
101         }
102       else
103         {
104           char ar[] = "*0123456789.,!?_#'\"/\\-:()";
105           for (int i = 0; i < (int) strlen (ar); ++i)
106             {
107               if (ar[i] == c)
108                 {
109                   c = i + 7;
110                   break;
111                 }
112             }
113
114           switch (currentCharSet)
115             {
116             case 0:
117               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5);      // temporarily changes to the second charset
118               break;
119             case 1:
120               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5);      // permanently changes to the zero charset
121               currentCharSet = 0;
122               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5);      // temporarily changes to the second charset
123               break;
124             case 2:
125               break;
126             }
127           write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, c);
128         }
129
130       ++index;
131     }
132
133   if (numberOfSymbolsInWord != 0)
134     {
135       zmem_putbyte (zmb, (w >> 8) & 255);
136       zmem_putbyte (zmb, w & 255);
137       numberOfSymbolsInWord = 0;
138       w = 0;
139     }
140   
141   if ((zmb->contents[zmb->used_size - 1] & 31) == 0) // if the last symbol is a space or not filled
142           zmb->contents[zmb->used_size - 2] |= 0x80;     // then we mark this word as the last word
143   else
144   {
145           zmem_putbyte(zmb, 0x80);                       // otherwise we add one more empty word
146           zmem_putbyte(zmb, 0x0);                        // with 15-th bit set
147   }
148
149   return zmb;
150 }
151
152 void
153 String_table::write_one_word_to_string_table (ZMemblock * zmb, Word * w,
154                                               int *numberOfSymbolsInWord,
155                                               Byte symbol)
156 {
157   int shift = ((2 - *numberOfSymbolsInWord) * 5);
158   *w |= (symbol << shift);
159
160   (*numberOfSymbolsInWord)++;
161
162   if (*numberOfSymbolsInWord == 3)
163     {
164       zmem_putbyte (zmb, (*w >> 8) & 255);
165       zmem_putbyte (zmb, (*w) & 255);
166       *numberOfSymbolsInWord = 0;
167       (*w) = 0;
168     }
169 }