]> jfr.im git - irc/evilnet/x3.git/blobdiff - src/math.c
Couple of srvx updates.
[irc/evilnet/x3.git] / src / math.c
index bac982033094cabd6b5b0d21fb97d833a5201922..bf7ae8c53e032151018377f2aff742014c590085 100644 (file)
@@ -2,7 +2,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.  Important limitations are
  * listed in the COPYING file that accompanies this software.
  *
 #include "saxdb.h"
 #include "timeq.h"
 
-#ifdef HAVE_TGMATH_H
-#include <tgmath.h>
+#ifndef HAVE_MATH_H
+  #include <math.h>
+  #include <complex.h>
 #else
-#include <math.h>
-#include <complex.h>
+  #include <tgmath.h>
 #endif
 
 #include <ctype.h>
@@ -125,6 +125,81 @@ void do_math(char *Buffer, char *Math)
        int Index;
        int OEndExpected;
 
+
+         /* This is a HACK to insert * before ( so multiplication takes place
+          * in things such as 3(3) instead of ignoring the first 3. Submitted
+          * by Osiris.
+          * I spent some time tlooking at calc.c and I didnt see an easy way
+          * to do this there... and that file gives me a headache.. so.. -Rubin
+          */
+         char newMath[MAXLEN];
+         char *ptr;
+         char lastNumber = false;
+         char lastBracket = false;
+
+
+         ptr = newMath;
+         while(*Math && ptr < newMath+MAXLEN-1)
+         {
+             switch(*Math)
+             {
+               case '1': case '2':
+               case '3': case '4':
+               case '5': case '6':
+               case '7': case '8':
+               case '9': case '0':
+                 lastNumber = true;
+                 if(lastBracket == true)
+                 {
+                     *ptr = '*';
+                     ptr++;
+                 }
+                 *ptr = *Math;
+                 lastBracket = false;
+                 break;
+               case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 
+                case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': 
+                case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': 
+                case 'v': case 'w': case 'x': case 'y': case 'z': 
+               case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 
+                case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': 
+                case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': 
+                case 'V': case 'W': case 'X': case 'Y': case 'Z': 
+                 case ')': case ']': /* Support sin[12](3) also */
+                 lastNumber = true;
+                 lastBracket = true;
+                 *ptr = *Math;
+                 break;
+               case '(': case '[':
+                 if (lastNumber == true)
+                 {
+                   *ptr = '*';
+                   ptr++;
+                 }
+                 *ptr = *Math;
+                 lastNumber = false;
+                 lastBracket = false;
+                 break;
+               default:
+                 if(isalpha(*Math))
+                 {
+                   if(lastNumber == true)
+                   {
+                     *ptr = '*';
+                     ptr++;
+                   }
+                 }
+                 *ptr = *Math;
+                 lastNumber = false;
+                 lastBracket = false;
+                 break;
+             }
+             Math++;
+             ptr++;
+           }
+           *ptr = '\0';
+           Math = newMath;
+
        if (!(List = ListFirst = malloc(sizeof(struct MathToken)))) goto MemError;
        if (!(Stack = StackFirst = malloc(sizeof(struct MathToken)))) goto MemError;
        if (!(PostFix = PostFixFirst = malloc(sizeof(struct MathToken)))) goto MemError;