gfx/skia/trunk/src/utils/SkOSFile.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/utils/SkOSFile.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,249 @@
     1.4 +/*
     1.5 + * Copyright 2011 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +#include "SkOSFile.h"
    1.11 +
    1.12 +SkString SkOSPath::SkPathJoin(const char *rootPath, const char *relativePath) {
    1.13 +    SkString result(rootPath);
    1.14 +    if (!result.endsWith(SkPATH_SEPARATOR)) {
    1.15 +        result.appendUnichar(SkPATH_SEPARATOR);
    1.16 +    }
    1.17 +    result.append(relativePath);
    1.18 +    return result;
    1.19 +}
    1.20 +
    1.21 +SkString SkOSPath::SkBasename(const char* fullPath) {
    1.22 +    if (!fullPath) {
    1.23 +        return SkString();
    1.24 +    }
    1.25 +    const char* filename = strrchr(fullPath, SkPATH_SEPARATOR);
    1.26 +    if (NULL == filename) {
    1.27 +        filename = fullPath;
    1.28 +    } else {
    1.29 +        ++filename;
    1.30 +    }
    1.31 +    return SkString(filename);
    1.32 +}
    1.33 +
    1.34 +#ifdef SK_BUILD_FOR_WIN
    1.35 +
    1.36 +static uint16_t* concat_to_16(const char src[], const char suffix[])
    1.37 +{
    1.38 +    size_t  i, len = strlen(src);
    1.39 +    size_t  len2 = 3 + (suffix ? strlen(suffix) : 0);
    1.40 +    uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t));
    1.41 +
    1.42 +    for (i = 0; i < len; i++)
    1.43 +        dst[i] = src[i];
    1.44 +
    1.45 +    if (i > 0 && dst[i-1] != '/')
    1.46 +        dst[i++] = '/';
    1.47 +    dst[i++] = '*';
    1.48 +
    1.49 +    if (suffix)
    1.50 +    {
    1.51 +        while (*suffix)
    1.52 +            dst[i++] = *suffix++;
    1.53 +    }
    1.54 +    dst[i] = 0;
    1.55 +    SkASSERT(i + 1 <= len + len2);
    1.56 +
    1.57 +    return dst;
    1.58 +}
    1.59 +
    1.60 +SkUTF16_Str::SkUTF16_Str(const char src[])
    1.61 +{
    1.62 +    size_t  len = strlen(src);
    1.63 +
    1.64 +    fStr = (uint16_t*)sk_malloc_throw((len + 1) * sizeof(uint16_t));
    1.65 +    size_t i;
    1.66 +    for (i = 0; i < len; i++)
    1.67 +        fStr[i] = src[i];
    1.68 +    fStr[i] = 0;
    1.69 +}
    1.70 +
    1.71 +////////////////////////////////////////////////////////////////////////////
    1.72 +
    1.73 +SkOSFile::Iter::Iter() : fHandle(0), fPath16(NULL)
    1.74 +{
    1.75 +}
    1.76 +
    1.77 +SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fHandle(0), fPath16(NULL)
    1.78 +{
    1.79 +    this->reset(path, suffix);
    1.80 +}
    1.81 +
    1.82 +SkOSFile::Iter::~Iter()
    1.83 +{
    1.84 +    sk_free(fPath16);
    1.85 +    if (fHandle)
    1.86 +        ::FindClose(fHandle);
    1.87 +}
    1.88 +
    1.89 +void SkOSFile::Iter::reset(const char path[], const char suffix[])
    1.90 +{
    1.91 +    if (fHandle)
    1.92 +    {
    1.93 +        ::FindClose(fHandle);
    1.94 +        fHandle = 0;
    1.95 +    }
    1.96 +    if (NULL == path)
    1.97 +        path = "";
    1.98 +
    1.99 +    sk_free(fPath16);
   1.100 +    fPath16 = concat_to_16(path, suffix);
   1.101 +}
   1.102 +
   1.103 +static bool is_magic_dir(const uint16_t dir[])
   1.104 +{
   1.105 +    // return true for "." and ".."
   1.106 +    return dir[0] == '.' && (dir[1] == 0 || dir[1] == '.' && dir[2] == 0);
   1.107 +}
   1.108 +
   1.109 +static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir)
   1.110 +{
   1.111 +    WIN32_FIND_DATAW    data;
   1.112 +
   1.113 +    if (NULL == dataPtr)
   1.114 +    {
   1.115 +        if (::FindNextFileW(handle, &data))
   1.116 +            dataPtr = &data;
   1.117 +        else
   1.118 +            return false;
   1.119 +    }
   1.120 +
   1.121 +    for (;;)
   1.122 +    {
   1.123 +        if (getDir)
   1.124 +        {
   1.125 +            if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !is_magic_dir((uint16_t*)dataPtr->cFileName))
   1.126 +                break;
   1.127 +        }
   1.128 +        else
   1.129 +        {
   1.130 +            if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
   1.131 +                break;
   1.132 +        }
   1.133 +        if (!::FindNextFileW(handle, dataPtr))
   1.134 +            return false;
   1.135 +    }
   1.136 +    // if we get here, we've found a file/dir
   1.137 +    if (name)
   1.138 +        name->setUTF16((uint16_t*)dataPtr->cFileName);
   1.139 +    return true;
   1.140 +}
   1.141 +
   1.142 +bool SkOSFile::Iter::next(SkString* name, bool getDir)
   1.143 +{
   1.144 +    WIN32_FIND_DATAW    data;
   1.145 +    WIN32_FIND_DATAW*   dataPtr = NULL;
   1.146 +
   1.147 +    if (fHandle == 0)   // our first time
   1.148 +    {
   1.149 +        if (fPath16 == NULL || *fPath16 == 0)    // check for no path
   1.150 +            return false;
   1.151 +
   1.152 +        fHandle = ::FindFirstFileW((LPCWSTR)fPath16, &data);
   1.153 +        if (fHandle != 0 && fHandle != (HANDLE)~0)
   1.154 +            dataPtr = &data;
   1.155 +    }
   1.156 +    return fHandle != (HANDLE)~0 && get_the_file(fHandle, name, dataPtr, getDir);
   1.157 +}
   1.158 +
   1.159 +#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
   1.160 +
   1.161 +#if 0
   1.162 +OSStatus FSPathMakeRef (
   1.163 +   const UInt8 * path,
   1.164 +   FSRef * ref,
   1.165 +   Boolean * isDirectory
   1.166 +);
   1.167 +#endif
   1.168 +
   1.169 +SkOSFile::Iter::Iter() : fDIR(0)
   1.170 +{
   1.171 +}
   1.172 +
   1.173 +SkOSFile::Iter::Iter(const char path[], const char suffix[]) : fDIR(0)
   1.174 +{
   1.175 +    this->reset(path, suffix);
   1.176 +}
   1.177 +
   1.178 +SkOSFile::Iter::~Iter()
   1.179 +{
   1.180 +    if (fDIR)
   1.181 +        ::closedir(fDIR);
   1.182 +}
   1.183 +
   1.184 +void SkOSFile::Iter::reset(const char path[], const char suffix[])
   1.185 +{
   1.186 +    if (fDIR)
   1.187 +    {
   1.188 +        ::closedir(fDIR);
   1.189 +        fDIR = 0;
   1.190 +    }
   1.191 +
   1.192 +    fPath.set(path);
   1.193 +    if (path)
   1.194 +    {
   1.195 +        fDIR = ::opendir(path);
   1.196 +        fSuffix.set(suffix);
   1.197 +    }
   1.198 +    else
   1.199 +        fSuffix.reset();
   1.200 +}
   1.201 +
   1.202 +// returns true if suffix is empty, or if str ends with suffix
   1.203 +static bool issuffixfor(const SkString& suffix, const char str[])
   1.204 +{
   1.205 +    size_t  suffixLen = suffix.size();
   1.206 +    size_t  strLen = strlen(str);
   1.207 +
   1.208 +    return  strLen >= suffixLen &&
   1.209 +            memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
   1.210 +}
   1.211 +
   1.212 +#include <sys/stat.h>
   1.213 +
   1.214 +bool SkOSFile::Iter::next(SkString* name, bool getDir)
   1.215 +{
   1.216 +    if (fDIR)
   1.217 +    {
   1.218 +        dirent* entry;
   1.219 +
   1.220 +        while ((entry = ::readdir(fDIR)) != NULL)
   1.221 +        {
   1.222 +            struct stat s;
   1.223 +            SkString    str(fPath);
   1.224 +
   1.225 +            if (!str.endsWith("/") && !str.endsWith("\\"))
   1.226 +                str.append("/");
   1.227 +            str.append(entry->d_name);
   1.228 +
   1.229 +            if (0 == stat(str.c_str(), &s))
   1.230 +            {
   1.231 +                if (getDir)
   1.232 +                {
   1.233 +                    if (s.st_mode & S_IFDIR)
   1.234 +                        break;
   1.235 +                }
   1.236 +                else
   1.237 +                {
   1.238 +                    if (!(s.st_mode & S_IFDIR) && issuffixfor(fSuffix, entry->d_name))
   1.239 +                        break;
   1.240 +                }
   1.241 +            }
   1.242 +        }
   1.243 +        if (entry)  // we broke out with a file
   1.244 +        {
   1.245 +            if (name)
   1.246 +                name->set(entry->d_name);
   1.247 +            return true;
   1.248 +        }
   1.249 +    }
   1.250 +    return false;
   1.251 +}
   1.252 +#endif // if one of:SK_BUILD_FOR_MAC, SK_BUILD_FOR_UNIX, SK_BUILD_FOR_ANDROID,SK_BUILD_FOR_IOS

mercurial