]> git.lyx.org Git - lyx.git/blobdiff - src/insets/figinset.C
pos=string::npos for regex not found, use handcoded transform in lstring.C, fix the...
[lyx.git] / src / insets / figinset.C
index dcc77e153235a8a314d93517a3ef7613e5f5b833..549c766d35b25b6a0d8325f4a3c3d77e54c96cd0 100644 (file)
@@ -40,7 +40,15 @@ extern long int background_pixels;
 #include <cctype>
 #include <cmath>
 #include <fstream>
+#include <queue>
+#include <list>
+#include <algorithm>
 using std::ofstream;
+using std::ifstream;
+using std::queue;
+using std::list;
+using std::find;
+using std::flush;
 
 #include "figinset.h"
 #include "lyx.h"
@@ -56,6 +64,7 @@ using std::ofstream;
 #include "gettext.h"
 #include "lyx_gui_misc.h" // CancelCloseBoxCB
 #include "support/FileInfo.h"
+#include "support/lyxlib.h"
 
 extern BufferView * current_view;
 #if 0
@@ -85,23 +94,19 @@ static int figarrsize = 0;  /* current max number of figures */
 static int bmpinsref = 0;      /* number of bitmaps */
 static int bmparrsize = 0;     /* current max number of bitmaps */
 
-struct queue {
-       float rx, ry;           /* resolution x and y */
-       int ofsx, ofsy;         /* x and y translation */
-       figdata * data;         /* we are doing it for this data */
-       queue * next;           /* next item in queue */
-};
-
-struct pidwait {
-       int pid;                /* pid to wait for */
-       pidwait * next; /* next */
+struct queue_element {
+       float rx, ry;          // resolution x and y
+       int ofsx, ofsy;        // x and y translation
+       figdata * data;        // we are doing it for this data
 };
 
 static int const MAXGS = 3;                    /* maximum 3 gs's at a time */
 
 static Figref ** figures;      /* all the figures */
 static figdata ** bitmaps;     /* all the bitmaps */
-static queue * gsqueue = 0;    /* queue for ghostscripting */
+
+static queue<queue_element> gsqueue; // queue for ghostscripting
+
 static int gsrunning = 0;      /* currently so many gs's are running */
 static bool bitmap_waiting = false; /* bitmaps are waiting finished */
 static char bittable[256];     /* bit reversion table */
@@ -115,26 +120,22 @@ static int gs_spc;                        // shades per color
 static bool gs_gray;                   // is grayscale?
 static int gs_allcolors;               // number of all colors
 
-static pidwait * pw = 0;               // pid wait list
-
+static list<int> pidwaitlist; // pid wait list
 
 extern Colormap color_map;
 
 void addpidwait(int pid)
 {
        // adds pid to pid wait list
-       register pidwait * p = new pidwait;
-
-       p->pid = pid;
-       p->next = pw;
-       pw = p;
+       pidwaitlist.push_back(pid);
 
        if (lyxerr.debugging()) {
-               lyxerr << "Pids to wait for: " << p->pid << endl;
-               while (p->next) {
-                       p = p->next;
-                       lyxerr << p->pid << endl;
+               lyxerr << "Pids to wait for: \n";
+               for (list<int>::const_iterator cit = pidwaitlist.begin();
+                    cit != pidwaitlist.end(); ++cit) {
+                       lyxerr << (*cit) << '\n';
                }
+               lyxerr << flush;
        }
 }
 
@@ -193,15 +194,15 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                                << "Cannot fork, using slow "
                                                "method for pixmap translation." << endl;
                                        tmpdisp = fl_display;
-                               } else if (forkstat > 0) {
+                               } else if (forkstat > 0) { // parent
                                        // register child
                                        if (lyxerr.debugging()) {
                                                lyxerr << "Spawned child "
                                                       << forkstat << endl;
                                        }
                                        addpidwait(forkstat);
-                                       break; // in parent process
-                               } else {
+                                       break;
+                               } else {  // child
                                        tmpdisp = XOpenDisplay(XDisplayName(0));
                                        XFlush(tmpdisp);
                                }
@@ -219,7 +220,6 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                }
                                {
                                // query current colormap
-                               //cmap = (XColor *) malloc(gs_allcolors*sizeof(XColor));
                                        XColor * cmap = new XColor[gs_allcolors];
                                        for (i = 0; i < gs_allcolors; ++i) cmap[i].pixel = i;
                                        XQueryColors(tmpdisp, color_map, cmap, gs_allcolors);
@@ -259,7 +259,7 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                        lyxerr << "Killing gs " 
                                               << p->gspid << endl;
                                }
