changeset 17384:609d93683f15

Using file_ops::concat to make files. And using strings to operate with files.
author Andrej Lojdl <andrej.lojdl@gmail.com>
date Thu, 05 Sep 2013 22:55:13 +0200
parents f983570ba8b8
children 1a1d0cff1977
files libinterp/corefcn/txt-latex.cc libinterp/corefcn/txt-latex.h
diffstat 2 files changed, 180 insertions(+), 197 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/txt-latex.cc
+++ b/libinterp/corefcn/txt-latex.cc
@@ -29,8 +29,7 @@
 #include <string>
 #include <cstdlib>
 #include <cstring>
-
-#include <stdlib.h>
+#include <sstream>
 
 #ifdef ENABLE_LATEX
 #include <png.h>
@@ -39,6 +38,7 @@
 #include "txt-latex.h"
 #include "oct.h"
 #include "dim-vector.h"
+#include "file-ops.h"
 
 
 latex_render::latex_render (void)
@@ -54,40 +54,36 @@
 latex_render::adapter (const std::string& txt)
 {
   std::ofstream file;
-  char def[] = "/tmp/latexrenderXXXXXX"; // default path to tmp directory 
-  char *dp;
-  
-  /* Creating unique temporary directory */ 
-  dp = mkdtemp(def);
-  
+  std::string def = "latex", directory,tex;
+  int st;
+
+  directory = file_ops::concat("","tmp");
+
+  /* Creating unique name of directory */ 
+  dp = octave_tempnam(directory, def);
+
+  /* Creating the directory */
+  st = octave_mkdir(dp, 0700);
+
   /*Check if directory is created successfully. */  
-  if (dp != NULL)
-  {
-     /* Pointing to temporary directory, for creating .tex file */
-     sprintf(tex,"%s/%s",def,"default.tex");
-     
-     /* Creating and writing to temporary .tex file */
-     file.open (tex);
-     file << "\\documentclass[fleqn]{article} \n\\usepackage{amsmath} \n\\usepackage{color} \n\\pagestyle{empty}	\n\\begin{document} \n\t\\fontsize{12pt}{12pt}\\selectfont \n\t";
-     file << "\\usefont{T1}{" << fname << "}{m}{n} \n\t\t";
-     file << txt;
-     file << "\n\\end{document}";
-     file.close();
-     
-     /* Pointing to temporary directory, for using all files needed. */
-     sprintf(log,"%s/%s",def,"default.log");
-     sprintf(dvi,"%s/%s",def,"default.dvi");
-     sprintf(eps,"%s/%s",def,"default.eps");
-     sprintf(png,"%s/%s",def,"default.png");
-     sprintf(aux,"%s/%s",def,"default.aux");
-     
-     strcpy(path,def);
-  }
+  if (st == 0)
+    {
+      /* Pointing to temporary directory, for creating .tex file */
+      tex = file_ops::concat(dp,"default.tex");
+
+      /* Creating and writing to temporary .tex file */
+      file.open (tex.c_str());
+      file << "\\documentclass[fleqn]{article} \n\\usepackage{amsmath} \n\\usepackage{color} \n\\pagestyle{empty}	\n\\begin{document} \n\t\\fontsize{12pt}{12pt}\\selectfont \n\t";
+      file << "\\usefont{T1}{" << fname << "}{m}{n} \n\t\t";
+      file << txt;
+      file << "\n\\end{document}";
+      file.close();  
+    }
   else
-  {
-     /* Printing error if directory is not created */
-     ::error("Failed to create temp directory.");
-  }
+    {
+      /* Printing error if directory is not created */
+      ::error("Failed to create temp directory.");
+    }
 }
 
 uint8NDArray 
@@ -97,203 +93,191 @@
   std::string comment;
   uint8NDArray data;
   std::ifstream file;
