static uint32_t kdBinAddTexture(u::vector<kdBinTexture> &textures, const u::string &texturePath) { uint32_t index = 0; for (const auto &it : textures) { if (it.name == texturePath) return index; index++; } kdBinTexture texture; // Truncate the string if it doesn't fit const size_t length = u::min(sizeof texture.name - 1, texturePath.size()); memcpy(texture.name, (const void *)texturePath.c_str(), length); texture.name[length] = '\0'; textures.push_back(texture); return textures.size() - 1; }
// file system stuff bool exists(const u::string &path, pathType type) { if (type == kFile) return dir::isFile(path); // type == kDirectory #ifndef _WIN32 struct stat info; if (stat(path.c_str(), &info) != 0) return false; // Couldn't stat directory if (!(info.st_mode & S_IFDIR)) return false; // Not a directory #else // _stat does not like trailing path separators u::string strip = path; if (strip.end()[-1] == u::kPathSep) strip.pop_back(); struct _stat info; if (_stat(strip.c_str(), &info) != 0) return false; #ifndef S_IFDIR # define S_IFDIR _S_IFDIR #endif if (!(info.st_mode & _S_IFDIR)) return false; #endif return true; }
bool getline(u::file &fp, u::string &line) { line.clear(); for (;;) { char buf[256]; if (!fgets(buf, sizeof buf, fp.get())) { if (feof(fp.get())) return !line.empty(); abort(); } size_t n = strlen(buf); if (n && buf[n - 1] == '\n') --n; if (n && buf[n - 1] == '\r') --n; line.append(buf, n); if (n < sizeof buf - 1) return true; } return false; }
// file system stuff bool exists(const u::string &inputPath, pathType type) { u::string &&path = u::fixPath(inputPath); if (type == kFile) return dir::isFile(path); // type == kDirectory #if defined(_WIN32) const DWORD attribs = GetFileAttributesA(inputPath.c_str()); if (attribs == INVALID_FILE_ATTRIBUTES) return false; if (!(attribs & FILE_ATTRIBUTE_DIRECTORY)) return false; #else struct stat info; if (stat(path.c_str(), &info) != 0) return false; // Couldn't stat directory if (!(info.st_mode & S_IFDIR)) return false; // Not a directory #endif return true; }