Beispiel #1
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);
}
Beispiel #2
0
static ekValue *absolutePath(struct ekContext *E, ekValue *path)
{
    char temppath[MAX_PATH_LENGTH];
    realpath(ekValueSafeStr(path), temppath);
    ekValueRemoveRefNote(E, path, "absolutePath doesn't need relative path anymore");
    return ekValueCreateString(E, temppath);
}
Beispiel #3
0
static ekU32 regexSubInternal(struct ekContext * E, ekU32 argCount, ekBool forceGlobal)
{
    ekValue * subject = NULL;
    ekValue * pattern = NULL;
    ekValue * replace = NULL;
    ekValue * options = NULL;
    ekValue * result = ekValueNullPtr;
    ekRegex * regex;
    int count = 0;

    if (!ekContextGetArgs(E, argCount, "sss|s", &subject, &pattern, &replace, &options)) {
        return ekContextArgsFailure(E, argCount, "re.sub([string] subject, [string] pattern, [string] replacement, [optional string] options)");
    }

    result = ekValueCreateString(E, "");
    regex = ekRegexCreate(E, subject, pattern, options);
    if (forceGlobal) {
        regex->global = ekTrue;
    }
    if (regex) {
        count = ekRegexSub(E, regex, replace, &result->stringVal);
        ekRegexDestroy(E, regex);
    }
    ekContextReturn(E, result);
}
Beispiel #4
0
static void addPermanentFile(struct ekContext *E, ekValue *module, const char *name, FILE *f, int permanentState)
{
    ekValue *fileValue = ekValueCreate(E, ekValueTypeId(E, 'F'));
    ekFile *file = (ekFile *)ekAlloc(sizeof(ekFile));
    file->filename = ekValueCreateString(E, name);
    file->state = permanentState;
    file->permanent = ekTrue;
    file->handle = f;
    fileValue->ptrVal = file;

    ekValueObjectSetMember(E, module, name, fileValue);
}
Beispiel #5
0
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;
}
Beispiel #6
0
static struct ekValue * intFuncToString(struct ekContext * E, struct ekValue * p)
{
    char temp[32];
    sprintf(temp, "%d", p->intVal);
    return ekValueCreateString(E, temp);
}
Beispiel #7
0
static struct ekValue * floatFuncToString(struct ekContext * E, struct ekValue * p)
{
    char temp[64];
    sprintf(temp, "%f", p->floatVal);
    return ekValueCreateString(E, temp);
}