-  char command[230];
+  std::string sfsize, tex, aux, log, dvi, eps, png, command;
 
   /* Check if command processor is available */
   if(system( NULL ))
     {
-      sprintf(command,"latex -output-directory=%s %s > %s", path, tex, log);
-      
+      log = file_ops::concat(dp,"default.log");
+      tex = file_ops::concat(dp,"default.tex");
+      command = "latex -output-directory="+dp+" "+tex+" > "+log;
+
       /* .tex -> .dvi */
-      cmd = system (command);
-      
+      cmd = system (command.c_str());
+
       if(cmd != 0)
-      {
-         data = uint8NDArray (dim_vector (4, 2, 2), static_cast<uint8_t> (0));
-         
-         ::error("LaTeX converting .tex to .dvi file, failed.");
-         
-         file.open(log);
-         
+        {
+          data = uint8NDArray (dim_vector (4, 2, 2), static_cast<uint8_t> (0));
+
+          ::error("LaTeX converting .tex to .dvi file, failed.");
+
+          file.open(log.c_str());
+
           for(int i=0; i<33; i++)
-        {
+            {
+              std::getline(file,comment);
+            }
+
+          /* Print additional info from default.log */
+          comment = "Here is LaTeX description of error:";
+          std::cout<<comment<<std::endl;
           std::getline(file,comment);
+          std::cout<<comment<<std::endl;
+          std::getline(file,comment); 
+          std::cout<<comment<<std::endl; 
+          file.close();       
+
+          return data;         
         }
-        
-         /* Print additional info from default.log */
-         comment = "Here is LaTeX description of error:";
-         std::cout<<comment<<std::endl;
-         std::getline(file,comment);
-         std::cout<<comment<<std::endl;
-         std::getline(file,comment); 
-         std::cout<<comment<<std::endl; 
-         file.close();       
-                  
-         return data;         
-      }
-          
-          /* .dvi -> .eps */     
-          sprintf(command,"dvips %s -E -o %s -q",dvi, eps);
-          
-          cmd = system (command);
-          
-          if(cmd != 0)
-          {
-             data = uint8NDArray (dim_vector (4, 2, 2), static_cast<uint8_t> (0));
-             
-             ::error("dvips converting .dvi to .eps file file, failed");
-             
-             return data;  
-          }
+
+      /* .dvi -> .eps */  
+      dvi = file_ops::concat(dp,"default.dvi"); 
+      eps = file_ops::concat(dp,"default.eps");
+      command = "dvips "+dvi+" -E -o "+eps+" -q";
 
-          /* .eps -> .png */
-          fsize = fsize*(72/12);
-     
-          sprintf(command, "gs -q -dEPSCrop -dSAFER -dBATCH -dNOPAUSE -r%d -dTextAlphaBits=4 -sDEVICE=pngalpha -sOutputFile=%s \"%s\"", fsize, png, eps);
+      cmd = system (command.c_str());
+
+      if(cmd != 0)
+        {
+          data = uint8NDArray (dim_vector (4, 2, 2), static_cast<uint8_t> (0));
 
-          cmd = system (command);
-          
-          if(cmd != 0)
-          {
-             data = uint8NDArray (dim_vector (4, 2, 2), static_cast<uint8_t> (0));
-             
-             ::error("GhostScript converting .eps to .png file, failed");
-             
-             return data; 
-          }
+          ::error("dvips converting .dvi to .eps file file, failed");
+
+          return data;  
+        }
+
+      /* .eps -> .png */
+      fsize = fsize*(72/12);
+
+      png = file_ops::concat(dp,"default.png");
+      command = "gs -q -dEPSCrop -dSAFER -dBATCH -dNOPAUSE -r100 -dTextAlphaBits=4 -sDEVICE=pngalpha -sOutputFile="+png+" \""+eps+"\" "; 
+
+      cmd = system (command.c_str());
+
+      if(cmd != 0)
+        {
+          data = uint8NDArray (dim_vector (4, 2, 2), static_cast<uint8_t> (0));
+
+          ::error("GhostScript converting .eps to .png file, failed");
+
+          return data; 
+        }
 
-          /* Removing temporary files and checking if there are removed successfully*/
-          cmd = remove (tex);
-          if(cmd != 0) 
-             ::warning("default.tex file is not deleted from temp directory.");
-          cmd = remove (dvi);
-          if(cmd != 0) 
-             ::warning("default.dvi file is not deleted from temp directory.");
-          cmd = remove (log);
-          if(cmd != 0) 
-             ::warning("default.log file is not deleted from temp directory.");
-          cmd = remove (aux);
-          if(cmd != 0) 
-             ::warning("default.aux file is not deleted from temp directory.");
-          cmd = remove (eps);
-          if(cmd != 0) 
-             ::warning("default.eps file is not deleted from temp directory.");
-          /* FIXME - the file that is not deleted should be forced to remove */
+      /* Read image from PNG file */
+      int x;
+      int y;
+
+      int width, height;
+      png_byte color_type;
+      png_byte bit_depth;
 
-          /* Read image from PNG file */
-          int x;
-          int y;
+      png_structp png_ptr;
+      png_infop info_ptr;
+      int number_of_passes;
+      png_bytep * row_pointers;
 
-          int width, height;
-          png_byte color_type;
-          png_byte bit_depth;
+      char header [8];
 
-          png_structp png_ptr;
-          png_infop info_ptr;
-          int number_of_passes;
-          png_bytep * row_pointers;
-
-          char header [8];
-
-          /* Open file and test for it being a png */
-          FILE *fp = fopen(png, "rb");
-          if (!fp)
-            {
-              ::error("File default.png could not be opened for reading");
-            }
-          fread(header, 1, 8, fp);
+      /* Open file and test for it being a png */
+      FILE *fp = fopen(png.c_str(), "rb");
+      if (!fp)
+        {
+          ::error("File default.png could not be opened for reading");
+        }
+      fread(header, 1, 8, fp);
 
 
-          /* Initialize stuff */
-          png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+      /* Initialize stuff */
+      png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+
+      if (!png_ptr)
+        {
+          ::error("png_create_read_struct failed");
+        }
 
-          if (!png_ptr)
-            {
-              ::error("png_create_read_struct failed");
-            }
+      info_ptr = png_create_info_struct(png_ptr);
+      if (!info_ptr)
+        {
+          ::error("png_create_info_struct failed");
+        }
 
-          info_ptr = png_create_info_struct(png_ptr);
-          if (!info_ptr)
-            {
-              ::error("png_create_info_struct failed");
-            }
+      if (setjmp(png_jmpbuf(png_ptr)))
+        {
+          ::error("Error during init_io");
+        }
+
+      png_init_io(png_ptr, fp);
+      png_set_sig_bytes(png_ptr, 8);
+
+      png_read_info(png_ptr, info_ptr);
 
-          if (setjmp(png_jmpbuf(png_ptr)))
-            {
-              ::error("Error during init_io");
-            }
+      width = png_get_image_width(png_ptr, info_ptr);
+      height = png_get_image_height(png_ptr, info_ptr);
+      color_type = png_get_color_type(png_ptr, info_ptr);
+      bit_depth = png_get_bit_depth(png_ptr, info_ptr);
 
-          png_init_io(png_ptr, fp);
-          png_set_sig_bytes(png_ptr, 8);
+      number_of_passes = png_set_interlace_handling(png_ptr);
+      png_read_update_info(png_ptr, info_ptr);
 
-          png_read_info(png_ptr, info_ptr);
+      /* Read file */
+      if (setjmp(png_jmpbuf(png_ptr)))
+        {
+          ::error("Error during read_image");
+        }
 
-          width = png_get_image_width(png_ptr, info_ptr);
-          height = png_get_image_height(png_ptr, info_ptr);
-          color_type = png_get_color_type(png_ptr, info_ptr);
-          bit_depth = png_get_bit_depth(png_ptr, info_ptr);
+      row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
+      for (y=0; y<height; y++)
+        row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
+
+      png_read_image(png_ptr, row_pointers);
+
+      fclose(fp);
 
-          number_of_passes = png_set_interlace_handling(png_ptr);
-          png_read_update_info(png_ptr, info_ptr);
+      /* Instantise data array */
+      data = uint8NDArray (dim_vector (4, width, height), static_cast<uint8_t> (0));
 
-          /* Read file */
-          if (setjmp(png_jmpbuf(png_ptr)))
+      for (int i=0; i<height; i++)
+        {
+          png_byte* row = row_pointers[height-1-i];
+          for (int j=0; j<width; j++)
             {
-              ::error("Error during read_image");
-            }
-
-          row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
-          for (y=0; y<height; y++)
-            row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
-
-          png_read_image(png_ptr, row_pointers);
-
-          fclose(fp);
-
-          /* Instantise data array */
-          data = uint8NDArray (dim_vector (4, width, height), static_cast<uint8_t> (0));
+              png_byte* ptr = &(row[j*4]);
 
-          for (int i=0; i<height; i++)
-            {
-              png_byte* row = row_pointers[height-1-i];
-              for (int j=0; j<width; j++)
+              /* If the color of text is black we can change it, if not we render the original color. */
+              if(ptr[0] < 50 && ptr[1] < 50 && ptr[2] < 50 )
                 {
-                  png_byte* ptr = &(row[j*4]);
-
-                  /* If the color of text is black we can change it, if not we render the original color. */
-                  if(ptr[0] < 50 && ptr[1] < 50 && ptr[2] < 50 )
-                    {
-                      data(0,j,i) = red;
-                      data(1,j,i) = green;
-                      data(2,j,i) = blue;
-                      data(3,j,i) = ptr[3];
-                    }
-                  else
-                    {
-                      data(0,j,i) = ptr[0];
-                      data(1,j,i) = ptr[1];
-                      data(2,j,i) = ptr[2];
-                      data(3,j,i) = ptr[3];                  	
-                    }
+                  data(0,j,i) = red;
+                  data(1,j,i) = green;
+                  data(2,j,i) = blue;
+                  data(3,j,i) = ptr[3];
+                }
+              else
+                {
+                  data(0,j,i) = ptr[0];
+                  data(1,j,i) = ptr[1];
+                  data(2,j,i) = ptr[2];
+                  data(3,j,i) = ptr[3];                  	
                 }
             }
-            
-          /* Removing .png file and the temporary directory. */
-          cmd = remove(png);
-          if(cmd != 0) 
-             ::warning("default.tex file is not deleted from temp directory.");
-             
-          unlink(path);
-          cmd = remove(path);
+        }
+
+      /* Remove the temporary directory. */  
+      if( !dp.empty() )         
+        cmd = octave_recursive_rmdir(dp);
     }
   else
     {
+      /* It's possible that command procesor is not
+         available and we already made a temporary dir*/  
+      if( !dp.empty() )         
+        cmd = octave_recursive_rmdir(dp);
+
       data = uint8NDArray (dim_vector (4, 2, 2), static_cast<uint8_t> (0));
-      
+
       ::warning("Command processor not ready yet, please try again."); 
     }  
-    
-    return data;
+
+  return data;
 }
 
 void
--- a/libinterp/corefcn/txt-latex.h
+++ b/libinterp/corefcn/txt-latex.h
@@ -59,11 +59,10 @@
 
 private:
   int fsize;
-  std::string fname;
+  std::string fname,dp;
   Matrix bbox;
   uint8NDArray pixels;
   uint8_t red, green, blue;
-  char tex[40],aux[45],log[45],dvi[45],eps[45],png[45], path[23], *dp;
 
 };
 #endif