I attach a small patch that should clean up insets/figinset.C a bit.
It should not change anything visible or any internal structure,
it justs uses  tostr() and strstreams instead of sprintf
and factors out common code to some local functions.

Andre'

Index: figinset.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/figinset.C,v
retrieving revision 1.59
diff -u -r1.59 figinset.C
--- figinset.C  2000/04/19 01:42:54     1.59
+++ figinset.C  2000/04/19 17:37:45
@@ -146,13 +146,23 @@
        }
 }
 
+static string make_tmp(int pid)
+{
+       return system_tempdir + "/~lyxgs" + tostr(pid) + ".ps";
+}
+
+static void kill_gs(int pid, int sig)
+{
+       if (lyxerr.debugging()) 
+               lyxerr << "Killing gs " << pid << endl;
+       lyx::kill(pid, sig);
+       unlink(make_tmp(pid).c_str());
+}
 
 extern "C" // static
 int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                   XEvent * ev, void *)
 {
-       char tmp[128];
-
        XClientMessageEvent * e = reinterpret_cast<XClientMessageEvent*>(ev);
 
        if(lyxerr.debugging()) {
@@ -272,31 +282,13 @@
                                        }
                                }
                          noim:
-                               if (lyxerr.debugging()) {
-                                       lyxerr << "Killing gs " 
-                                              << p->gspid << endl;
-                               }
-                               lyx::kill(p->gspid, SIGHUP);
-
-                               sprintf(tmp, "%s/~lyxgs%d.ps",
-                                       system_tempdir.c_str(), 
-                                       p->gspid);
-                               unlink(tmp);
+                               kill_gs(p->gspid, SIGHUP);
                                if (forkstat == 0) {
                                        XCloseDisplay(tmpdisp);
                                        _exit(0);
                                }
                        } else {
-                               if (lyxerr.debugging()) {
-                                       lyxerr << "Killing gs " 
-                                              << p->gspid << endl;
-                               }
-                               lyx::kill(p->gspid, SIGHUP);
-
-                               sprintf(tmp, "%s/~lyxgs%d.ps", 
-                                       system_tempdir.c_str(),
-                                       p->gspid);
-                               unlink(tmp);
+                               kill_gs(p->gspid, SIGHUP);
                        }
                        break;
                }
@@ -457,12 +449,9 @@
 
        if (tmpdata->gspid > 0) {
                int pid = tmpdata->gspid;
-               char buf[128];
                // kill ghostscript and unlink it's files
                tmpdata->gspid = -1;
-               lyx::kill(pid, SIGKILL);
-               sprintf(buf, "%s/~lyxgs%d.ps", system_tempdir.c_str(), pid);
-               unlink(buf);
+               kill_gs(pid, SIGKILL);
        }
 
        if (tmpdata->bitmap) XFreePixmap(fl_display, tmpdata->bitmap);
