6const std::string TSUtils::voidString = std::string(
"");
8bool TSUtils::writeAllTablesToFile(
TSManager* tsManager,
const std::string& filePath)
12 file = strcmp(filePath.c_str(),
"")==0?file:fopen(filePath.c_str(),
"w");
15 tserror(
"File \"%s\" cannot be opened for write: %s",filePath.c_str(),strerror(errno));
19 tsdebug(
"Writing all tables");
21 bool gotSomeTables =
false;
22 for(
auto& table: tsManager->_getTablesStorage())
25 success &= writeTableToFile(table, filePath);
31 fprintf(file,
"(el almacen de tablas esta vacio)\n");
39bool TSUtils::writeTableToFile(
SymbolTable* table,
const std::string& filePath)
42 bool fileAlreadyOpened = file !=
nullptr;
45 tsdebug(
"File is already opened, appending the Table to the file",NULL);
49 file = fopen(filePath.c_str(),
"w");
52 tserror(
"File \"%s\" cannot be opened for write: %s",filePath.c_str(),strerror(errno));
56 tsdebug(
"Writing table with name=\"%s\" to file \"%s\"",table->
getTableName().c_str(),filePath.c_str());
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())
62 fprintf(file,
"---------------------------------------------------\n");
63 success &= writeEntryToFile(entry, filePath);
64 gotSomeEntries =
true;
66 fprintf(file,
"---------------------------------------------------\n");
69 fprintf(file,
"(la tabla esta vacia)\n");
72 if(!fileAlreadyOpened)
80bool TSUtils::writeEntryToFile(
Entry* entry,
const std::string& filePath)
84 tsdebug(
"Entry is marked as deleted, skipping it",NULL);
89 bool fileAlreadyOpened = file !=
nullptr;
92 tsdebug(
"File is already opened, appending the entry to the file",NULL);
96 file = fopen(filePath.c_str(),
"w");
99 tserror(
"File \"%s\" cannot be opened for write",filePath.c_str());
104 tsdebug(
"Writing entry with lexeme=\"%s\" to file \"%s\"",entry->
getLexeme().c_str(),filePath.c_str());
106 fprintf(file,
"* LEXEMA : '%s' \n",entry->
getLexeme().c_str());
107 fprintf(file,
"* ID : '%d' \n",entry->
getId());
111 fprintf(file,
"(no se han definido atributos para este lexema)\n");
115 fprintf(file,
"ATRIBUTOS :\n");
116 for(
auto& attribute : entry->_getAttributesStorage())
118 success &= writeAttributeToFile(attribute.first,attribute.second,filePath);
122 if(!fileAlreadyOpened)
130bool TSUtils::writeAttributeToFile(
const std::string& attributeName,
AttributeValue* value,
const std::string& filePath)
132 bool fileAlreadyOpened = file !=
nullptr;
133 if(fileAlreadyOpened)
135 tsdebug(
"File is already opened, appending the attribute to the file",NULL);
139 file = fopen(filePath.c_str(),
"w");
142 tserror(
"File \"%s\" cannot be opened for write",filePath.c_str());
147 std::pair<AttributeValue::AttributeType, AttributeValue::DataUnion> valuePair;
149 #if TS_PRINT_MESSAGES_LEVEL >= TS_LEVEL_DEBUG
153 if (valuePair.first == AttributeValue::STRING)
155 tsdebug(
"Writing attribute with attributeName=\"%s\" and value=\"%s\" to file \"%s\"",
156 attributeName.c_str(), valuePair.second.strPtr, filePath.c_str());
158 else if (valuePair.first == AttributeValue::INT)
160 tsdebug(
"Writing attribute with attributeName=\"%s\" and value=%lld to file \"%s\"", attributeName.c_str(),
161 valuePair.second.integer, filePath.c_str());
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",
171 fprintf(file,
" + %s : ",attributeName.c_str());
174 fprintf(file,TS_NULLSTRINGREPLACEMENT
"\n");
176 else if ((valuePair = value->
getValue()).first==AttributeValue::STRING)
178 fprintf(file,
"'%s'\n",valuePair.second.strPtr);
182 fprintf(file,
"%lld\n",valuePair.second.integer);
185 if(!fileAlreadyOpened)
187 #if TS_PRINT_MESSAGES_LEVEL >= TS_LEVEL_INFO
191 if (valuePair.first == AttributeValue::STRING)
193 tsdebug(
"Attribute with attributeName=\"%s\" and value=\"%s\" successfully written to file \"%s\"",
194 attributeName.c_str(), valuePair.second.strPtr, filePath.c_str());
196 else if (valuePair.first == AttributeValue::INT)
198 tsdebug(
"Attribute with attributeName=\"%s\" and value=%lld successfully written to file \"%s\"",
199 attributeName.c_str(), valuePair.second.integer, filePath.c_str());
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",
214bool TSUtils::writeAllTablesToFileF(
TSManager* tsManager, FILE* filePtr)
217 return writeAllTablesToFile(tsManager, voidString);
220bool TSUtils::writeTableToFileF(
SymbolTable* table, FILE* filePtr)
223 return writeTableToFile(table,voidString);
226bool TSUtils::writeEntryToFileF(
Entry* entry, FILE* filePtr)
229 return writeEntryToFile(entry,voidString);
232bool TSUtils::writeAttributeToFileF(
const std::string& attributeName,
AttributeValue* value, FILE* filePtr)
235 return writeAttributeToFile(attributeName,value,voidString);
std::pair< AttributeType, DataUnion > getValue()
bool isAValidAttributeValue()
std::string getLexeme() const
std::unordered_map< std::string, AttributeValue * > & _getAttributesStorage()
std::string getTableName() const