bool SetupDefaultDirs(const char *exefilepath, const char *auxfilepath, bool forcecommandline) { datadir = StripFilePart(exefilepath); auxdir = auxfilepath ? StripFilePart(SanitizePath(auxfilepath).c_str()) : datadir; writedir = auxdir; #ifdef __APPLE__ if (!forcecommandline) { // default data dir is the Resources folder inside the .app bundle CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle); char path[PATH_MAX]; auto res = CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX); CFRelease(resourcesURL); if (!res) return false; datadir = string(path) + "/"; #ifdef __IOS__ writedir = StripFilePart(path) + "Documents/"; // there's probably a better way to do this in CF #else writedir = datadir; // FIXME: this should probably be ~/Library/Application Support/AppName, but for now this works for non-app store apps #endif } #elif defined(ANDROID) datadir = "/Documents/Lobster/"; // FIXME: temp solution writedir = datadir; #endif return true; }
uchar *LoadFile(const char *relfilename, size_t *lenret) { auto srfn = SanitizePath(relfilename); auto f = LoadFilePlatform((datadir + srfn).c_str(), lenret); if (f) return f; f = LoadFilePlatform((auxdir + srfn).c_str(), lenret); if (f) return f; return LoadFilePlatform((writedir + srfn).c_str(), lenret); }
STARTDECL(scan_folder) (Value &fld, Value &divisor) { string folder = SanitizePath(fld.sval->str()); fld.DEC(); if (divisor.ival <= 0) divisor.ival = 1; #ifdef WIN32 WIN32_FIND_DATA fdata; HANDLE fh = FindFirstFile((folder + "\\*.*").c_str(), &fdata); if (fh == INVALID_HANDLE_VALUE) return Value(0, V_NIL); auto list = g_vm->NewVector(0, V_VECTOR); do { if (strcmp(fdata.cFileName, ".") && strcmp(fdata.cFileName, "..")) { ULONGLONG size = (static_cast<ULONGLONG>(fdata.nFileSizeHigh) << (sizeof(uint) * 8)) | fdata.nFileSizeLow; AddDirItem(list, fdata.cFileName, fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? -1 : size, divisor.ival); } } while(FindNextFile(fh, &fdata)); FindClose(fh); return Value(list); #elif !defined(ANDROID) glob_t gl; string mask = folder + "/*"; if (glob(mask.c_str(), GLOB_MARK | GLOB_TILDE, NULL, &gl)) return Value(0, V_NIL); auto list = g_vm->NewVector(0, V_VECTOR); for (size_t fi = 0; fi < gl.gl_pathc; fi++) { string xFileName = gl.gl_pathv[fi]; bool isDir = xFileName[xFileName.length()-1] == '/'; if (isDir) xFileName = xFileName.substr(0, xFileName.length() - 1); string cFileName = xFileName.substr(xFileName.find_last_of('/') + 1); struct stat st; stat(gl.gl_pathv[fi], &st); AddDirItem(list, cFileName.c_str(), isDir ? -1 : st.st_size, divisor.ival); } globfree(&gl); return Value(list); #else return Value(0, V_NIL); #endif }
std::string Repository::PathFromUrl(std::string url) { fs::path path(base_path_); UrlInfo url_info = ParseUrl(url); // добавляем директорию с схемой и доменом { std::string schema = url_info.schema; std::transform(schema.begin(), schema.end(), schema.begin(), tolower); std::string top_dir; if(schema == std::string("https")) { top_dir += "https_"; } else { top_dir += "http_"; } top_dir += url_info.domain; path /= SanitizePath(top_dir); } // добавляем директории с путем path /= SanitizePath(url_info.path); if(path.filename()=="." || url_info.path.empty()) { // если путь в URL заканчивается на / или пуст (только домен) path /= "_"; } // добавляем запрос if(!url_info.query.empty()) { path += '_'; path += url_info.query; } // добавляем расширение файла if(path.extension().empty()) { path += ".html"; } return path.native(); }
uchar *LoadFile(const char *relfilename, size_t *len, string *usedfilename) { auto srfn = SanitizePath(relfilename); uchar *result; if (tryloadfile(&result, datadir + srfn, len, usedfilename)) return result; if (tryloadfile(&result, auxdir + srfn, len, usedfilename)) return result; if (tryloadfile(&result, writedir + srfn, len, usedfilename)) return result; return NULL; }
bool SetupDefaultDirs(const char *exefilepath, const char *auxfilepath, bool from_bundle) { datadir = StripFilePart(exefilepath); auxdir = auxfilepath ? StripFilePart(SanitizePath(auxfilepath).c_str()) : datadir; writedir = auxdir; // FIXME: use SDL_GetBasePath() instead? #ifdef __APPLE__ if (from_bundle) { // default data dir is the Resources folder inside the .app bundle CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle); char path[PATH_MAX]; auto res = CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX); CFRelease(resourcesURL); if (!res) return false; datadir = string(path) + "/"; #ifdef __IOS__ writedir = StripFilePart(path) + "Documents/"; // there's probably a better way to do this in CF #else // FIXME: this should probably be ~/Library/Application Support/AppName, // but for now this works for non-app store apps writedir = datadir; #endif } #elif defined(__ANDROID__) SDL_Init(0); // FIXME, is this needed? bad dependency. auto internalstoragepath = SDL_AndroidGetInternalStoragePath(); auto externalstoragepath = SDL_AndroidGetExternalStoragePath(); Output(OUTPUT_INFO, internalstoragepath); Output(OUTPUT_INFO, externalstoragepath); if (internalstoragepath) datadir = internalstoragepath + string("/"); if (externalstoragepath) writedir = externalstoragepath + string("/"); // for some reason, the above SDL functionality doesn't actually work, // we have to use the relative path only to access APK files: datadir = ""; auxdir = writedir; #endif (void)from_bundle; return true; }
CommandExecutionResults CatCommand::Execute(const ExecutionContext& context) { auto& currentDir = GetCurrentPath(this->context->UserData, context.Message->SourceProtocol, context.Message->Sender); std::string path = SanitizePath(context.ParseResults->GetParameter("file")); std::string newPath = JoinPath(currentDir, path); if (newPath.empty()) { throw Exceptions::ExecutionException(InvalidPath); } if (PathIsDirectoryA(newPath.c_str())) { throw Exceptions::ExecutionException(PathIsNotFile); } if (!PathFileExistsA(newPath.c_str())) { throw Exceptions::ExecutionException(FileDoesNotExist); } std::ifstream file(newPath, std::ios_base::binary); file.seekg(0, std::ios_base::end); int64_t length = file.tellg(); file.seekg(0, std::ios_base::beg); if (length == -1 || !file) { throw Exceptions::ExecutionException(CannotReadFile); } if (length > MaxFileLength) { length = MaxFileLength; } std::unique_ptr<char[]> buffer(new char[(unsigned)length + 1]); if (file.read(buffer.get(), length).bad()) { throw Exceptions::ExecutionException(CannotReadFile); } buffer.get()[length] = 0; return CommandExecutionResults(std::string(buffer.get())); }
CommandExecutionResults StoreTextCommand::Execute(const ExecutionContext& context) { auto& currentDir = GetCurrentPath(this->context->UserData, context.Message->SourceProtocol, context.Message->Sender); auto& content = context.ParseResults->GetParameter("content"); auto path = SanitizePath(context.ParseResults->GetParameter("file")); auto newPath = JoinPath(currentDir, path); if (newPath.empty()) { throw Exceptions::ExecutionException(InvalidPath); } if (PathIsDirectoryA(newPath.c_str())) { throw Exceptions::ExecutionException(PathIsNotFile); } std::ofstream file(newPath, std::ios_base::binary | std::ios_base::trunc); file.write(content.c_str(), content.size()); if (!file) { throw Exceptions::ExecutionException(CannotWriteToFile); } return CommandExecutionResults(FileWritten); }
int _tmain(int argc, _TCHAR* argv[]) { char *path = NULL, *pathDir = NULL; FILE *fpMview = NULL; FILE *fpDst = NULL; DWORD flag, sizeData, sizeDataDecompressed; if(argc<2)goto err; fpMview = _wfopen(argv[1], L"rb"); if(fpMview==NULL)goto err; path = (char *)malloc(4096); if(path==NULL)goto err; pathDir = (char *)malloc(4096); if(pathDir==NULL)goto err; buf = (BYTE *)malloc(bufSize); if(buf==NULL)goto err; while(1) { flag=0; sizeData=0; sizeDataDecompressed=0; int numPath = freadNullStr(fpMview, path, 4096); if(numPath==0)goto err; SanitizePath(path); freadNullStr(fpMview, NULL, 0); if(fread(&flag, 1, 4, fpMview)!=4)goto err; if(fread(&sizeData, 1, 4, fpMview)!=4)goto err; if(fread(&sizeDataDecompressed, 1, 4, fpMview)!=4)goto err; if(flag & 0x1)printf("\"%s\" flag=%u (compressed!), size=%u, decompressedSize=%u\n", path, flag, sizeData, sizeDataDecompressed); else printf("\"%s\" flag=%u, size=%u\n", path, flag, sizeData); if(PathCombineA(path, "out\\", path)==NULL)goto err; strcpy_s(pathDir, 4096, path); PathRemoveFileSpecA(pathDir); PathAddBackslashA(pathDir); BOOL bRet = MakeSureDirectoryPathExists(pathDir); fpDst = fopen(path, "wb"); if(fpDst==NULL)goto err; if(!FILEtoFILE(fpDst, fpMview, sizeData))goto err; fclose(fpDst); } if(buf)free(buf); if(path)free(path); if(pathDir)free(pathDir); if(fpMview)fclose(fpMview); if(fpDst)fclose(fpDst); return 0; err: if(buf)free(buf); if(path)free(path); if(pathDir)free(pathDir); if(fpMview)fclose(fpMview); if(fpDst)fclose(fpDst); return 0; }
FILE *OpenForWriting(const char *relfilename, bool binary) { return fopen((writedir + SanitizePath(relfilename)).c_str(), binary ? "wb" : "w"); }