-                               kill(p->gspid, SIGHUP);
+                               lyx::kill(p->gspid, SIGHUP);
 
                                sprintf(tmp, "%s/~lyxgs%d.ps",
                                        system_tempdir.c_str(), 
@@ -274,7 +274,7 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                        lyxerr << "Killing gs " 
                                               << p->gspid << endl;
                                }
-                               kill(p->gspid, SIGHUP);
+                               lyx::kill(p->gspid, SIGHUP);
 
                                sprintf(tmp, "%s/~lyxgs%d.ps", 
                                        system_tempdir.c_str(),
@@ -366,10 +366,14 @@ void AllocGrays(int num)
 void InitFigures()
 {
        bmparrsize = figarrsize = figallocchunk;
-       figures = static_cast<Figref**>
-               (malloc(sizeof(Figref*) * figallocchunk));
-       bitmaps = static_cast<figdata**>
-               (malloc(sizeof(figdata*) * figallocchunk));
+       typedef Figref * Figref_p;
+       figures = new Figref_p[figallocchunk];
+       //figures = static_cast<Figref**>
+       //      (malloc(sizeof(Figref*) * figallocchunk));
+       typedef figdata * figdata_p;
+       bitmaps = new figdata_p[figallocchunk];
+       //bitmaps = static_cast<figdata**>
+       //(malloc(sizeof(figdata*) * figallocchunk));
 
        unsigned int k;
        for (unsigned int i = 0; i < 256; ++i) {
@@ -416,8 +420,10 @@ void InitFigures()
 
 void DoneFigures()
 {
-       free(figures);
-       free(bitmaps);
+       //free(figures);
+       //free(bitmaps);
+       delete[] figures;
+       delete[] bitmaps;
        figarrsize = 0;
        bmparrsize = 0;
 
@@ -471,7 +477,7 @@ static void freefigdata(figdata * tmpdata)
                chpixmap(tmpdata->bitmap, tmpdata->wid, tmpdata->hgh);
                // kill ghostscript and unlink it's files
                tmpdata->gspid = -1;
-               kill(pid, SIGKILL);
+               lyx::kill(pid, SIGKILL);
                sprintf(buf, "%s/~lyxgs%d.ps", system_tempdir.c_str(), pid);
                unlink(buf);
        }
@@ -502,7 +508,7 @@ static void runqueue()
                Atom * prop;
                int nprop, i;
 
-               if (!gsqueue) {
+               if (gsqueue.empty()) {
                        if (!gsrunning && gs_xcolor) {
                                // de-allocate rest of colors
                                // *****
@@ -510,10 +516,9 @@ static void runqueue()
                        }
                        return;
                }
-               queue * p = gsqueue;
-
+               queue_element * p = &gsqueue.front();
                if (!p->data) {
-                       delete p;
+                       gsqueue.pop();
                        continue;
                }
 
@@ -665,8 +670,10 @@ static void runqueue()
 
                        // set up environment
                        while (environ[ne]) ++ne;
-                       env = static_cast<char **>
-                               (malloc(sizeof(char*) * (ne + 2)));
+                       typedef char * char_p;
+                       env = new char_p[ne + 2];
+                       //env = static_cast<char **>
+                       //      (malloc(sizeof(char*) * (ne + 2)));
                        env[0] = tbuf2;
                        memcpy(&env[1], environ, sizeof(char*) * (ne + 1));
                        environ = env;
@@ -711,10 +718,10 @@ static void runqueue()
                if (lyxerr.debugging()) {
                        lyxerr << "GS ["  << pid << "] started" << endl;
                }
-               gsqueue = gsqueue->next;
-               gsrunning++;
+
                p->data->gspid = pid;
-               delete p;
+               ++gsrunning;
+               gsqueue.pop();
        }
 }
 
@@ -722,22 +729,15 @@ static void runqueue()
 static void addwait(int psx, int psy, int pswid, int pshgh, figdata * data)
 {
        // recompute the stuff and put in the queue
-       queue * p = new queue;
-       p->ofsx = psx;
-       p->ofsy = psy;
-       p->rx = (float(data->raw_wid) * 72.0) / pswid;
-       p->ry = (float(data->raw_hgh) * 72.0) / pshgh;
+       queue_element p;
+       p.ofsx = psx;
+       p.ofsy = psy;
+       p.rx = (float(data->raw_wid) * 72.0) / pswid;
+       p.ry = (float(data->raw_hgh) * 72.0) / pshgh;
 
-       p->data = data;
-       p->next = 0;
+       p.data = data;
 
-       // now put into queue
-       queue * p2 = gsqueue;
-       if (!gsqueue) gsqueue = p;
-       else {
-               while (p2->next) p2 = p2->next;
-               p2->next = p;
-       }
+       gsqueue.push(p);
 
        // if possible, run the queue
        runqueue();
@@ -767,11 +767,14 @@ static figdata * getfigdata(int wid, int hgh, string const & fname,
        if (bmpinsref > bmparrsize) {
                // allocate more space
                bmparrsize += figallocchunk;
-               figdata ** tmp = static_cast<figdata**>
-                       (malloc(sizeof(figdata*) * bmparrsize));
+               typedef figdata * figdata_p;
+               figdata ** tmp = new figdata_p[bmparrsize];
+               //figdata ** tmp = static_cast<figdata**>
+               //      (malloc(sizeof(figdata*) * bmparrsize));
                memcpy(tmp, bitmaps,
                       sizeof(figdata*) * (bmparrsize - figallocchunk));
-               free(bitmaps);
+               delete[] bitmaps;
+               //free(bitmaps);
                bitmaps = tmp;
        }
        figdata * p = new figdata;
@@ -873,30 +876,22 @@ void sigchldchecker(pid_t pid, int * status)
                                p->broken = true;
                        }
                        makeupdatelist(bitmaps[i]);
-                       gsrunning--;
+                       --gsrunning;
                        runqueue();
                        pid_handled = true;
                }
        }
        if (!pid_handled) {
                lyxerr.debug() << "Checking pid in pidwait" << endl;
-               pidwait * p = pw, * prev = 0;
-               while (p) {
-                       if (pid == p->pid) {
-                               lyxerr.debug() << "Found pid in pidwait" << endl;
-                               lyxerr.debug() << "Caught child pid of recompute routine " << pid << endl;
-                               if (prev)
-                                       prev->next = p->next;
-                               else
-                                       pw = p->next;
-                               delete p;
-                               break;
-                       }
-                       prev = p;
-                       p = p->next;
+               list<int>::iterator it = find(pidwaitlist.begin(),
+                                             pidwaitlist.end(), pid);
+               if (it != pidwaitlist.end()) {
+                       lyxerr.debug() << "Found pid in pidwait\n"
+                                      << "Caught child pid of recompute "
+                               "routine" << pid << endl;
+                       pidwaitlist.erase(it);
                }
        }
-
        if (pid == -1) {
                lyxerr.debug() << "waitpid error" << endl;
                switch (errno) {
@@ -936,7 +931,7 @@ static void getbitmaps()
 }
 
 
-static void RegisterFigure(InsetFig *fi)
+static void RegisterFigure(InsetFig * fi)
 {
        if (figinsref == 0) InitFigures();
        fi->form = 0;
@@ -944,11 +939,14 @@ static void RegisterFigure(InsetFig *fi)
        if (figinsref > figarrsize) {
                // allocate more space
                figarrsize += figallocchunk;
-               Figref ** tmp = static_cast<Figref**>
-                       (malloc(sizeof(Figref*)*figarrsize));
+               typedef Figref * Figref_p;
+               Figref ** tmp = new Figref_p[figarrsize];
+               //Figref ** tmp = static_cast<Figref**>
+               //      (malloc(sizeof(Figref*)*figarrsize));
                memcpy(tmp, figures,
                       sizeof(Figref*)*(figarrsize-figallocchunk));
-               free(figures);
+               delete[] figures;
+               //free(figures);
                figures = tmp;
        }
        Figref * tmpfig = new Figref;
@@ -990,7 +988,7 @@ static void UnregisterFigure(InsetFig * fi)
 #warning Reactivate this free_form calls
 #else
                fl_free_form(tmpfig->inset->form->Figure);
-               free(tmpfig->inset->form);
+               free(tmpfig->inset->form); // Why free?
                tmpfig->inset->form = 0;
 #endif
        }
@@ -1924,7 +1922,7 @@ void InsetFig::CallbackFig(long arg)
 #warning Reactivate this free_form calls
 #else
                                fl_free_form(form->Figure);
-                               free(form);
+                               free(form); // Why free?
                                form = 0;
 #endif
                        }
@@ -1942,7 +1940,7 @@ void InsetFig::CallbackFig(long arg)
 #warning Jug, is this still a problem?
 #else
                fl_free_form(form->Figure);
-               free(form);
+               free(form); // Why free?
                form = 0;
 #endif
                break;