int cstr_has_item(const char *in_string, const char *in_item) { char **items; int count, result = 0; _itemize((char*)in_string, &items, &count); for (int i = 0; i < count; i++) { if (_streq(_inplace_trim(items[i]), (char*)in_item)) { result = 1; break; } } _items_cleanup(items, count); return result; }
const char* cstr_item_remove(const char *in_string, const char *in_item) { char **items; int count; _itemize((char*)in_string, &items, &count); for (int i = 0; i < count; i++) { if (_streq(_inplace_trim(items[i]), (char*)in_item)) { if (items[i]) free(items[i]); items[i] = NULL; break; } } static char *result = NULL; if (result) free(result); result = _items_join(items, count); _items_cleanup(items, count); return result; }
const char* cstr_item_add(const char *in_string, const char *in_item) { char **items; int count, exists = 0; _itemize((char*)in_string, &items, &count); for (int i = 0; i < count; i++) { if (_streq(_inplace_trim(items[i]), (char*)in_item)) { exists = 1; break; } } if (!exists) { _extend(&items, &count); items[count-1] = _cstr_clone((char*)in_item); } static char *result = NULL; if (result) free(result); result = _items_join(items, count); _items_cleanup(items, count); return result; }
int main(int argc, char** argv){ const char* fileName = "IFgame/God_IF.html"; FILE* ifFile = fopen(fileName, "rb"); if(ifFile == NULL){ printf("Could not open file: '%s'\n", fileName); return 0; } fseek(ifFile, 0, SEEK_END); int fileSize = ftell(ifFile); fseek(ifFile, 0, SEEK_SET); char* fileBuffer = (char*)malloc(fileSize+1); fread(fileBuffer, 1, fileSize, ifFile); fileBuffer[fileSize] = '\0'; fclose(ifFile); DialogTree tree = {}; ParserStream stream = CreateParserStream(fileBuffer, fileSize); AdvanceToString(&stream, "<tw-passagedata"); while(LengthLeft(&stream) > 0){ AdvancePast(&stream, "name=\""); Token nodeName = AdvanceToString(&stream, "\""); AdvancePast(&stream, ">"); Token nodeData = AdvanceToString(&stream, "</tw-passagedata>"); DialogNode node = {}; node.name = AllocateStringFromToken(nodeName); ParserStream substream = CreateParserStream(nodeData.start, nodeData.length); Token nodePara = AdvanceToString(&substream, "[["); node.passage = AllocateStringFromToken(nodePara); while(LengthLeft(&substream) > 0){ AdvancePast(&substream, "[["); Token option = AdvanceToString(&substream, "]]"); if(option.length > 0){ node.options[node.optionCount].option = AllocateStringFromToken(option); node.optionCount++; } } AddDialogNode(&tree, node); } free(fileBuffer); for(int i = 0; i < tree.nodeCount; i++){ for(int j = 0; j < tree.nodes[i].optionCount; j++){ tree.nodes[i].options[j].goToIndex = -1; for(int k = 0; k < tree.nodeCount; k++){ if(_streq(tree.nodes[i].options[j].option, tree.nodes[k].name)){ tree.nodes[i].options[j].goToIndex = k; break; } } } } for(int i = 0; i < tree.nodeCount; i++){ printf("Name: '%s'\n", tree.nodes[i].name); printf("Passage: '%s'\n", tree.nodes[i].passage); for(int j = 0; j < tree.nodes[i].optionCount; j++){ printf("\t[[%s]] -> %d\n", tree.nodes[i].options[j].option, tree.nodes[i].options[j].goToIndex); } printf("--------------\n"); } return 0; }