EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Archive::Development (https://www.eqemulator.org/forums/forumdisplay.php?f=621)
-   -   5.0 fixes / changes (https://www.eqemulator.org/forums/showthread.php?t=8701)

alkrun 07-17-2003 12:58 AM

5.0 fixes / changes
 
I figured I'd make one thread for things I fix / change, this was just the first significant change. If I understood what calc() was supposed to do in parser.cpp (that is, calculate a simple math expression in a string) then it had a few problems. I rewrote it, see below:

Code:

int calc( string calc )
{
        string::iterator iterator = calc.begin();
        string buffer;
        string integer1;
        char op = 0;
        int returnvalue = 0;
       
        while (*iterator)
        {
                char ch = *iterator;
               
                if(ch >= '0' && ch <= '9')//If this character is numeric add it to the buffer
                {
                        buffer += ch;
                }
                else if(ch == '+' || ch == '-' || ch == '/' || ch == '*')//otherwise, are we an operator?
                {
                        int val = atoi(buffer.c_str());
                        if(!op)//if this is the first time through, set returnvalue to what we have so far
                        {
                                returnvalue = val;
                        }
                        else//otherwise we've got returnvalue initialized, perform operation on previous numbers
                        {
                                if(buffer.length() == 0)//Do we have a value?
                                {
                                        printf("Parser::calc() Error in syntax: '%s'.\n", calc);
                                        return 0;
                                }
                                //what was the previous op
                                switch(op)
                                {
                                case '+':
                                        {
                                                returnvalue += val;
                                                break;
                                        }
                                case '-':
                                        {
                                                returnvalue -= val;
                                                break;
                                        }
                                case '/':
                                        {
                                                if(val == 0)//can't divide by zero
                                                {
                                                        printf("Parser::calc() Error, Divide by zero '%s'.\n", calc);
                                                        return 0;
                                                }
                                                returnvalue /= val;
                                                break;
                                        }
                                case '*':
                                        {
                                                returnvalue *= val;
                                                break;
                                        }
                                };
                                op = ch;//save current operator and continue parsing
                                buffer.clear();//clear buffer now that we're starting on a new number
                        }
                        op = ch;
                }
                else
                {
                        printf("Parser::calc() Error processing '%c'.\n", ch);
                        return 0;
                }
        }
        return returnvalue;
}


Merth 07-17-2003 08:26 AM

This has been applied and will be available on the next CVS push. I only verified that it compiled, I haven't tested the code at all.

Thanks!

alkrun 07-17-2003 08:46 AM

I was looking back through this and I missed a change that I had made.

At the end of the op section, where I call buffer.clear(), that needs to be moved outside of the if(!op) block, and the op = ch above it is redundant. So what was:

Code:

            };
            op = ch;//save current operator and continue parsing
            buffer.clear();//clear buffer now that we're starting on a new number
        }
        op = ch;
      }

should be:

Code:

            };
        }
        buffer.clear();//clear buffer now that we're starting on a new number
        op = ch;
      }


Merth 07-17-2003 08:58 AM

Applied that change, too

alkrun 07-17-2003 04:16 PM

EDIT: This doesn't really work. I was assuming the client kept it's connection to the world server, that doesn't look like the case. I hadn't tested it enough.

alkrun 07-18-2003 12:16 PM

in parser.cpp the itoa() function should declare the tmp[] buffer as 12 chars instead of 10. it needs one char for the terminating null and it's possible the value is negative which will prepend a '-' to the string and requiring another character.

devn00b 07-18-2003 12:22 PM

nice changes here alkrun
:juggle:

alkrun 07-18-2003 12:32 PM

thanks.

Another thing that's sort of unrelated to a code fix is that the anonymous CVS source is "messed up"(tm). A lot of the files are doublespaced when checked out on windows (not sure about unix). In some files the EOL sequence when checked out is CRCRLF. I've written something to strip all of the files of CRCRLF and replace it with a CRLF, but I figured I'd pass it along. My guess is that something is wrong with the way the code is being dumped into CVS.

mangoo 07-18-2003 12:39 PM

Alkrun, regarding CVS in the Windows platform... The double spacing isn't a problem in the CVS itself. I believe it's from going from UNIX to Windows. I'm assuming you are using WinCVS to retrieve files from CVS. So here is how to fix the double spacing...

In WinCVS go into preferences, Globals tab. At the bottom will be a checkbox for "Check out text files with the Unix LF". Make sure that is checked and all is well :D

alkrun 07-18-2003 12:46 PM

Ah, I'm actually using cvs from the commandline. I've seen this before but it's normally when files are checked out on a unix platform, then checked in using a cygwin version of CVS or some other method of copying files checked out from CVS on one platform to the other.

But I'll take a look and see if there's an option, I don't see one, so I might have to just use wincvs. Thanks.


All times are GMT -4. The time now is 06:51 AM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.