InputCodedBlockFile* LocalFileSystem::OpenInput(const std::string& uri){ std::auto_ptr<LocalInputCodedBlockFile> file_ptr(new LocalInputCodedBlockFile()); if (!file_ptr->Open(uri)){ file_ptr.reset(); } return file_ptr.release(); }
boost::shared_ptr<display::PalettizedSurface> Filesystem::readImage(const std::string &filename) { // open the file boost::shared_ptr<File> file_ptr(open(filename)); // get its length, ensuring it's something we're okay with loading from the stack uint64_t length = file_ptr->length(); assert(length < 1024 * 1024); // allocate a buffer on the stack and read into it uint8_t *buffer = new uint8_t[length]; uint64_t bytes_read = file_ptr->read(buffer, length); if (bytes_read < length) { delete[] buffer; throw_error; } // construct a PNGImage from this buffer boost::shared_ptr<display::PalettizedSurface> image(display::image::readPalettizedPNG(buffer, length)); delete[] buffer; // pass it back to the caller return image; }
avlog::ofstream_ptr avlog::create_file(const std::string& groupid) const { // 生成对应的路径. std::string save_path = make_path(groupid); // 如果不存在, 则创建目录. if (!fs::exists(fs::path(save_path))) { if (!fs::create_directory(fs::path(save_path))) { std::cerr << "create directory " << save_path.c_str() << " failed!" << std::endl; return ofstream_ptr(); } } // 按时间构造文件名. save_path = make_filename(save_path); // 创建文件. ofstream_ptr file_ptr(new std::ofstream(save_path.c_str(), fs::exists(save_path) ? std::ofstream::app : std::ofstream::out)); if (file_ptr->bad() || file_ptr->fail()) { std::cerr << "create file " << save_path.c_str() << " failed!" << std::endl; return ofstream_ptr(); } // 写入信息头. std::string info = "<head><meta http-equiv=\"Content-Type\" content=\"text/plain; charset=UTF-8\">\n"; file_ptr->write(info.c_str(), info.length()); return file_ptr; }
static VALUE rb_shadow_fgetspent(VALUE self, VALUE file) { struct spwd *entry; VALUE result; if( TYPE(file) != T_FILE ) rb_raise(rb_eTypeError,"argument must be a File."); entry = fgetspent(file_ptr(RFILE(file)->fptr)); if( entry == NULL ) return Qnil; result = rb_struct_new(rb_sPasswdEntry, rb_tainted_str_new2(entry->sp_namp), rb_tainted_str_new2(entry->sp_pwdp), INT2FIX(entry->sp_lstchg), INT2FIX(entry->sp_min), INT2FIX(entry->sp_max), INT2FIX(entry->sp_warn), INT2FIX(entry->sp_inact), INT2FIX(entry->sp_expire), INT2FIX(entry->sp_flag), 0); return result; };
static VALUE rb_shadow_putspent(VALUE self, VALUE entry, VALUE file) { struct spwd centry; FILE* cfile; VALUE val[9]; int i; int result; for(i=0; i<=8; i++) val[i] = RSTRUCT_PTR(entry)[i]; cfile = file_ptr(RFILE(file)->fptr); centry.sp_namp = StringValuePtr(val[0]); centry.sp_pwdp = StringValuePtr(val[1]); centry.sp_lstchg = FIX2INT(val[2]); centry.sp_min = FIX2INT(val[3]); centry.sp_max = FIX2INT(val[4]); centry.sp_warn = FIX2INT(val[5]); centry.sp_inact = FIX2INT(val[6]); centry.sp_expire = FIX2INT(val[7]); centry.sp_flag = FIX2INT(val[8]); result = putspent(¢ry,cfile); if( result == -1 ) rb_raise(rb_eStandardError,"can't change password"); return Qtrue; };
OutputCodedBlockFile* LocalFileSystem::OpenOutput(const std::string& uri, short replication, uint64 chunk_size){ CHECK_EQ(replication, 0) << "local file system doesn't support replication"; CHECK_EQ(chunk_size, 0) << "local file system doesn't use chunks."; std::auto_ptr<LocalOutputCodedBlockFile> file_ptr(new LocalOutputCodedBlockFile()); if (!file_ptr->Open(uri, replication, chunk_size)){ file_ptr.reset(); } return file_ptr.release(); }
boost::shared_ptr<File> Filesystem::openWrite(const std::string &filename) { PHYSFS_File *file_handle = PHYSFS_openWrite(filename.c_str()); if (!file_handle) { throw_error; } boost::shared_ptr<File> file_ptr(new File(file_handle)); return file_ptr; }
void QueuesToStorageFiles::addLog(std::string valueName) { std::string filename = valueName + ".sto"; std::string outFilename = outputDir_ + filename; std::shared_ptr<std::ofstream> file_ptr(new std::ofstream(outFilename.c_str())); if (!(file_ptr->is_open())) { std::cout << "ERROR: " + filename + " cannot be opened!\n"; exit(EXIT_FAILURE); } outFiles_.insert(std::make_pair(valueName, file_ptr)); }
void Filesystem::readToBuffer(const std::string &filename, void *buffer, uint32_t length, uint32_t offset) { boost::shared_ptr<File> file_ptr(open(filename)); if (offset) { file_ptr->seek(offset); } uint32_t bytes_read = file_ptr->read(buffer, length); if (bytes_read < length) { throw_error; } }
static VALUE rb_shadow_fgetspent(VALUE self, VALUE file) { struct spwd *entry; VALUE result; if( TYPE(file) != T_FILE ) rb_raise(rb_eTypeError,"argument must be a File."); entry = fgetspent( file_ptr( (RFILE(file)->fptr) ) ); if( entry == NULL ) return Qnil; result = convert_pw_struct( entry ); return result; };
std::string tail(const std::string &file_name, int N) { fstream file_ptr(file_name.c_str()); file_ptr.seekg(0, ios::end); int file_size = file_ptr.tellg(), newline_count = 0; string last_N_lines; for (int i=0; i<file_size; ++i) { file_ptr.seekg(-1 - i, ios::end); char c; file_ptr.get(c); if (c == '\n') { if (++newline_count >= N) break; } last_N_lines.append(1, c); } reverse(last_N_lines.begin(), last_N_lines.end()); return last_N_lines; }