static void ekRegexDestroy(ekContext * E, ekRegex * regex) { if (regex->regex) { pcre_free(regex->regex); } ekFree(regex); }
void ekErrorDestroy(ekContext *E, ekError *error) { ekStringClear(E, &error->line); ekStringClear(E, &error->explanation); ekStringClear(E, &error->filename); ekFree(error); }
void ekSyntaxDestroy(struct ekContext * E, ekSyntax * syntax) { ekSyntaxElementClear(E, &syntax->v); ekSyntaxElementClear(E, &syntax->l); ekSyntaxElementClear(E, &syntax->r); ekFree(syntax); }
static void ekRegexDestroy(ekContext *E, ekRegex *regex) { ekValueRemoveRefNote(E, regex->subject, "ekRegex no longer needs subject"); if(regex->regex) { pcre_free(regex->regex); } ekFree(regex); }
static void fileFuncClear(struct ekContext *E, struct ekValue *p) { ekFile *file = (ekFile *)p->ptrVal; if(!file->permanent) { switchState(E, file, EFS_CLOSED); } ekValueRemoveRefNote(E, file->filename, "file doesnt need filename anymore"); ekFree(file); }
static ekU32 fileRead(struct ekContext *E, ekU32 argCount) { ekValue *thisValue; ekValue *ret = ekValueNullPtr; ekValue *bytesValue = NULL; int bytes = 0; ekFile *file; if(!ekContextGetArgs(E, argCount, "*F|i", &thisValue, &bytesValue)) { return ekContextArgsFailure(E, argCount, "file.read([optional int] bytes)"); } file = (ekFile *)thisValue->ptrVal; switchState(E, file, EFS_READ); if(file->handle) { if(bytesValue) { bytesValue = ekValueToInt(E, bytesValue); bytes = bytesValue->intVal; ekValueRemoveRefNote(E, bytesValue, "bytesValue temporary no longer needed"); } else { // we want "the rest" of the file (could be the whole thing if you just opened it) int end; int currentPos = ftell(file->handle); fseek(file->handle, 0, SEEK_END); end = ftell(file->handle); fseek(file->handle, currentPos, SEEK_SET); bytes = end - currentPos; } if(bytes > 0) { char *data = ekAlloc(bytes+1); int bytesRead = fread(data, 1, bytes, file->handle); if(bytesRead >= 0) { data[bytesRead] = 0; ret = ekValueDonateString(E, data); } else { ekFree(data); } } } ekValueRemoveRefNote(E, thisValue, "fileRead no longer needs thisValue"); ekContextReturn(E, ret); // will be the data if we succesfully read }
static void ekSyntaxElementClear(struct ekContext * E, ekSyntaxElement * e) { if (e->p) { ekSyntaxDestroy(E, e->p); } if (e->s) { ekFree(e->s); } if (e->a) { ekArrayDestroy(E, &e->a, (ekDestroyCB)ekSyntaxDestroy); } }
void ekValueTypeDestroy(struct ekContext *E, ekValueType *type) { if(type->funcDestroyUserData) { type->funcDestroyUserData(E, type); } if(type->intrinsics) { ekMapDestroy(E, type->intrinsics, NULL); } ekFree(type); }
void ekChunkDestroy(struct ekContext * E, ekChunk * chunk) { // chunk->block is either NULL or pointing inside chunk->blocks ekArrayDestroy(E, &chunk->blocks, (ekDestroyCB)ekBlockDestroy); ekArrayDestroy(E, &chunk->kStrings, (ekDestroyCB)ekDestroyCBFree); ek32ArrayDestroy(E, &chunk->kInts); ek32ArrayDestroy(E, &chunk->kFloats); ekStringClear(E, &chunk->sourcePath); ekStringClear(E, &chunk->searchPath); ekFree(chunk); }
void ekCompilerDestroy(ekCompiler *compiler) { struct ekContext *E = compiler->E; if(compiler->chunk) { ekChunkDestroy(E, compiler->chunk); } if(compiler->root) { ekSyntaxDestroy(E, compiler->root); } ekArrayDestroy(E, &compiler->errors, (ekDestroyCB)ekErrorDestroy); ekFree(compiler); }
static ekValue *readLineInternal(struct ekContext *E, ekFile *file, int chomp) { ekValue *ret = ekValueNullPtr; if(file->handle == stdin) { char readBuffer[1024]; if(fgets(readBuffer, 1024, stdin)) { ret = ekValueCreateString(E, readBuffer); } } else if(file->handle) { int readBufferSize = 100; int startPos = ftell(file->handle); char *readBuffer = NULL; char *sepLoc; int bytesRead; while(ret == ekValueNullPtr) { readBufferSize *= 2; readBuffer = ekRealloc(readBuffer, readBufferSize); fseek(file->handle, startPos, SEEK_SET); bytesRead = fread(readBuffer, 1, readBufferSize-1, file->handle); if(bytesRead > 0) { readBuffer[bytesRead] = 0; sepLoc = strchr(readBuffer, '\n'); if(sepLoc) { int advance = (int)(sepLoc - readBuffer + 1); // how much to advance the file pointer? if(chomp) { while((*sepLoc == '\n') || (*sepLoc == '\r')) { --sepLoc; } } *(sepLoc+1) = 0; ret = ekValueCreateString(E, readBuffer); fseek(file->handle, startPos + advance, SEEK_SET); // move the file ptr back to the beginning of the next line } else { if(bytesRead < (readBufferSize-1)) { // we must have reached the end of the file. Just return 'the rest'. ret = ekValueCreateString(E, readBuffer); break; } } } else { break; } } ekFree(readBuffer); } return ret; }