(file) Return to dir.c CVS log (file) (dir) Up to [OMI] / omi / samples / Providers / FileSystem

File: [OMI] / omi / samples / Providers / FileSystem / Attic / dir.c (download)
Revision: 1.1, Fri Jun 15 19:51:15 2012 UTC (12 years ago) by mike
Branch: MAIN
CVS Tags: OMI_1_0_2, OMI_1_0_1, OMI_1_0_0
OMI 1.0.1

#include "dir.h"
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>

#if defined(_MSC_VER)
# include <windows.h>
# include <sys/stat.h>
# include <io.h>
#else
# include <sys/types.h>
# include <sys/stat.h>
# include <dirent.h>
# include <unistd.h>
#endif

/*
**==============================================================================
**
** POSIC Implementation
**
**==============================================================================
*/

#if !defined(_MSC_VER)

struct _Dir
{
    char path[MAX_PATH_SIZE];
    DIR* dir;
    DirEnt ent;
};

Dir* Dir_Open(const char* path)
{
    Dir* self = (Dir*)calloc(1, sizeof(Dir));

    if (!self)
        return NULL;

    Strlcpy(self->path, path, sizeof(self->path));

    self->dir = opendir(path);
    if (!self->dir)
    {
        free(self);
        return NULL;
    }

    return self;
}

DirEnt* Dir_Read(Dir* self)
{
    struct dirent* p = readdir(self->dir);

    if (!p)
        return NULL;

    Strlcpy(self->ent.name, p->d_name, sizeof(self->ent.name));

    /* Determine whether it is a directory or data file */
    {
        char path[MAX_PATH_SIZE];
        Strlcpy(path, self->path, sizeof(path));
        Strlcat(path, "/", sizeof(path));
        Strlcat(path, p->d_name, sizeof(path));
        self->ent.isDir = Isdir(path);
    }

    return &self->ent;
}

int Dir_Close(Dir* self)
{
    if (!self)
        return -1;

    if (closedir(self->dir) != 0)
    {
        free(self);
        return -1;
    }

    free(self);
    return 0;
}

#endif

/*
**==============================================================================
**
** Windows Implementation
**
**==============================================================================
*/

#if defined(_MSC_VER)

struct _Dir
{
    intptr_t handle;
    struct _finddata_t fileinfo;
    DirEnt ent;
    int firstTime;
};

Dir* Dir_Open(const char* path)
{
    Dir* dir;
    char filespec[MAX_PATH_SIZE];
    
    /* Allocate and zero-fill struct */
    dir = (Dir*)calloc(1, sizeof(Dir));
    if (!dir)
        return NULL;

    /* Save path */

    Strlcpy(self->path, path, sizeof(self->path));

    /* Build files spec */
    {
        if (Strlcpy(filespec, path, sizeof(filespec)) >= MAX_PATH_SIZE)
            return NULL;

        if (Strlcat(filespec, "/*", sizeof(filespec)) >= MAX_PATH_SIZE)
            return NULL;
    }

    /* Find first file matching the file spec */
    dir->handle = _findfirst(filespec, &dir->fileinfo);
    if (dir->handle == -1)
    {
        free(dir);
        return NULL;
    }

    /* Note that readdir() has not been called yet */
    dir->firstTime = 1;

    return dir;
}

DirEnt* Dir_Read(Dir* dir)
{
    if (!dir->firstTime)
    {
        if (_findnext(dir->handle, &dir->fileinfo) != 0)
            return NULL;
    }

    Strlcpy(dir->ent.name, dir->fileinfo.name, MAX_PATH_SIZE);
    dir->firstTime = 0;

    /* Determine whether it is a directory or data file */
    {
        char path[MAX_PATH_SIZE];
        Strlcpy(path, self->path, sizeof(path));
        Strlcat(path, "/", sizeof(path));
        Strlcpy(path, p->d_name, sizeof(path));
        self->ent.isDir = Isdir(path);
    }

    return &dir->ent;
}

int Dir_Close(Dir* dir)
{
    _findclose(dir->handle);
    free(dir);
    return 0;
}

#endif

size_t Strlcpy(char* dest, const char* src, size_t size)
{
    const char* start = src;

    if (size)
    {
        char* end = dest + size - 1;

        while (*src && dest != end)
            *dest++ = *src++;

        *dest = '\0';
    }

    while (*src)
        src++;

    return src - start;
}

size_t Strlcat(char* dest, const char* src, size_t size)
{
    const char* start = src;

    if (size)
    {
        char* end = dest + size - 1;

        while (*dest && dest != end)
            dest++;

        while (*src && dest != end)
            *dest++ = *src++;

        *dest = '\0';
    }

    while (*src)
        src++;

    return src - start;
}

int Isdir(const char* path)
{
#if defined(CONFIG_OS_WINDOWS)
    {
        struct _stat st;

        if (_stat(path, &st) != 0)
            return 0;

        return _S_IFDIR & st.st_mode;
    }
#else
    {
        struct stat st;

        if (stat(path, &st) != 0)
            return 0;

        return S_ISDIR(st.st_mode);
    }
#endif
}

ViewCVS 0.9.2