Variant f_bzopen(CVarRef filename, CStrRef mode) { if (mode != "r" && mode != "w") { raise_warning( "'%s' is not a valid mode for bzopen(). " "Only 'w' and 'r' are supported.", mode.data() ); return false; } BZ2File *bz; if (filename.isString()) { if (filename.asCStrRef().empty()) { raise_warning("filename cannot be empty"); return false; } bz = NEWOBJ(BZ2File)(); bool ret = bz->open(File::TranslatePath(filename.toString()), mode); if (!ret) { raise_warning("%s", Util::safe_strerror(errno).c_str()); return false; } } else { if (!filename.isResource()) { raise_warning("first parameter has to be string or file-resource"); return false; } PlainFile* f = filename.cast<PlainFile>(); if (!f) { return false; } std::string stream_mode = f->getMode(); int stream_mode_len = stream_mode.length(); if (stream_mode_len != 1 && !(stream_mode_len == 2 && stream_mode.find('b') != string::npos)) { raise_warning("cannot use stream opened in mode '%s'", stream_mode.c_str()); return false; } else if (stream_mode_len == 1 && stream_mode[0] != 'r' && stream_mode[0] != 'w' && stream_mode[0] != 'a' && stream_mode[0] != 'x') { raise_warning("cannot use stream opened in mode '%s'", stream_mode.c_str()); return false; } const char rw_mode = stream_mode[0]; if (mode == "r" && rw_mode != 'r') { raise_warning("cannot write to a stream opened in read only mode"); return false; } else if (mode == "w" && rw_mode != 'w' && rw_mode != 'a' && rw_mode != 'x') { raise_warning("cannot read from a stream opened in write only mode"); return false; } bz = NEWOBJ(BZ2File)(f); } Object handle(bz); return handle; }
bool TestExtMailparse::test_mailparse_msg_create() { const char *files[] = { "mime", "phpcvs1", "qp", "uue" }; for (unsigned int i = 0; i < sizeof(files)/sizeof(files[0]); i++) { string file = files[i]; string testname = "test/ext/test_ext_mailparse." + file + ".txt"; string expname = "test/ext/test_ext_mailparse." + file + ".exp"; Variant mime = f_mailparse_msg_create(); PlainFile input; input.open(testname, "r"); while (!input.eof()) { String data = input.read(1024); if (!data.isNull()) { f_mailparse_msg_parse(mime, data); } } input.close(); Array arr = f_mailparse_msg_get_structure(mime); f_ob_start(); echo("Message: "); echo(file.c_str()); echo("\n"); for (ArrayIter iter(arr); iter; ++iter) { Variant partname = iter.second(); int depth = f_count(f_explode(".", partname)) - 1; String indent = f_str_repeat(" ", depth * 2); Variant subpart = f_mailparse_msg_get_part(mime, partname); if (subpart.isNull()) { f_var_dump(partname); echo("\n"); f_var_dump(arr); break; } Variant data = f_mailparse_msg_get_part_data(subpart); echo("\n"); echo(indent); echo("Part "); echo(partname); echo("\n"); f_ksort(ref(data)); for (ArrayIter iter(data); iter; ++iter) { String key = iter.first().toString(); if (key != "headers" && key != "ending-pos-body") { echo(indent); echo(key); echo(" => "); f_var_dump(iter.second()); } } } String output = f_ob_get_contents(); Variant expect = f_file_get_contents(expname.c_str()); VS(output, expect); f_ob_end_clean(); } return Count(true); }
int64 f_stream_set_write_buffer(CObjRef stream, int buffer) { PlainFile *file = stream.getTyped<PlainFile>(false, true); if (file) { switch (buffer) { case PHP_STREAM_BUFFER_NONE: return setvbuf(file->getStream(), NULL, _IONBF, 0); case PHP_STREAM_BUFFER_LINE: return setvbuf(file->getStream(), NULL, _IOLBF, BUFSIZ); case PHP_STREAM_BUFFER_FULL: return setvbuf(file->getStream(), NULL, _IOFBF, BUFSIZ); default: break; } } return -1; }
Variant HHVM_FUNCTION(bzopen, const Variant& filename, const String& mode) { if (mode != s_r && mode != s_w) { raise_warning( "'%s' is not a valid mode for bzopen(). " "Only 'w' and 'r' are supported.", mode.data() ); return false; } BZ2File *bz; if (filename.isString()) { if (filename.asCStrRef().empty()) { raise_warning("filename cannot be empty"); return false; } bz = newres<BZ2File>(); bool ret = bz->open(File::TranslatePath(filename.toString()), mode); if (!ret) { raise_warning("%s", folly::errnoStr(errno).c_str()); return false; } } else { if (!filename.isResource()) { raise_warning("first parameter has to be string or file-resource"); return false; } PlainFile* f = filename.toResource().getTyped<PlainFile>(); if (!f) { return false; } std::string stream_mode = f->getMode(); int stream_mode_len = stream_mode.length(); if (stream_mode_len != 1 && !(stream_mode_len == 2 && stream_mode.find('b') != std::string::npos)) { raise_warning("cannot use stream opened in mode '%s'", stream_mode.c_str()); return false; } else if (stream_mode_len == 1 && stream_mode[0] != 'r' && stream_mode[0] != 'w' && stream_mode[0] != 'a' && stream_mode[0] != 'x') { raise_warning("cannot use stream opened in mode '%s'", stream_mode.c_str()); return false; } const char rw_mode = stream_mode[0]; if (mode == s_r && rw_mode != 'r') { raise_warning("cannot write to a stream opened in read only mode"); return false; } else if (mode == s_w && rw_mode != 'w' && rw_mode != 'a' && rw_mode != 'x') { raise_warning("cannot read from a stream opened in write only mode"); return false; } bz = newres<BZ2File>(f); } Resource handle(bz); return handle; }