home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / misc / math / MCalc17.lha / MCalc / Source / MCalcARexx.c < prev    next >
C/C++ Source or Header  |  1995-11-29  |  20KB  |  830 lines

  1. /*
  2. Auto:        smake MCalc
  3. */
  4.  
  5. /* $Revision Header built automatically *************** (do not edit) ************
  6. **
  7. ** © Copyright by GuntherSoft
  8. **
  9. ** File             : SnakeSYS:CPrgs/MUICalc/MCalcARexx.c
  10. ** Created on       : Sunday, 05.12.93 14:37:01
  11. ** Created by       : Kai Iske
  12. ** Current revision : V1.0
  13. **
  14. **
  15. ** Purpose
  16. ** -------
  17. **   - ARexx support for MUIProCalc
  18. **
  19. ** Revision V1.0
  20. ** --------------
  21. ** created on Sunday, 05.12.93 14:37:01  by  Kai Iske.   LogMessage :
  22. **     --- Initial release ---
  23. **
  24. *********************************************************************************/
  25.  
  26.  
  27.  
  28.  
  29.  
  30. /**********************************************************************/
  31. /*                      Routines for that module                      */
  32. /**********************************************************************/
  33. static APTR __saveds __asm DoRXCalc(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms);
  34. static APTR __saveds __asm DoRXCalcTeX(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms);
  35. static APTR __saveds __asm GetOutput(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms);
  36. static APTR __saveds __asm GetTeXOutput(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms);
  37. static APTR __saveds __asm GetInput(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms);
  38. static APTR __saveds __asm GetTeXInput(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms);
  39. static APTR __saveds __asm DoFormatTeX(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms);
  40. static APTR __saveds __asm SetMode(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms);
  41. static APTR __saveds __asm DeleteHistoryFunc(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms);
  42. static APTR ReturnEntry(UWORD Mode, UWORD OutMode, LONG Pos);
  43. static void FormatTeX(char *Source, char *Dest);
  44.  
  45.  
  46. /**********************************************************************/
  47. /*           external references to main programs variables           */
  48. /**********************************************************************/
  49. extern APTR    AppObject;
  50. extern APTR    InputString;
  51. extern APTR    OutputBox;
  52. extern UWORD    IntType;
  53. extern UWORD    IntBase;
  54. extern UWORD    IntSign;
  55. extern UWORD    IntAngle;
  56.  
  57.  
  58.  
  59.  
  60. /**********************************************************************/
  61. /*                               Hooks                                */
  62. /**********************************************************************/
  63. static struct Hook CalcHook =
  64. {
  65.     {NULL},
  66.     (APTR)DoRXCalc,
  67.     NULL,
  68.     NULL
  69. };
  70.  
  71. static struct Hook CalcTeXHook =
  72. {
  73.     {NULL},
  74.     (APTR)DoRXCalcTeX,
  75.     NULL,
  76.     NULL
  77. };
  78.  
  79. static struct Hook GetOutputHook =
  80. {
  81.     {NULL},
  82.     (APTR)GetOutput,
  83.     NULL,
  84.     NULL
  85. };
  86.  
  87. static struct Hook GetTeXOutputHook =
  88. {
  89.     {NULL},
  90.     (APTR)GetTeXOutput,
  91.     NULL,
  92.     NULL
  93. };
  94.  
  95. static struct Hook GetInputHook =
  96. {
  97.     {NULL},
  98.     (APTR)GetInput,
  99.     NULL,
  100.     NULL
  101. };
  102.  
  103. static struct Hook GetTeXInputHook =
  104. {
  105.     {NULL},
  106.     (APTR)GetTeXInput,
  107.     NULL,
  108.     NULL
  109. };
  110.  
  111. static struct Hook FormatTeXHook =
  112. {
  113.     {NULL},
  114.     (APTR)DoFormatTeX,
  115.     NULL,
  116.     NULL
  117. };
  118.  
  119. static struct Hook SetModeHook =
  120. {
  121.     {NULL},
  122.     (APTR)SetMode,
  123.     NULL,
  124.     NULL
  125. };
  126.  
  127. static struct Hook DeleteHistoryHook =
  128. {
  129.     {NULL},
  130.     (APTR)DeleteHistoryFunc,
  131.     NULL,
  132.     NULL
  133. };
  134.  
  135.  
  136.  
  137.  
  138. /**********************************************************************/
  139. /*                       This is for our modes                        */
  140. /**********************************************************************/
  141. UWORD    RXIntType;
  142. UWORD    RXIntBase;
  143. UWORD    RXIntSign;
  144. UWORD    RXIntAngle;
  145.  
  146.  
  147.  
  148.  
  149. /**********************************************************************/
  150. /*               Array of ARexx commands for MUIProCalc               */
  151. /**********************************************************************/
  152. struct    MUI_Command    RXCommands[] =
  153. {
  154.         // Calculate an expression (normal output)
  155.  
  156.     {
  157.         "CALC",
  158.         "EXPRESSION/A/M",
  159.         1,
  160.         &CalcHook
  161.     },
  162.  
  163.         // Calculate an expression (TeX output)
  164.  
  165.     {
  166.         "CALCTEX",
  167.         "EXPRESSION/A/M",
  168.         1,
  169.         &CalcTeXHook
  170.     },
  171.  
  172.         // Select output from history (normal output; as is)
  173.  
  174.     {
  175.         "GETOUTPUT",
  176.         "ENTRYNUM/N",
  177.         1,
  178.         &GetOutputHook
  179.     },
  180.  
  181.         // Select output from history (TeX output)
  182.  
  183.     {
  184.         "GETTEXOUTPUT",
  185.         "ENTRYNUM/N",
  186.         1,
  187.         &GetTeXOutputHook
  188.     },
  189.  
  190.         // Select input from history (normal output; as is)
  191.  
  192.     {
  193.         "GETINPUT",
  194.         "ENTRYNUM/N",
  195.         1,
  196.         &GetInputHook
  197.     },
  198.  
  199.         // Select input from history (TeX output)
  200.  
  201.     {
  202.         "GETTEXINPUT",
  203.         "ENTRYNUM/N",
  204.         1,
  205.         &GetTeXInputHook
  206.     },
  207.  
  208.  
  209.         // Format an expression to TeX format
  210.  
  211.     {
  212.         "FORMATTEX",
  213.         "EXPRESSION/A/M",
  214.         1,
  215.         &FormatTeXHook
  216.     },
  217.  
  218.  
  219.         // Set output mode
  220.  
  221.     {
  222.         "SETMODE",
  223.         "BASE/K,SIZE/K,SIGN/K,ANGLE/K",
  224.         4,
  225.         &SetModeHook
  226.     },
  227.  
  228.         // Delete an entry from history
  229.  
  230.     {
  231.         "DELETEHISTORY",
  232.         "ENTRYNUM/N",
  233.         1,
  234.         &DeleteHistoryHook
  235.     },
  236.  
  237.  
  238.  
  239.         // Delimiter
  240.  
  241.     {
  242.         NULL, NULL, NULL, NULL
  243.     }
  244. };
  245.  
  246.  
  247.  
  248.  
  249.  
  250. /**********************************************************************/
  251. /*        Let MUIProCalc (normal output) calculate through ARexx      */
  252. /*        ------------------------------------------------------      */
  253. /* Parameters must be :                                               */
  254. /* Expr  = Expression to calculate                                    */
  255. /**********************************************************************/
  256. static APTR __saveds __asm DoRXCalc(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms)
  257. {
  258.     UWORD    BackIntType, BackIntBase, BackIntSign, BackIntAngle;
  259.     char    NewBuff[258], **Exprs;
  260.     BOOL    GoOn = TRUE;
  261.  
  262.         // Build expression from partial input
  263.  
  264.     strcpy(NewBuff, "");
  265.     Exprs = (char **)Parms[0];
  266.     while(*Exprs && GoOn)
  267.     {
  268.         if((strlen(*Exprs) + strlen(NewBuff)) < 256)
  269.             strcat(NewBuff, *Exprs);
  270.         else
  271.             GoOn = FALSE;
  272.  
  273.         Exprs++;
  274.     }
  275.  
  276.         // Set input string to current expression
  277.  
  278.     set(InputString, MUIA_String_Contents, NewBuff);
  279.  
  280.         // Calculate expression (with ARexx settings)
  281.  
  282.     BackIntType    = IntType;
  283.     BackIntBase    = IntBase;
  284.     BackIntSign    = IntSign;
  285.     BackIntAngle    = IntAngle;
  286.  
  287.     IntType        = RXIntType;
  288.     IntBase        = RXIntBase;
  289.     IntSign        = RXIntSign;
  290.     IntAngle    = RXIntAngle;
  291.  
  292.     GoOn = FormatOutput(FALSE);
  293.  
  294.     IntType        = BackIntType;
  295.     IntBase        = BackIntBase;
  296.     IntSign        = BackIntSign;
  297.     IntAngle    = BackIntAngle;
  298.  
  299.     if(GoOn)
  300.     {
  301.  
  302.             // Return output
  303.  
  304.         return(ReturnEntry(GET_OUTPUT, NORMAL_OUTPUT, GET_ACTIVE));
  305.     }
  306.     else
  307.         return((APTR)set(AppObject, MUIA_Application_RexxString,  " "));
  308. }
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315. /**********************************************************************/
  316. /*         Let MUIProCalc (TeX output) calculate through ARexx        */
  317. /*         ---------------------------------------------------        */
  318. /* Parameters must be :                                               */
  319. /* Expr  = Expression to calculate                                    */
  320. /**********************************************************************/
  321. static APTR __saveds __asm DoRXCalcTeX(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms)
  322. {
  323.     UWORD    BackIntType, BackIntBase, BackIntSign, BackIntAngle;
  324.     char    NewBuff[258], **Exprs;
  325.     BOOL    GoOn = TRUE;
  326.  
  327.         // Build expression from partial input
  328.  
  329.     strcpy(NewBuff, "");
  330.     Exprs = (char **)Parms[0];
  331.     while(*Exprs && GoOn)
  332.     {
  333.         if((strlen(*Exprs) + strlen(NewBuff)) < 256)
  334.             strcat(NewBuff, *Exprs);
  335.         else
  336.             GoOn = FALSE;
  337.  
  338.         Exprs++;
  339.     }
  340.  
  341.         // Set input string to current expression
  342.  
  343.     set(InputString, MUIA_String_Contents, NewBuff);
  344.  
  345.         // Calculate expression (with ARexx settings)
  346.  
  347.     BackIntType    = IntType;
  348.     BackIntBase    = IntBase;
  349.     BackIntSign    = IntSign;
  350.     BackIntAngle    = IntAngle;
  351.  
  352.     IntType        = RXIntType;
  353.     IntBase        = RXIntBase;
  354.     IntSign        = RXIntSign;
  355.     IntAngle    = RXIntAngle;
  356.  
  357.     GoOn = FormatOutput(FALSE);
  358.  
  359.     IntType        = BackIntType;
  360.     IntBase        = BackIntBase;
  361.     IntSign        = BackIntSign;
  362.     IntAngle    = BackIntAngle;
  363.  
  364.     if(GoOn)
  365.     {
  366.  
  367.             // Return output
  368.  
  369.         return(ReturnEntry(GET_OUTPUT, TEX_OUTPUT, GET_ACTIVE));
  370.     }
  371.     else
  372.         return((APTR)set(AppObject, MUIA_Application_RexxString,  " "));
  373. }
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380. /**********************************************************************/
  381. /*               GetOutput (normal output) from History               */
  382. /*               --------------------------------------               */
  383. /* Parameters may be :                                                */
  384. /* n    = Nth entry                                                   */
  385. /* -1   = Active entry                                                */
  386. /* -2   = TopMost entry                                               */
  387. /* -3   = BottomMost entry                                            */
  388. /* default :                                                          */
  389. /*        get active entry                                            */
  390. /**********************************************************************/
  391. static APTR __saveds __asm GetOutput(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms)
  392. {
  393.     LONG    *ValuePtr = (LONG *)Parms[0],
  394.         Value;
  395.  
  396.     if(ValuePtr)
  397.         Value    = *ValuePtr;
  398.     else
  399.         Value    = -1;
  400.  
  401.     return(ReturnEntry(GET_OUTPUT, NORMAL_OUTPUT, Value));
  402. }
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409. /**********************************************************************/
  410. /*                GetOutput (TeX output) from History                 */
  411. /*                -----------------------------------                 */
  412. /* Parameters may be :                                                */
  413. /* n    = Nth entry                                                   */
  414. /* -1   = Active entry                                                */
  415. /* -2   = TopMost entry                                               */
  416. /* -3   = BottomMost entry                                            */
  417. /* default :                                                          */
  418. /*        get active entry                                            */
  419. /**********************************************************************/
  420. static APTR __saveds __asm GetTeXOutput(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms)
  421. {
  422.     LONG    *ValuePtr = (LONG *)Parms[0],
  423.         Value;
  424.  
  425.     if(ValuePtr)
  426.         Value    = *ValuePtr;
  427.     else
  428.         Value    = -1;
  429.  
  430.     return(ReturnEntry(GET_OUTPUT, TEX_OUTPUT, Value));
  431. }
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439. /**********************************************************************/
  440. /*                GetInput (normal input) from History                */
  441. /*                ------------------------------------                */
  442. /* Parameters may be :                                                */
  443. /* n    = Nth entry                                                   */
  444. /* -1   = Active entry                                                */
  445. /* -2   = TopMost entry                                               */
  446. /* -3   = BottomMost entry                                            */
  447. /* default :                                                          */
  448. /*        get active entry                                            */
  449. /**********************************************************************/
  450. static APTR __saveds __asm GetInput(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms)
  451. {
  452.     LONG    *ValuePtr = (LONG *)Parms[0],
  453.         Value;
  454.  
  455.     if(ValuePtr)
  456.         Value    = *ValuePtr;
  457.     else
  458.         Value    = -1;
  459.  
  460.     return(ReturnEntry(GET_INPUT, NORMAL_OUTPUT, Value));
  461. }
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469. /**********************************************************************/
  470. /*                 GetInput (TeX input) from History                  */
  471. /*                 ---------------------------------                  */
  472. /* Parameters may be :                                                */
  473. /* n    = Nth entry                                                   */
  474. /* -1   = Active entry                                                */
  475. /* -2   = TopMost entry                                               */
  476. /* -3   = BottomMost entry                                            */
  477. /* default :                                                          */
  478. /*        get active entry                                            */
  479. /**********************************************************************/
  480. static APTR __saveds __asm GetTeXInput(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms)
  481. {
  482.     LONG    *ValuePtr = (LONG *)Parms[0],
  483.         Value;
  484.  
  485.     if(ValuePtr)
  486.         Value    = *ValuePtr;
  487.     else
  488.         Value    = -1;
  489.  
  490.     return(ReturnEntry(GET_INPUT, TEX_OUTPUT, Value));
  491. }
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500. /**********************************************************************/
  501. /*                 Format an expression to TeX format                 */
  502. /*                 ----------------------------------                 */
  503. /* Parameters must be                                                 */
  504. /* Expr  = Expression to convert                                      */
  505. /**********************************************************************/
  506. static APTR __saveds __asm DoFormatTeX(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms)
  507. {
  508.     char    NewBuff[512], **Exprs;
  509.     char    OutBuff[840];
  510.     BOOL    GoOn = TRUE;
  511.  
  512.         // Build expression from partial input
  513.  
  514.     strcpy(NewBuff, "");
  515.     Exprs = (char **)Parms[0];
  516.     while(*Exprs && GoOn)
  517.     {
  518.         if((strlen(*Exprs) + strlen(NewBuff)) < 256)
  519.             strcat(NewBuff, *Exprs);
  520.         else
  521.             GoOn = FALSE;
  522.  
  523.         Exprs++;
  524.     }
  525.  
  526.         // Format output
  527.  
  528.     FormatTeX(NewBuff, OutBuff);
  529.  
  530.         // Return RexxString
  531.  
  532.     return((APTR)set(AppObject, MUIA_Application_RexxString,  OutBuff));
  533. }
  534.  
  535.  
  536.  
  537.  
  538.  
  539. /**********************************************************************/
  540. /*               Set outputmodes for ARexx Calculation                */
  541. /*               -------------------------------------                */
  542. /* Parameters may be :                                                */
  543. /* BASE  = Set the output base                                        */
  544. /* SIZE  = Set output size                                            */
  545. /* SIGN  = Set signed/unsigned output                                 */
  546. /* ANGLE = Set default angle                                          */
  547. /**********************************************************************/
  548. static APTR __saveds __asm SetMode(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms)
  549. {
  550.     char    *ThisVal;
  551.  
  552.         // Check for BASE keyword
  553.  
  554.     if((ThisVal = (char *)Parms[0]))
  555.     {
  556.         if(MatchToolValue(ThisVal, "DEC"))
  557.             RXIntType    = ID_DECIMAL;
  558.         else if(MatchToolValue(ThisVal, "HEX"))
  559.             RXIntType    = ID_HEXDECIMAL;
  560.         else if(MatchToolValue(ThisVal, "OCT"))
  561.             RXIntType    = ID_OCTAL;
  562.         else if(MatchToolValue(ThisVal, "BIN"))
  563.             RXIntType    = ID_BINARY;
  564.     }
  565.  
  566.         // Check for SIZE keyword
  567.  
  568.     if((ThisVal = (char *)Parms[1]))
  569.     {
  570.         if(MatchToolValue(ThisVal, "8"))
  571.             RXIntBase    = ID_8BIT;
  572.         else if(MatchToolValue(ThisVal, "16"))
  573.             RXIntBase    = ID_16BIT;
  574.         else if(MatchToolValue(ThisVal, "32"))
  575.             RXIntBase    = ID_32BIT;
  576.     }
  577.  
  578.         // Check for SIGN keyword
  579.  
  580.     if((ThisVal = (char *)Parms[2]))
  581.     {
  582.         if(MatchToolValue(ThisVal, "SIGNED"))
  583.             RXIntSign    = ID_SIGNED;
  584.         else if(MatchToolValue(ThisVal, "UNSIGNED"))
  585.             RXIntSign    = ID_UNSIGNED;
  586.     }
  587.  
  588.  
  589.         // Check for ANGLE keyword
  590.  
  591.     if((ThisVal = (char *)Parms[3]))
  592.     {
  593.         if(MatchToolValue(ThisVal, "RAD"))
  594.             RXIntAngle    = ID_RAD;
  595.         else if(MatchToolValue(ThisVal, "DEG"))
  596.             RXIntAngle    = ID_DEG;
  597.         else if(MatchToolValue(ThisVal, "GRA"))
  598.             RXIntAngle    = ID_GRAD;
  599.     }
  600.  
  601.     return(NULL);
  602. }
  603.  
  604.  
  605.  
  606.  
  607. /**********************************************************************/
  608. /*                    Delete an entry from history                    */
  609. /*                    ----------------------------                    */
  610. /* Parameters may be:                                                 */
  611. /*  n    = n-th entry                                                 */
  612. /*  -1   = active entry                                               */
  613. /*  -2   = topmost entry                                              */
  614. /*  -3   = bottommost entry                                           */
  615. /* default                                                            */
  616. /*   remove active entry                                              */
  617. /**********************************************************************/
  618. static APTR __saveds __asm DeleteHistoryFunc(register __a0 struct Hook *MyHook, register __a2 APTR *MyObject, register __a1 ULONG *Parms)
  619. {
  620.     LONG    *ValuePtr = (LONG *)Parms[0],
  621.         Value;
  622.  
  623.     if(ValuePtr)
  624.         Value    = *ValuePtr;
  625.     else
  626.         Value    = -1;
  627.  
  628.     DeleteHistory(Value);
  629.  
  630.     return(NULL);
  631. }
  632.  
  633.  
  634.  
  635.  
  636. /**********************************************************************/
  637. /*                       Return a normal entry                        */
  638. /**********************************************************************/
  639. static APTR ReturnEntry(UWORD Mode, UWORD OutMode, LONG Pos)
  640. {
  641.     struct    ListStruct    *LSV    = NULL;
  642.     char    *Source;
  643.     char    OutBuffer[300];
  644.  
  645.         // Get appropriate entry
  646.  
  647.     if(Pos == GET_ACTIVE)
  648.         DoMethod(OutputBox, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &LSV);
  649.     else if(Pos == GET_TOPMOST)
  650.         DoMethod(OutputBox, MUIM_List_GetEntry, 0, &LSV);
  651.     else if(Pos == GET_BOTTOMMOST)
  652.     {
  653.         struct    ListStruct    *Dummy;
  654.         ULONG    i;
  655.  
  656.             // Move to end of list
  657.  
  658.         for(i = 0;; i++)
  659.         {
  660.             DoMethod(OutputBox, MUIM_List_GetEntry, i, &Dummy);
  661.  
  662.             if(Dummy)
  663.                 LSV = Dummy;
  664.             else
  665.                 break;
  666.         }
  667.     }
  668.     else
  669.     {
  670.         ULONG    NumEntries;
  671.  
  672.             // First check, whether entry argument is appropriate
  673.  
  674.         get(OutputBox, MUIA_List_Entries, &NumEntries);
  675.  
  676.         if(Pos < GET_BOTTOMMOST || Pos >= NumEntries)
  677.             Pos    = MUIV_List_GetEntry_Active;
  678.  
  679.         DoMethod(OutputBox, MUIM_List_GetEntry, Pos, &LSV);
  680.     }
  681.  
  682.     if(LSV)
  683.     {
  684.             // Get correct source of input
  685.  
  686.         Source    = ((Mode == GET_OUTPUT) ? LSV->Output : LSV->Input);
  687.  
  688.         if(OutMode == NORMAL_OUTPUT)
  689.             strncpy(OutBuffer, Source, 299);
  690.         else
  691.             FormatTeX(Source, OutBuffer);
  692.  
  693.             // Not to return Error message ?!?
  694.  
  695.         if(strncmp(Source, "\0338", 2))
  696.         {
  697.                 // Return RexxString
  698.  
  699.             return((APTR)set(AppObject, MUIA_Application_RexxString,  OutBuffer));
  700.         }
  701.         else
  702.             return((APTR)set(AppObject, MUIA_Application_RexxString,  " "));
  703.     }
  704.     else
  705.         return((APTR)set(AppObject, MUIA_Application_RexxString,  " "));
  706. }
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715. /**********************************************************************/
  716. /*            Format a given entry according to TeX rules             */
  717. /**********************************************************************/
  718. static void FormatTeX(char *Source, char *Dest)
  719. {
  720.     char    *CopyDest    = Dest;
  721.     BOOL    HexMode        = FALSE;
  722.     BOOL    PowMode        = FALSE;
  723.     UWORD    BraceMode    = 0;
  724.  
  725.     while(*Source)
  726.     {
  727.         if(*Source == '$')
  728.         {
  729.             *Dest++    = '\\';
  730.             *Dest++    = '$';
  731.             Source++;
  732.  
  733.             HexMode    = TRUE;
  734.         }
  735.  
  736.         if(*Source == '0' && (*(Source + 1) == 'x' || *(Source + 1) == 'X'))
  737.         {
  738.             *Dest++    = '\\';
  739.             *Dest++    = '$';
  740.             Source++;
  741.             Source++;
  742.  
  743.             HexMode    = TRUE;
  744.         }
  745.  
  746.         else if(*Source == '*')
  747.         {
  748.             *Dest    = '\0';
  749.             strcat(CopyDest, "\\cdot ");
  750.             Dest    = CopyDest + strlen(CopyDest);
  751.             Source++;
  752.  
  753.             HexMode    = FALSE;
  754.         }
  755.  
  756.         else if(*Source == '\\')
  757.         {
  758.             *Dest++    = '\\';
  759.  
  760.             HexMode    = FALSE;
  761.         }
  762.  
  763.         else if(*Source == '&')
  764.         {
  765.             *Dest++    = '\\';
  766.             *Dest++    = *Source++;
  767.  
  768.             HexMode    = FALSE;
  769.         }
  770.  
  771.         else if(*Source == '%')
  772.         {
  773.             *Dest++    = '\\';
  774.             *Dest++    = *Source++;
  775.  
  776.             HexMode    = FALSE;
  777.         }
  778.  
  779.         else if(*Source == '(')
  780.         {
  781.             *Dest++        = *Source++;
  782.             BraceMode++;
  783.         }
  784.  
  785.         else if(*Source == ')')
  786.         {
  787.             *Dest++        = *Source++;
  788.             BraceMode--;
  789.             if(PowMode && !BraceMode)
  790.             {
  791.                 *Dest++    = '}';
  792.                 PowMode    = FALSE;
  793.             }
  794.         }
  795.  
  796.         else if(*Source == '^')
  797.         {
  798.             *Dest++        = *Source++;
  799.             *Dest++        = '{';
  800.             PowMode        = TRUE;
  801.         }
  802.  
  803.         else if((*Source == 'e' || *Source == 'E') && !HexMode)
  804.         {
  805.             *Dest    = '\0';
  806.             strcat(CopyDest, "\\cdot 10^{");
  807.             Dest    = CopyDest + strlen(CopyDest);
  808.             Source++;
  809.             while(*Source == '-' || (*Source >= '0' && *Source <= '9'))
  810.                 *Dest++    = *Source++;
  811.             *Dest++    = '}';
  812.         }
  813.  
  814.         else if((*Source >= '0' && *Source <= '9') || *Source == '.' || *Source == '/' || *Source == '+' || *Source == '-' || *Source == ' ' || (*Source >= 'a' && *Source <= 'f') || (*Source >= 'A' && *Source <= 'F'))
  815.         {
  816.             *Dest++    = *Source++;
  817.             if(*Source == ' ' && PowMode && !BraceMode)
  818.             {
  819.                 *Dest++    = '}';
  820.                 PowMode    = FALSE;
  821.             }
  822.         }
  823.  
  824.         else
  825.             Source++;
  826.     }
  827.  
  828.     *Dest    = '\0';
  829. }
  830.