@@ -517,15 +506,13 @@
                        return;
                }
                if (pid == 0) { // child
-                       char ** env, rbuf[80], gbuf[40];
+                       char ** env;
                        int ne = 0;
                        Display * tempdisp = XOpenDisplay(XDisplayName(0));
 
                        // create translation file
-                       sprintf(tbuf, "%s/~lyxgs%d.ps", system_tempdir.c_str(),
-                               int(getpid()));
-                       
-                       ofstream ofs(tbuf);
+                       ofstream ofs;
+                       ofs.open(make_tmp(getpid()).c_str());
                        ofs << "gsave clippath pathbbox grestore\n"
                            << "4 dict begin\n"
                            << "/ury exch def /urx exch def /lly exch def "
@@ -542,14 +529,20 @@
                        ofs.close(); // Don't remove this.
 
                        // gs process - set ghostview environment first
-                       sprintf(tbuf2, "GHOSTVIEW=%ld %ld", fl_get_canvas_id(
-                               figinset_canvas), p->data->bitmap);
+                       // // sprintf(tbuf2, "GHOSTVIEW=%ld %ld", fl_get_canvas_id(
+                       // //   figinset_canvas), p->data->bitmap);
+                       ostrstream t2(tbuf2, sizeof(tbuf));
+                       t2 << "GHOSTVIEW=" << fl_get_canvas_id(figinset_canvas)
+                                       << ' ' << p->data->bitmap << '\0';
 
                        // now set up ghostview property on a window
-                       sprintf(tbuf, "0 0 0 0 %d %d 72 72 0 0 0 0",
-                               p->data->wid, p->data->hgh);
+                       // // sprintf(tbuf, "0 0 0 0 %d %d 72 72 0 0 0 0",
+                       // //   p->data->wid, p->data->hgh);
                        // #warning BUG seems that the only bug here
                        // might be the hardcoded dpi.. Bummer!
+                       ostrstream t1(tbuf, sizeof(tbuf));
+                       t1 << "0 0 0 0 " << p->data->wid << ' '
+                               << p->data->hgh << " 72 72 0 0 0 0" << '\0';
                        
                        if (lyxerr.debugging()) {
                                lyxerr << "Will set GHOSTVIEW property to ["
@@ -610,23 +603,22 @@
                                        strlen(tbuf));
                        XUngrabServer(tempdisp);
                        XFlush(tempdisp);
-                       
+               
+                       ostrstream t3(tbuf, sizeof(tbuf));
                        switch (p->data->flags & 3) {
-                       case 0: tbuf[0] = 'H'; break; // Hidden
-                       case 1: tbuf[0] = 'M'; break; // Mono
-                       case 2: tbuf[0] = 'G'; break; // Gray
+                       case 0: t3 << 'H'; break; // Hidden
+                       case 1: t3 << 'M'; break; // Mono
+                       case 2: t3 << 'G'; break; // Gray
                        case 3:
                                if (color_visual) 
-                                       tbuf[0] = 'C'; // Color
+                                       t3 << 'C'; // Color
                                else 
-                                       tbuf[0] = 'G'; // Gray
+                                       t3 << 'G'; // Gray
                                break;
                        }
-                       
-                       sprintf(tbuf + 1, " %ld %ld",
-                               BlackPixelOfScreen(
-                               DefaultScreenOfDisplay(tempdisp)),
-                               background_pixel);
+       
+                       t3 << ' ' << 
+BlackPixelOfScreen(DefaultScreenOfDisplay(tempdisp));
+                       t3 << ' ' << background_pixel << '\0';
 
                        XGrabServer(tempdisp);
                        XChangeProperty(tempdisp, 
@@ -646,7 +638,8 @@
                        XCloseDisplay(tempdisp);
 
                        // set up environment
-                       while (environ[ne]) ++ne;
+                       while (environ[ne])
+                               ++ne;
                        typedef char * char_p;
                        env = new char_p[ne + 2];
                        env[0] = tbuf2;
@@ -657,16 +650,18 @@
                        // close(0);
                        // close(1); do NOT close. If GS writes out
                        // errors it would hang. (Matthias 290596) 
-                       sprintf(rbuf, "-r%gx%g", p->rx, p->ry);
-                       sprintf(gbuf, "-g%dx%d", p->data->wid, p->data->hgh);
+                       //sprintf(rbuf, "-r%gx%g", p->rx, p->ry);
+                       //sprintf(gbuf, "-g%dx%d", p->data->wid, p->data->hgh);
+                       string rbuf = "-r" + tostr(p->rx) + "x" + tostr(p->ry);
+                       string gbuf = "-g" + tostr(p->data->wid) + "x" + 
+tostr(p->data->hgh);
+
                        // now chdir into dir with .eps file, to be on the safe
                        // side
                        ::chdir(OnlyPath(p->data->fname).c_str());
                        // make temp file name
-                       sprintf(tbuf, "%s/~lyxgs%d.ps", system_tempdir.c_str(),
-                               int(::getpid()));
+                       string xbuf = make_tmp(getpid());
                        if (lyxerr.debugging()) {
-                               lyxerr << "starting gs " << tbuf << " "
+                               lyxerr << "starting gs " << xbuf << " "
                                       << p->data->fname
                                       << ", pid: " << getpid() << endl;
                        }
@@ -676,7 +671,7 @@
                                         "-sDEVICE=x11",
                                         "-dNOPAUSE", "-dQUIET",
                                         "-dSAFER", 
-                                        rbuf, gbuf, tbuf, 
+                                        rbuf.c_str(), gbuf.c_str(), xbuf.c_str(), 
                                         p->data->fname.c_str(), 
                                         "showpage.ps", "quit.ps", "-", 0);
                        // if we are still there, an error occurred.
@@ -685,7 +680,7 @@
                        lyxerr.debug() << "Cmd: " 
                                       << lyxrc.ps_command
                                       << " -sDEVICE=x11 "
-                                      << tbuf << ' '
+                                      << xbuf.c_str() << ' '
                                       << p->data->fname << endl;
                        _exit(0);       // no gs?
                }
