X-Git-Url: https://harishankar.org/repos/?p=biaweb2.git;a=blobdiff_plain;f=biawebutil.hpp;h=279d394471fab1e2fcdb5adf3aac2d14c5afbb39;hp=6a878271b40e20bb4a0e6df4cb2da876884f9881;hb=HEAD;hpb=26b38b4a38c24955293a7144f1e1d74676601caf diff --git a/biawebutil.hpp b/biawebutil.hpp index 6a87827..279d394 100644 --- a/biawebutil.hpp +++ b/biawebutil.hpp @@ -1,6 +1,7 @@ #ifndef __BIAWEBUTIL__ #define __BIAWEBUTIL__ #include +#include // "discount" markdown library is a C library and hence requires to be wrapped in // extern "C" @@ -11,6 +12,13 @@ extern "C" { // utility functions for Biaweb that don't fit into any class and can be used by // any class namespace biaweb { + // load a string from file + std::string load_from_file (std::string filename) { + std::ifstream f (filename); + std::string r ((std::istreambuf_iterator (f)), + (std::istreambuf_iterator ())); + return r; + } // convert markdown std::string convert_to_markdown (std::string str) { @@ -25,16 +33,13 @@ namespace biaweb { doc = mkd_string (str.c_str(), str.size(), 0); char tempfile[20]; strcpy (tempfile, "/tmp/biawebXXXXXX"); - mkstemp (tempfile); - FILE *f = fopen (tempfile, "w"); + int fd = mkstemp (tempfile); + FILE *f = fdopen (fd, "w"); markdown (doc, f, 0); fclose (f); - std::ifstream ftmp (tempfile); - std::string tmpl ( (std::istreambuf_iterator (ftmp)), - (std::istreambuf_iterator ()) - ); - ftmp.close (); + std::string tmpl = load_from_file (tempfile); mkd_cleanup (doc); + remove (tempfile); return tmpl; }