Readme.md - added Section for customization and notes
[biaweb2.git] / biawebutil.hpp
index b519615..279d394 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef __BIAWEBUTIL__
 #define __BIAWEBUTIL__
 #include <string>
+#include <fstream>
+
 // "discount" markdown library is a C library and hence requires to be wrapped in 
 // extern "C"
 extern "C" {
@@ -10,8 +12,15 @@ extern "C" {
 // utility functions for Biaweb that don't fit into any class and can be used by 
 // any class
 namespace biaweb {
-    // convert markdown
+    // load a string from file
+    std::string load_from_file (std::string filename) {
+        std::ifstream f (filename);
+        std::string r ((std::istreambuf_iterator<char> (f)),
+                        (std::istreambuf_iterator<char> ()));
+        return r;
+    }
 
+    // convert markdown
     std::string convert_to_markdown (std::string str) {
         // discount is a C library and it doesn't work well with C++ streams
         // and there seems no way to get the output of any of these functions
@@ -22,16 +31,15 @@ namespace biaweb {
         // till a cleaner solution can be found. 
         MMIOT *doc;
         doc = mkd_string (str.c_str(), str.size(), 0);
-        FILE *f = fopen (".biaweb.tmp", "w");
+        char tempfile[20];
+        strcpy (tempfile, "/tmp/biawebXXXXXX");
+        int fd = mkstemp (tempfile);
+        FILE *f = fdopen (fd, "w");
         markdown (doc, f, 0);
         fclose (f);
-        std::ifstream ftmp (".biaweb.tmp");
-        std::string tmpl ( (std::istreambuf_iterator<char> (ftmp)),
-                            (std::istreambuf_iterator<char> ())
-                                );
-        ftmp.close ();
+        std::string tmpl = load_from_file (tempfile);
         mkd_cleanup (doc);
-        remove (".biaweb.tmp");
+        remove (tempfile);
         return tmpl;
     }