home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / TXI2IPF1 / SOURCE.ZIP / util.cc < prev   
Encoding:
C/C++ Source or Header  |  1993-02-11  |  9.2 KB  |  441 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // $Id: util.cc,v 1.7 1993/02/10 21:25:56 ak Exp $
  3. ///////////////////////////////////////////////////////////////////////////////
  4. // $Log: util.cc,v $
  5. // Revision 1.7  1993/02/10  21:25:56  ak
  6. // *** empty log message ***
  7. //
  8. // Revision 1.6  1993/02/10  20:58:24  ak
  9. // Pass menues.
  10. //
  11. // Revision 1.5  1993/02/06  00:09:58  ak
  12. // *** empty log message ***
  13. //
  14. // Revision 1.4  1993/02/05  23:44:11  ak
  15. // Bugfixes and consmetic changes.
  16. //
  17. // Revision 1.3  1993/02/04  17:51:22  ak
  18. // *** empty log message ***
  19. //
  20. // Revision 1.2  1993/02/04  15:45:58  ak
  21. // *** empty log message ***
  22. //
  23. // Revision 1.1.1.1  1993/02/04  15:22:01  ak
  24. // Texinfo -> OS/2 IPF/INF converter.
  25. //
  26. // Revision 1.1  1993/02/04  15:21:58  ak
  27. // Initial revision
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////
  30.  
  31. static char *rcsid = "$Id: util.cc,v 1.7 1993/02/10 21:25:56 ak Exp $";
  32.  
  33. #include "info.h"
  34. #include <ctype.h>
  35. #include <alloca.h>
  36.  
  37. static char *    old_font = "default";
  38. static int    preface  = 1;
  39.  
  40. ////////////////////////////////////////////////////////////////////////
  41.  
  42. static const char *fstack[10] = {"default"};
  43. static int       fstackx    = 0;
  44.  
  45. void
  46. font(const char *name, const char *size)
  47. {
  48.     if (!name)
  49.         name = fstack[fstackx];
  50.     if (strcmp(name, "default") == 0) {
  51.         PUTA(":font facename=default size=0x0.");
  52.     } else
  53.         PUTA(":font facename=" << name << " size=" << size << '.');
  54. }
  55.  
  56. void
  57. beg_example(const char *fontname)
  58. {
  59.     newpara = 0;
  60.     ++nofill;
  61.     *out << ":lines align=left.";
  62.     margin(+10);
  63.     fstack[++fstackx] = fontname;
  64.     font(0);
  65.     PUTL();
  66.     newpara = 0;
  67. }
  68.  
  69. void
  70. end_example()
  71. {
  72.     newpara = 0;
  73.     --nofill;
  74.     --fstackx;
  75.     font(0);
  76.     margin(-10);
  77.     *out << ":elines.";
  78.     PUTL();
  79.     newpara = 0;
  80. }
  81.  
  82. ////////////////////////////////////////////////////////////////////////
  83.  
  84. void
  85. words(String& line)
  86. {
  87.     int i = 0;
  88.     for (const char *p1 = line.chars(), *end = p1 + line.length();;) {
  89.         while (p1 < end && isspace(*p1))
  90.             ++p1;
  91.         if (p1 == end)
  92.             break;
  93.         const char *p2 = p1;
  94.         while (p2 < end && *p2 != ',')
  95.             ++p2;
  96.         if (i < MaxW)
  97.             w[i++] = String(p1, p2 - p1);
  98.         if (p2 == end)
  99.             break;
  100.         p1 = p2 + 1;
  101.     }
  102.     while (i < MaxW)
  103.         w[i++] = null;
  104. }
  105.  
  106. ////////////////////////////////////////////////////////////////////////
  107.  
  108. static int    hstack[6];
  109. static int    hlevel = 0;
  110.  
  111. void
  112. heading(int level, char *s)
  113. {
  114.     if (istackp != istack)
  115.         return;
  116.     if (f_node) {
  117.         if (preface)
  118.             PUT(":h2." << s << endl << ":lm margin=1.");
  119.     } else {
  120.         if (debug)
  121.             cerr << "Heading, logical level=" << level
  122.                  << " stack level=" << hlevel
  123.                  << " stacked=" << hstack[hlevel];
  124.         if (level > hstack[hlevel])
  125.             hstack[++hlevel] = level;
  126.         else while (level < hstack[hlevel])
  127.             --hlevel;
  128.         if (debug)
  129.             cerr << " panel level=" << hlevel << endl;
  130.         if (node_id) {
  131.             panel_id = node_id;
  132.             node_id = 0;
  133.         } else
  134.             panel_id = ++hdid;
  135.         panel_name = s;
  136.         PUT(":h" << hlevel << " id=hd" << panel_id << '.' << s
  137.              << endl << ":lm margin=1.");
  138.     }
  139. }
  140.  
  141.  
  142. void
  143. Menu::flush()
  144. {
  145.     if (line.length() == 0)
  146.         return;
  147.     int x1 = line.index("&colon.");
  148.     if (x1 >= 0) {
  149.         int x2 = line.index("&colon.", x1+7);
  150.         if (x2 >= 0) {
  151.             String name = line.at(x1+7, x2-x1-7);
  152.             if (name.length() == 0)
  153.                 name = line.before(x1);
  154.             if (!table.contains(name))
  155.                 table[name] = Name(++hdid);
  156.             for (x2 += 7; x2 < line.length() && isspace(line[x2]); ++x2)
  157.                 ;
  158.             *out << ":pt.:link reftype=hd refid=hd" << table[name].id
  159.                  << '.' << line.before(x1) << ":elink.:pd."
  160.                  << line.from(x2) << endl;
  161.         }
  162.     }
  163.     line = "";
  164. }
  165.  
  166. void
  167. node(String name, String next, String prev, String up)
  168. {
  169.     if (table.contains(up)) {
  170.         if (f_node)
  171.             hlevel = table[up].level + 1;
  172.     } else if (name == "Top" || up.length() == 0) {
  173.         if (f_node)
  174.             hlevel = 0;
  175.     } else {
  176.         cerr << "<up> node undefined in '" << name << "', '" << next
  177.             << "', '" << prev << "', '" << up << '\'' << endl;
  178.         cerr.flush();
  179.         exit(1);
  180.     }
  181.     if (debug)
  182.         cerr << "Define name='" << name << "' next='" << next
  183.              << "' prev='" << prev << "' up='" << up << "'";
  184.     Name& entry = table[name];
  185.     if (entry.id) {
  186.         if (entry.defined) {
  187.             cerr << "Node '" << name << "' redefined " << endl;
  188.             cerr.flush();
  189.             exit(1);
  190.         }
  191.         entry.defined = 1;
  192.         if (debug)
  193.             cerr << ", known id=" << entry.id << endl;
  194.     } else {
  195.         table[name] = Name(++hdid, 1, hlevel);
  196.         if (debug)
  197.             cerr << ", new id=" << hdid << endl;
  198.     }
  199.     if (f_node && hlevel >= 1 && hlevel <= 6 && istackp == istack) {
  200.         preface = 0;
  201.         PUT(":h" << hlevel << " id=hd" << entry.id << '.' << name
  202.              << endl << ":lm margin=1.");
  203.     }
  204.     node_name = name;
  205.     node_id = entry.id;
  206. }
  207.  
  208. void
  209. xref(char *see, String name, String entry, String topic, String file, String manual)
  210. {
  211.     if (debug)
  212.         cerr << "Refer to name='" << name << "' entry='" << entry
  213.              << "' topic='" << topic << "' file='" << file
  214.              << "' manual='" << manual << "'";
  215.     if (see)
  216.         PUTS(see);
  217.     if (file.empty()) {
  218.         if (!table.contains(name)) {
  219.             table[name] = Name(++hdid);
  220.             if (debug)
  221.                 cerr << " new id=" << hdid << endl;
  222.         } else
  223.             if (debug)
  224.                 cerr << " known id=" << table[name].id << endl;
  225.         PUTS(":link reftype=hd refid=hd" << table[name].id << '.');
  226.         if (topic.length()) {
  227.             PUT(topic);
  228.         } else
  229.             PUT(name);
  230.         if (entry.length())
  231.             PUT(": " << entry);
  232.         PUT(":elink.");
  233.     } else
  234.         PUTS("Section " << topic << " of :hp1." << manual << ":ehp1.");
  235. }
  236.  
  237. ////////////////////////////////////////////////////////////////////////
  238.  
  239. static int current_margin = 0;
  240.  
  241. static void
  242. set_margin(int indent)
  243. {
  244.     current_margin = indent;
  245.     PUTA(":lm margin=" << (1 + current_margin) << '.');
  246. }
  247.  
  248.     // > 0: indent
  249.     // < 0: extend
  250.     // = 0: reset
  251. int
  252. margin(int delta)
  253. {
  254.     int old = current_margin;
  255.     if (delta) {
  256.         current_margin += delta;
  257.         if (current_margin < 0)
  258.             current_margin = 0;
  259.     } else
  260.         current_margin = 0;
  261.     PUTA(":lm margin=" << (1 + current_margin) << '.');
  262.     return old;
  263. }
  264.  
  265. void
  266. item_begin(const char *text, int it, char *ic)
  267. {
  268.     if (istackp == istack) {
  269.         istackp->indent = current_margin;
  270.         istackp->nested = 1;
  271.     }
  272.     ++istackp;
  273.     istackp->type = it;
  274.     istackp->cat = ic;
  275.     istackp->beg = Item();
  276.     istackp->end = Item();
  277.     istackp->indent = current_margin;
  278.     switch (it) {
  279.     case 'o':
  280.         istackp->indent += 3;
  281.         break;
  282.     case 'u':
  283.         istackp->indent += 5;
  284.         break;
  285.     case 'p':
  286.         istackp->indent += 12;
  287.         break;
  288.     }
  289.     istackp->nested = 0;
  290.     istackp->count = 0;
  291.     for (ItemType *p = istack; p < istackp; ++p)
  292.         if (p->type == 'p')
  293.             ++istackp->nested;
  294.     PUTL();
  295.     if (!istackp->nested)
  296.         PUT(text);
  297.     newpara = 0;
  298. }
  299.  
  300. void
  301. ItemType::item(const char *text)
  302. {
  303.     newpara = 0;
  304.  
  305.     if (nested) {
  306.         set_margin((istackp-1)->indent);
  307.         *out << ":p.";
  308.         switch (type) {
  309.         case 'o':
  310.             margin(+1);
  311.             *out << ".&larrow.";
  312.             margin(+2);
  313.             break;
  314.         case 'u':
  315.             margin(+2);
  316.             *out << ++count << '.';
  317.             margin(+3);
  318.             break;
  319.         }
  320.     } else if (type == 'p')
  321.         *out << ":pt.";
  322.     else
  323.         *out << ":li.";
  324.  
  325.     if (text && *text) {
  326.         if (beg.fontname)
  327.             *out << ":font facename=" << beg.fontname
  328.                  << " size=" FontSize ".";
  329.         if (beg.hpnumber)
  330.             *out << ":hp" << beg.hpnumber << '.';
  331.         if (beg.delimiter)
  332.             *out << beg.delimiter;
  333.  
  334.         *out << text;
  335.  
  336.         if (end.delimiter)
  337.             *out << end.delimiter;
  338.         if (end.hpnumber)
  339.             *out << ":ehp" << end.hpnumber << '.';
  340.         if (end.fontname)
  341.             *out << ":font facename=" << end.fontname
  342.                  << " size=" FontSize ".";
  343.     }
  344.  
  345.     if (type == 'p') {
  346.         if (nested)
  347.             margin(+12);
  348.         else
  349.             *out << ":pd.";
  350.     }
  351. }
  352.  
  353. void
  354. item_end(const char *text)
  355. {
  356.     PUTL();
  357.     if (istackp->nested)
  358.         newpara = 1;
  359.     else {
  360.         PUT(text);
  361.         newpara = 0;
  362.     }
  363.     --istackp;
  364.     set_margin(istackp->indent);
  365. }
  366.  
  367. ////////////////////////////////////////////////////////////////////////
  368.  
  369. void
  370. define(String& type, String& name, String *args, String& category)
  371. {
  372.     PUT("!!define "
  373.      << " type=" << type
  374.      << " name=" << name
  375.      << " category=" << category);
  376.     if (args) {
  377.         PUT(" args=");
  378.         for (int i = 0; args[i] != null; ++i)
  379.             PUT(args[i]);
  380.     }
  381.     PUT("!!" << endl);
  382. }
  383.  
  384. ////////////////////////////////////////////////////////////////////////
  385.  
  386. void
  387. printindex(String& classname)
  388. {
  389.     XClassT& xc = indexes[classname].entries;
  390.     PUT(":parml tsize=50 break=none compact." << endl);
  391.     for (Pix entry = xc.first(); entry; xc.next(entry)) {
  392.         PUT(":pt." << xc.key(entry) << endl << ":pd.");
  393.         XEntry& xe = xc.contents(entry);
  394.         int ne = 0;
  395.         for (Pix item = xe.first(); item; xe.next(item)) {
  396.             if (ne++)
  397.                 PUT(".br" << endl);
  398.             XItem& xi = xe(item);
  399.             PUT(":link reftype=hd refid=hd" << xi.id << '.'
  400.              << xi.title << ":elink." << endl);
  401.         }
  402.     }
  403.     PUT(":eparml.");
  404.     PUTL();
  405. }
  406.  
  407. ////////////////////////////////////////////////////////////////////////
  408.  
  409. void
  410. single_word(char *text)
  411. {
  412.     int ipf = 0;
  413.     while (*text) {
  414.         char c = *text++;
  415.         switch (c) {
  416.         case ' ':
  417.             if (ipf)
  418.                 break;
  419.             PUTS("&rbl.");
  420.             continue;
  421.         case ':':
  422.         case '&':
  423.             ipf = 1;
  424.             break;
  425.         }
  426.         PUTS(c);
  427.     }
  428. }
  429.  
  430. ////////////////////////////////////////////////////////////////////////
  431.  
  432. void
  433. para()
  434. {
  435.     if (!plevel) {
  436.         *out << ":p.";
  437.         newpara = 0;
  438.     }
  439. }
  440.  
  441.