void HTTPFormClass::saveFile(Stringp key, Stringp destPath) { string sKey(((StUTF8String) key).c_str()); for (std::vector<FormFile>::iterator it=sapi->files.begin(); it!=sapi->files.end(); it++) { if (sKey == (*it).name) { StUTF8String filenameUTF8(destPath); File* fp = Platform::GetInstance()->createFile(); if (!fp || !fp->open(filenameUTF8.c_str(), File::OPEN_WRITE)) { if(fp) { Platform::GetInstance()->destroyFile(fp); } toplevel()->throwError(kFileWriteError, destPath); } if ((int32_t)fp->write((*it).data.data(), (*it).data.length()) != (int32_t)(*it).data.length()) toplevel()->throwError(kFileWriteError, destPath); fp->close(); Platform::GetInstance()->destroyFile(fp); } } }
Stringp FileClass::read(Stringp filename) { Toplevel* toplevel = this->toplevel(); AvmCore* core = this->core(); if (!filename) { toplevel->throwArgumentError(kNullArgumentError, "filename"); } StUTF8String filenameUTF8(filename); File* fp = Platform::GetInstance()->createFile(); if(!fp || !fp->open(filenameUTF8.c_str(), File::OPEN_READ)) { if(fp) { Platform::GetInstance()->destroyFile(fp); } toplevel->throwError(kFileOpenError, filename); } int64_t fileSize = fp->size(); if(fileSize >= (int64_t)INT32_T_MAX) //File APIs cannot handle files > 2GB { toplevel->throwRangeError(kOutOfRangeError, filename); } int len = (int)fileSize; MMgc::GC::AllocaAutoPtr _c; union { uint8_t* c; wchar* c_w; }; c = (uint8_t*)VMPI_alloca(core, _c, len+1); len = (int)fp->read(c, len); //need to force since the string creation functions expect an int c[len] = 0; fp->close(); Platform::GetInstance()->destroyFile(fp); if (len >= 3) { // UTF8 BOM if ((c[0] == 0xef) && (c[1] == 0xbb) && (c[2] == 0xbf)) { return core->newStringUTF8((const char*)c + 3, len - 3); } else if ((c[0] == 0xfe) && (c[1] == 0xff)) { //UTF-16 big endian c += 2; len = (len - 2) >> 1; return core->newStringEndianUTF16(/*littleEndian*/false, c_w, len); } else if ((c[0] == 0xff) && (c[1] == 0xfe))
avmplus::Stringp FileClass::read(avmplus::Stringp filename) { avmplus::Toplevel* toplevel = this->toplevel(); avmplus::AvmCore* core = this->core(); if (!filename) { toplevel->throwArgumentError(kNullArgumentError, "filename"); } avmplus::StUTF8String filenameUTF8(filename); File* fp = Platform::GetInstance()->createFile(); if(!fp || !fp->open(filenameUTF8.c_str(), File::OPEN_READ)) { if(fp) { Platform::GetInstance()->destroyFile(fp); } toplevel->throwError(kFileOpenError, filename); } int64_t fileSize = fp->size(); if(fileSize >= (int64_t)INT32_T_MAX) //File APIs cannot handle files > 2GB { toplevel->throwRangeError(kOutOfRangeError, filename); } int len = (int)fileSize; // Avoid avmStackAlloc - the buffer can be large and the memory is non-pointer-containing, // but the GC will scan it conservatively. uint8_t* c = (uint8_t*)core->gc->Alloc(len+1, MMgc::kNone, MMgc::kAVMShellGCPartition); len = (int)fp->read(c, len); //need to force since the string creation functions expect an int c[len] = 0; fp->close(); Platform::GetInstance()->destroyFile(fp); avmplus::Stringp ret = NULL; if (len >= 3) { // UTF8 BOM if ((c[0] == 0xef) && (c[1] == 0xbb) && (c[2] == 0xbf)) { ret = core->newStringUTF8((const char*)c + 3, len - 3); } else if ((c[0] == 0xfe) && (c[1] == 0xff)) { //UTF-16 big endian c += 2; len = (len - 2) >> 1; ret = core->newStringEndianUTF16(/*littleEndian*/false, (wchar*)(void*)c, len); } else if ((c[0] == 0xff) && (c[1] == 0xfe))
static DKObject<UnZipFile> Create(const DKString& zipFile, const DKString& file, const char* password) { if (zipFile.Length() == 0 || file.Length() == 0) return NULL; DKString filename = zipFile.FilePathString(); unzFile uf = NULL; #ifdef _WIN32 { zlib_filefunc64_def ffunc; fill_win32_filefunc64W(&ffunc); uf = unzOpen2_64((const wchar_t*)filename, &ffunc); // UTF16LE } #else { DKStringU8 filenameUTF8(filename); if (filenameUTF8.Bytes() > 0) uf = unzOpen64((const char*)filenameUTF8); // UTF8 } #endif if (uf) { unz_file_info64 file_info; DKStringU8 fileUTF8(file); if (fileUTF8.Bytes() > 0 && unzLocateFile(uf, (const char*)fileUTF8, 0) == UNZ_OK && unzGetCurrentFileInfo64(uf, &file_info, NULL, 0, NULL, 0, NULL, 0) == UNZ_OK && file_info.uncompressed_size > 0) { if (unzOpenCurrentFilePassword(uf, password) == UNZ_OK) { DKObject<UnZipFile> p = DKOBJECT_NEW UnZipFile(uf, file_info, password); return p; } else { DKLog("[%s] failed to open file: %ls.\n", DKLIB_FUNCTION_NAME, (const wchar_t*)file); } } } return NULL; }
bool FileClass::exists(Stringp filename) { if (!filename) { toplevel()->throwArgumentError(kNullArgumentError, "filename"); } bool result = false; StUTF8String filenameUTF8(filename); File* fp = Platform::GetInstance()->createFile(); if(fp) { if (fp->open(filenameUTF8.c_str(), File::OPEN_READ)) { result = true; fp->close(); } Platform::GetInstance()->destroyFile(fp); } return result; }