@@ -832,11 +827,7 @@
                                p->broken = false;
                        } else {
                                // remove temporary files
-                               char tmp[128];
-                               sprintf(tmp, "%s/~lyxgs%d.ps", 
-                                       system_tempdir.c_str(),
-                                       p->gspid);
-                               unlink(tmp);
+                               unlink(make_tmp(p->gspid).c_str());
                                p->gspid = -1;
                                p->broken = true;
                        }
@@ -1270,6 +1261,28 @@
        return Inset::GRAPHICS_CODE;
 }
 
+static string stringify(InsetFig::HWTYPE hw, float f, string suffix)
+{
+       string res;
+       switch (hw) {
+               case InsetFig::DEF:
+                       break;
+               case InsetFig::CM:// \resizebox*{h-length}{v-length}{text}
+                       res = tostr(f) + "cm";
+                       break;
+               case InsetFig::IN: 
+                       res = tostr(f) + "in";
+                       break;
+               case InsetFig::PER_PAGE:
+                       res = tostr(f/100) + "\\text" + suffix;
+                       break;
+               case InsetFig::PER_COL:
+                       // Doesn't occur for htype...
+                       res = tostr(f/100) + "\\column" + suffix;
+                       break;
+       }
+       return res;
+}
 
 void InsetFig::Regenerate() const
 {
@@ -1288,70 +1301,9 @@
        string fname2 = MakeRelPath(fname, buf1);
 
        string gcmd = "\\includegraphics{" + fname2 + '}';
-       
-       switch (wtype) {
-       case DEF:
-               break;
-       case CM:{// \resizebox*{h-length}{v-length}{text}
-               char buf[10];
-               sprintf(buf, "%g", xwid); // should find better
-               resizeW = buf;
-               resizeW += "cm";
-               break;
-       }
-       case IN: {
-               char buf[10];
-               sprintf(buf, "%g", xwid);
-               resizeW = buf;
-               resizeW += "in";
-               break;
-       }
-       case PER_PAGE:{
-               char buf[10];
-               sprintf(buf, "%g", xwid/100);
-               resizeW = buf;
-               resizeW += "\\textwidth";
-               break;
-       }
-       case PER_COL:{
-               char buf[10];
-               sprintf(buf, "%g", xwid/100);
-               resizeW = buf;
-               resizeW += "\\columnwidth";
-               break;
-       }
-       }
+       resizeW = stringify(wtype, xwid, "width");
+       resizeH = stringify(htype, xhgh, "height");
 
-       switch (htype) {
-       case DEF:
-               break;
-       case CM: {
-               char buf[10];
-               sprintf(buf, "%g", xhgh);
-               resizeH = buf;
-               resizeH += "cm";
-               break;
-       }
-       case IN:{
-               char buf[10];
-               sprintf(buf, "%g", xhgh);
-               resizeH = buf;
-               resizeH += "in";
-               break;
-       }
-       case PER_PAGE: {
-               char buf[10];
-               sprintf(buf, "%g", xhgh/100);
-               resizeH = buf;
-               resizeH += "\\textheight";
-               break;
-       }
-       case PER_COL: {
-                // Doesn't occur; case exists to suppress compiler warnings.
-               break;
-       }
-       }
-
        if (!resizeW.empty() || !resizeH.empty()) {
                recmd = "\\resizebox*{";
                if (!resizeW.empty())
@@ -1368,12 +1320,8 @@
        
        
        if (angle != 0) {
-               char buf[10];
-               sprintf(buf, "%g", angle);
                // \rotatebox{angle}{text}
-               rotate = "\\rotatebox{";
-               rotate += buf;
-               rotate += "}{";
+               rotate = "\\rotatebox{" + tostr(angle) + "}{";
        }
 
        cmdbuf = recmd;
@@ -1406,8 +1354,8 @@
        float txhgh = atof(fl_get_input(form->Height));
 
        if (!tfname || !*tfname) {
-               cmd = "\\fbox{\\rule[-0.5in]{0pt}{1in}";
-               cmd += _("empty figure path");
+               cmd = "\\fbox{\\rule[-0.5in]{0pt}{1in}";
+               cmd += _("empty figure path");
                cmd += '}';
                return;
        }
@@ -1416,69 +1364,9 @@
        string fname2 = MakeRelPath(tfname, buf1);
        // \includegraphics*[<llx,lly>][<urx,ury>]{file}
        string gcmd = "\\includegraphics{" + fname2 + '}';
-       
-       switch (twtype) {
-       case DEF:
-               break;
-       case CM: {// \resizebox*{h-length}{v-length}{text}
-               char buf[10];
-               sprintf(buf, "%g", txwid); // should find better
-               resizeW = buf;
-               resizeW += "cm";
-               break;
-       }
-       case IN: {
-               char buf[10];
-               sprintf(buf, "%g", txwid);
-               resizeW = buf;
-               resizeW += "in";
-               break;
-       }
-       case PER_PAGE: {
-               char buf[10];
-               sprintf(buf, "%g", txwid/100);
-               resizeW = buf;
-               resizeW += "\\textwidth";
-               break;
-       }
-       case PER_COL: {
-               char buf[10];
-               sprintf(buf, "%g", txwid/100);
-               resizeW = buf;
-               resizeW += "\\columnwidth";
-               break;
-       }
-       }
 
-       switch (thtype) {
-       case DEF:
-               break;
-       case CM: {
-               char buf[10];
-               sprintf(buf, "%g", txhgh);
-               resizeH = buf;
-               resizeH += "cm";
-               break;
-       }
-       case IN: {
-               char buf[10];
-               sprintf(buf, "%g", txhgh);
-               resizeH = buf;
-               resizeH += "in";
-               break;
-       }
-       case PER_PAGE: {
-               char buf[10];
-               sprintf(buf, "%g", txhgh/100);
-               resizeH = buf;
-               resizeH += "\\textheight";
-               break;
-       }
-       case PER_COL: {
-                // Doesn't occur; case exists to suppress compiler warnings.
-               break;
-       }
-       }
+       resizeW = stringify(twtype, txwid, "width");    
+       resizeH = stringify(thtype, txhgh, "height");   
 
        // \resizebox*{h-length}{v-length}{text}
        if (!resizeW.empty() || !resizeH.empty()) {
@@ -1496,17 +1384,11 @@
        }
        
        if (tangle != 0) {
-               char buf[10];
-               sprintf(buf, "%g", tangle);
                // \rotatebox{angle}{text}
-               rotate = "\\rotatebox{";
-               rotate += buf;
-               rotate += "}{";
+               rotate = "\\rotatebox{" + tostr(tangle) + "}{";
        }
 
-       cmdbuf = recmd;
-       cmdbuf += rotate;
-       cmdbuf += gcmd;
+       cmdbuf = recmd + rotate + gcmd;
        if (!rotate.empty()) cmdbuf += '}';
        if (!recmd.empty()) cmdbuf += '}';
        if (psubfigure && !tsubcap.empty()) {
@@ -1944,8 +1826,6 @@
 
 void InsetFig::RestoreForm()
 {
-       char buf[32];
-
        EnableFigurePanel(form);
 
        twtype = wtype;
@@ -1983,12 +1863,9 @@
        fl_set_button(form->Subfigure, (subfigure != 0));
        pflags = flags;
        psubfigure = subfigure;
-       sprintf(buf, "%g", xwid);
-       fl_set_input(form->Width, buf);
-       sprintf(buf, "%g", xhgh);
-       fl_set_input(form->Height, buf);
-       sprintf(buf, "%g", angle);
-       fl_set_input(form->Angle, buf);
+       fl_set_input(form->Width, tostr(xwid).c_str());
+       fl_set_input(form->Height, tostr(xhgh).c_str());
+       fl_set_input(form->Angle, tostr(angle).c_str());
        if (!fname.empty()){
                string buf1 = OnlyPath(owner->fileName());
                string fname2 = MakeRelPath(fname, buf1);

Reply via email to