Example #1
0
static ekU32 fileLines(struct ekContext *E, ekU32 argCount)
{
    ekValue *thisValue;
    ekValue *ret = ekValueNullPtr;
    ekValue *lineValue;
    ekValue *chompValue = NULL;
    ekBool chomp = ekFalse;
    ekFile *file;

    if(!ekContextGetArgs(E, argCount, "*F|?", &thisValue, &chompValue))
    {
        return ekContextArgsFailure(E, argCount, "file.lines([optional bool] chompNewline)");
    }

    file = (ekFile *)thisValue->ptrVal;
    if(chompValue)
    {
        chompValue = ekValueToBool(E, chompValue);
        chomp = chompValue->boolVal;
        ekValueRemoveRefNote(E, chompValue, "chompValue temporary no longer needed");
    }

    switchState(E, file, EFS_READ);

    ret = ekValueCreateArray(E);
    while((lineValue = readLineInternal(E, file, chomp)) != ekValueNullPtr)
    {
        ekValueArrayPush(E, ret, lineValue);
    }

    ekValueRemoveRefNote(E, thisValue, "lines no longer needs thisValue");
    ekContextReturn(E, ret); // will be the data if we succesfully read
}
Example #2
0
static ekU32 fileLs(struct ekContext *E, ekU32 argCount)
{
    ekValue *filenameValue = NULL;
    ekValue *arrayValue = NULL;

    if(!ekContextGetArgs(E, argCount, "s", &filenameValue))
    {
        return ekContextArgsFailure(E, argCount, "file.ls(dirpath)");
    }

    arrayValue = ekValueCreateArray(E);

#ifdef USE_DIRENT
    {
        DIR *dir = opendir(ekValueSafeStr(filenameValue));
        if(dir)
        {
            struct dirent *entry;
            while((entry = readdir(dir)) != NULL)
            {
                if(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
                {
                    ekValueArrayPush(E, arrayValue, ekValueCreateString(E, entry->d_name));
                }
            }
            closedir(dir);
        }
    }
#endif

    ekValueRemoveRefNote(E, filenameValue, "fileExists doesnt need filename anymore");
    ekContextReturn(E, arrayValue);
}
Example #3
0
static ekU32 regexMatch(struct ekContext * E, ekU32 argCount)
{
    ekValue * results = ekValueNullPtr;
    ekRegex * regex = regexCreateMatchFromArgs(E, argCount, "re.match([string] subject, [string] pattern, [optional string] options)");
    if (regex) {
        results = ekValueCreateArray(E);
        if (!ekRegexMatchNext(E, regex, results)) {
            results = ekValueNullPtr;
        }
        ekRegexDestroy(E, regex);
    }
    ekContextReturn(E, results);
}
Example #4
0
static ekU32 regexGMatchIterator(struct ekContext * E, ekU32 argCount)
{
    ekFrame * frame = ekArrayTop(E, &E->frames);
    ekValue * regexValue;
    ekRegex * regex;
    ekValue * results;

    ekAssert(frame->closure && frame->closure->closureVars);
    regexValue = ekMapGetS2P(E, frame->closure->closureVars, "regex");
    regex = (ekRegex *)regexValue->ptrVal;

    ekAssert(argCount == 0);
    ekContextPopValues(E, argCount);

    results = ekValueCreateArray(E);
    if (!ekRegexMatchNext(E, regex, results)) {
        results = ekValueNullPtr;
    }

    ekContextReturn(E, results);
}