Changeset 70


Ignore:
Timestamp:
Feb 22, 2006, 8:40:36 AM (13 years ago)
Author:
Jari Häkkinen
Message:

Fixed too many files open problem.

Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Gnuplot.cc

    r64 r70  
    44
    55#include <cstdio>
     6#include <string>
    67#include <unistd.h>
    7 #include <string>
    88
    9 #include <iostream>
    109
    1110namespace theplu {
  • trunk/lib/Gnuplot.h

    r65 r70  
    2727  /// communication with the binary.
    2828  ///
    29   /// Temporary files are created in directory /tmp. These temporaru
     29  /// Temporary files are created in directory /tmp. These temporary
    3030  /// files are removed on a normal program exit.
    3131  ///
     
    143143      assert(plotcmd=="plot" || plotcmd=="replot");
    144144      char name[]="/tmp/svnstatXXXXXX";
    145       if (mkstemp(name) == -1){
     145      int fd=mkstemp(name); // mkstemp return a file descriptor
     146      if (fd == -1)
    146147        throw GnuplotException(std::string("Failed to get unique filename: ") +
    147148                               name);
    148       }
     149      // Jari would like to do something like 'std::ofstream tmp(fd);'
     150      // but has to settle for (which is stupid since the file is
     151      // already open for writing.
    149152      std::ofstream tmp(name);
    150       if (tmp.bad())
    151         throw GnuplotException(std::string("Failed to create file: ") + name);
    152      
    153       for (size_t i = 0; i<y.size() && (i<x.size() || x.empty()); ++i ){
     153      for (size_t i = 0; i<y.size() && (i<x.size() || x.empty()); ++i )
    154154        if (x.empty())
    155155          tmp << y[i] << '\n';
    156156        else
    157157          tmp << x[i] << '\t' << y[i] << '\n';
    158       }
     158      // Jari, here the stupidity goes beyond everything - the
     159      // ofstream must be closed as well as the file descriptor. If
     160      // not the program will exhaust the availabe number of
     161      // simultaneous open files.
    159162      tmp.close();
     163      close(fd);
     164
    160165      std::string cmdstring(plotcmd + " '" + name + "' u 1:2 title '" +
    161166                              linetitle_ + "' with " + linestyle_ + "\n");
Note: See TracChangeset for help on using the changeset viewer.