CSS-Sass

 view release on metacpan or  search on metacpan

libsass/file.cpp  view on Meta::CPAN

      // next test plain name with exts
      for(auto ext : exts) {
        path = join_paths(base, name + ext);
        if (file_exists(path)) return path;
      }
      // nothing found
      return string("");
    }

    // helper function to resolve a filename
    string find_file(const string& file, const vector<string> paths)
    {
      // search in every include path for a match
      for (size_t i = 0, S = paths.size(); i < S; ++i)
      {
        string path(join_paths(paths[i], file));
        string resolved(resolve_file(path));
        if (resolved != "") return resolved;
      }
      // nothing found
      return string("");
    }

    // inc paths can be directly passed from C code
    string find_file(const string& file, const char* paths[])
    {
      if (paths == 0) return string("");
      vector<string> includes(0);
      // includes.push_back(".");
      const char** it = paths;
      while (it && *it) {
        includes.push_back(*it);
        ++it;
      }
      return find_file(file, includes);
    }

    // try to load the given filename
    // returned memory must be freed
    // will auto convert .sass files
    char* read_file(const string& path)
    {
      #ifdef _WIN32
        BYTE* pBuffer;
        DWORD dwBytes;
        // windows unicode filepaths are encoded in utf16
        wstring wpath = UTF_8::convert_to_utf16(path);
        HANDLE hFile = CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
        if (hFile == INVALID_HANDLE_VALUE) return 0;
        DWORD dwFileLength = GetFileSize(hFile, NULL);
        if (dwFileLength == INVALID_FILE_SIZE) return 0;
        // allocate an extra byte for the null char
        pBuffer = (BYTE*)malloc((dwFileLength+1)*sizeof(BYTE));
        ReadFile(hFile, pBuffer, dwFileLength, &dwBytes, NULL);
        pBuffer[dwFileLength] = '\0';
        CloseHandle(hFile);
        // just convert from unsigned char*
        char* contents = (char*) pBuffer;
      #else
        struct stat st;
        if (stat(path.c_str(), &st) == -1 || S_ISDIR(st.st_mode)) return 0;
        ifstream file(path.c_str(), ios::in | ios::binary | ios::ate);
        char* contents = 0;
        if (file.is_open()) {
          size_t size = file.tellg();
          // allocate an extra byte for the null char
          contents = (char*) malloc((size+1)*sizeof(char));
          file.seekg(0, ios::beg);
          file.read(contents, size);
          contents[size] = '\0';
          file.close();
        }
      #endif
      string extension;
      if (path.length() > 5) {
        extension = path.substr(path.length() - 5, 5);
      }
      for(size_t i=0; i<extension.size();++i)
        extension[i] = tolower(extension[i]);
      if (extension == ".sass" && contents != 0) {
        char * converted = sass2scss(contents, SASS2SCSS_PRETTIFY_1 | SASS2SCSS_KEEP_COMMENT);
        free(contents); // free the indented contents
        return converted; // should be freed by caller
      } else {
        return contents;
      }
    }

  }
}



( run in 0.581 second using v1.01-cache-2.11-cpan-39bf76dae61 )