Osmium
0.1
|
00001 #ifndef OSMIUM_EXPORT_CSV_HPP 00002 #define OSMIUM_EXPORT_CSV_HPP 00003 00004 /* 00005 00006 Copyright 2011 Jochen Topf <jochen@topf.org> and others (see README). 00007 00008 This file is part of Osmium (https://github.com/joto/osmium). 00009 00010 Osmium is free software: you can redistribute it and/or modify it under the 00011 terms of the GNU Lesser General Public License or (at your option) the GNU 00012 General Public License as published by the Free Software Foundation, either 00013 version 3 of the Licenses, or (at your option) any later version. 00014 00015 Osmium is distributed in the hope that it will be useful, but WITHOUT ANY 00016 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 00017 PARTICULAR PURPOSE. See the GNU Lesser General Public License and the GNU 00018 General Public License for more details. 00019 00020 You should have received a copy of the Licenses along with Osmium. If not, see 00021 <http://www.gnu.org/licenses/>. 00022 00023 */ 00024 00025 #include <fstream> 00026 00027 #ifdef OSMIUM_WITH_JAVASCRIPT 00028 # include <v8.h> 00029 #endif // OSMIUM_WITH_JAVASCRIPT 00030 00031 namespace Osmium { 00032 00033 namespace Export { 00034 00035 class CSV { 00036 00037 public: 00038 00039 std::ofstream out; 00040 00041 CSV(const char *filename) { 00042 out.open(filename); 00043 } 00044 00045 ~CSV() { 00046 out.flush(); 00047 out.close(); 00048 } 00049 00050 #ifdef OSMIUM_WITH_JAVASCRIPT 00051 v8::Local<v8::Object> js_instance() const { 00052 return JavascriptTemplate::get<JavascriptTemplate>().create_instance((void *)this); 00053 } 00054 00055 v8::Handle<v8::Value> js_print(const v8::Arguments& args) { 00056 for (int i = 0; i < args.Length(); i++) { 00057 if (i != 0) { 00058 out << '\t'; 00059 } 00060 Osmium::v8_String_to_ostream(args[i]->ToString(), out); 00061 } 00062 out << std::endl; 00063 return v8::Integer::New(1); 00064 } 00065 00066 v8::Handle<v8::Value> js_close(const v8::Arguments& /*args*/) { 00067 out.flush(); 00068 out.close(); 00069 return v8::Undefined(); 00070 } 00071 00072 struct JavascriptTemplate : public Osmium::Javascript::Template { 00073 00074 JavascriptTemplate() : Osmium::Javascript::Template() { 00075 js_template->Set("print", v8::FunctionTemplate::New(function_template<CSV, &CSV::js_print>)); 00076 js_template->Set("close", v8::FunctionTemplate::New(function_template<CSV, &CSV::js_close>)); 00077 } 00078 00079 }; 00080 #endif // OSMIUM_WITH_JAVASCRIPT 00081 00082 }; // class CSV 00083 00084 } // namespace Export 00085 00086 } // namespace Osmium 00087 00088 #endif // OSMIUM_EXPORT_CSV_HPP