TS_CPP
An efficient & easy to use implementation of a Symbol table used in most compilers
Loading...
Searching...
No Matches
TSUtils.cpp
1#include "TSUtils.h"
2
3using namespace TS_CPP;
4
5FILE* TSUtils::file;
6const std::string TSUtils::voidString = std::string("");
7
8bool TSUtils::writeAllTablesToFile(TSManager* tsManager, const std::string& filePath)
9{
10 bool success = true;
11
12 file = strcmp(filePath.c_str(),"")==0?file:fopen(filePath.c_str(),"w");
13 if(file == nullptr)
14 {
15 tserror("File \"%s\" cannot be opened for write: %s",filePath.c_str(),strerror(errno));
16 return false;
17 }
18
19 tsdebug("Writing all tables");
20
21 bool gotSomeTables = false;
22 for(auto& table: tsManager->_getTablesStorage())
23 {
24 fprintf(file,"\n\n");
25 success &= writeTableToFile(table, filePath);
26 gotSomeTables = true;
27 }
28 fprintf(file,"\n\n");
29 if(!gotSomeTables)
30 {
31 fprintf(file,"(el almacen de tablas esta vacio)\n");
32 }
33
34 fclose(file);
35 file = nullptr;
36 return success;
37}
38
39bool TSUtils::writeTableToFile(SymbolTable* table, const std::string& filePath)
40{
41 bool success = true;
42 bool fileAlreadyOpened = file != nullptr;
43 if(fileAlreadyOpened)
44 {
45 tsdebug("File is already opened, appending the Table to the file",NULL);
46 }
47 else
48 {
49 file = fopen(filePath.c_str(),"w");
50 if(file == nullptr)
51 {
52 tserror("File \"%s\" cannot be opened for write: %s",filePath.c_str(),strerror(errno));
53 return false;
54 }
55 }
56 tsdebug("Writing table with name=\"%s\" to file \"%s\"",table->getTableName().c_str(),filePath.c_str());
57
58 fprintf(file,"CONTENIDO ACTUAL DE LA TABLA %s # %d :\n",table->getTableName().c_str(),table->getId());
59 bool gotSomeEntries = false;
60 for(auto& entry: table->_getEntriesStorage())
61 {
62 fprintf(file,"---------------------------------------------------\n");
63 success &= writeEntryToFile(entry, filePath);
64 gotSomeEntries = true;
65 }
66 fprintf(file,"---------------------------------------------------\n");
67 if(!gotSomeEntries)
68 {
69 fprintf(file,"(la tabla esta vacia)\n");
70 }
71
72 if(!fileAlreadyOpened)
73 {
74 fclose(file);
75 file = nullptr;
76 }
77 return success;
78}
79
80bool TSUtils::writeEntryToFile(Entry* entry, const std::string& filePath)
81{
82 if(entry->_isDeleted())
83 {
84 tsdebug("Entry is marked as deleted, skipping it",NULL);
85 return true;
86 }
87
88 bool success = true;
89 bool fileAlreadyOpened = file != nullptr; //file.is_open();
90 if(fileAlreadyOpened)
91 {
92 tsdebug("File is already opened, appending the entry to the file",NULL);
93 }
94 else
95 {
96 file = fopen(filePath.c_str(),"w");
97 if(file == nullptr)
98 {
99 tserror("File \"%s\" cannot be opened for write",filePath.c_str());
100 return false;
101 }
102 }
103
104 tsdebug("Writing entry with lexeme=\"%s\" to file \"%s\"",entry->getLexeme().c_str(),filePath.c_str());
105
106 fprintf(file,"* LEXEMA : '%s' \n",entry->getLexeme().c_str());
107 fprintf(file,"* ID : '%d' \n",entry->getId());
108
109 if (entry->_getAttributesStorage().empty())
110 {
111 fprintf(file,"(no se han definido atributos para este lexema)\n");
112 }
113 else
114 {
115 fprintf(file,"ATRIBUTOS :\n");
116 for(auto& attribute : entry->_getAttributesStorage())
117 {
118 success &= writeAttributeToFile(attribute.first,attribute.second,filePath);
119 }
120 }
121
122 if(!fileAlreadyOpened)
123 {
124 fclose(file);
125 file = nullptr;
126 }
127 return success;
128}
129
130bool TSUtils::writeAttributeToFile(const std::string& attributeName, AttributeValue* value, const std::string& filePath)
131{
132 bool fileAlreadyOpened = file != nullptr; //file.is_open();
133 if(fileAlreadyOpened)
134 {
135 tsdebug("File is already opened, appending the attribute to the file",NULL);
136 }
137 else
138 {
139 file = fopen(filePath.c_str(),"w");
140 if(file == nullptr)
141 {
142 tserror("File \"%s\" cannot be opened for write",filePath.c_str());
143 return false;
144 }
145 }
146
147 std::pair<AttributeValue::AttributeType, AttributeValue::DataUnion> valuePair;
148
149 #if TS_PRINT_MESSAGES_LEVEL >= TS_LEVEL_DEBUG
150 if (value->isAValidAttributeValue())
151 {
152 valuePair = value->getValue();
153 if (valuePair.first == AttributeValue::STRING)
154 {
155 tsdebug("Writing attribute with attributeName=\"%s\" and value=\"%s\" to file \"%s\"",
156 attributeName.c_str(), valuePair.second.strPtr, filePath.c_str());
157 }
158 else if (valuePair.first == AttributeValue::INT)
159 {
160 tsdebug("Writing attribute with attributeName=\"%s\" and value=%lld to file \"%s\"", attributeName.c_str(),
161 valuePair.second.integer, filePath.c_str());
162 }
163 else
164 {
165 tserror("This line should not be called never. if it does, a bug has been detected with the type of the attribute being printed",
166 NULL);
167 }
168 }
169 #endif
170
171 fprintf(file," + %s : ",attributeName.c_str());
172 if(!value->isAValidAttributeValue())
173 {
174 fprintf(file,TS_NULLSTRINGREPLACEMENT "\n");
175 }
176 else if ((valuePair = value->getValue()).first==AttributeValue::STRING)
177 {
178 fprintf(file,"'%s'\n",valuePair.second.strPtr);
179 }
180 else
181 {
182 fprintf(file,"%lld\n",valuePair.second.integer);
183 }
184
185 if(!fileAlreadyOpened)
186 {
187 #if TS_PRINT_MESSAGES_LEVEL >= TS_LEVEL_INFO
188 if (value->isAValidAttributeValue())
189 {
190 valuePair = value->getValue();
191 if (valuePair.first == AttributeValue::STRING)
192 {
193 tsdebug("Attribute with attributeName=\"%s\" and value=\"%s\" successfully written to file \"%s\"",
194 attributeName.c_str(), valuePair.second.strPtr, filePath.c_str());
195 }
196 else if (valuePair.first == AttributeValue::INT)
197 {
198 tsdebug("Attribute with attributeName=\"%s\" and value=%lld successfully written to file \"%s\"",
199 attributeName.c_str(), valuePair.second.integer, filePath.c_str());
200 }
201 else
202 {
203 tserror("This line should not be called never. if it does, a bug has been detected with the type of the attribute being printed",
204 NULL);
205 }
206 }
207 #endif
208 fclose(file);
209 file = nullptr;
210 }
211 return true;
212}
213
214bool TSUtils::writeAllTablesToFileF(TSManager* tsManager, FILE* filePtr)
215{
216 file = filePtr;
217 return writeAllTablesToFile(tsManager, voidString);
218}
219
220bool TSUtils::writeTableToFileF(SymbolTable* table, FILE* filePtr)
221{
222 file = filePtr;
223 return writeTableToFile(table,voidString);
224}
225
226bool TSUtils::writeEntryToFileF(Entry* entry, FILE* filePtr)
227{
228 file = filePtr;
229 return writeEntryToFile(entry,voidString);
230}
231
232bool TSUtils::writeAttributeToFileF(const std::string& attributeName, AttributeValue* value, FILE* filePtr)
233{
234 file = filePtr;
235 return writeAttributeToFile(attributeName,value,voidString);
236}
std::pair< AttributeType, DataUnion > getValue()
Definition Entry.h:19
int getId() const
Definition Entry.cpp:21
bool _isDeleted() const
Definition Entry.cpp:117
std::string getLexeme() const
Definition Entry.cpp:16
std::unordered_map< std::string, AttributeValue * > & _getAttributesStorage()
Definition Entry.cpp:85
std::string getTableName() const