Example #1
0
XMLBigDecimal::XMLBigDecimal(const XMLCh* const strValue,
                             MemoryManager* const manager)
: fSign(0)
, fTotalDigits(0)
, fScale(0)
, fRawDataLen(0)
, fRawData(0)
, fIntVal(0)
, fMemoryManager(manager)
{
    if ((!strValue) || (!*strValue))
        ThrowXMLwithMemMgr(NumberFormatException, XMLExcepts::XMLNUM_emptyString, fMemoryManager);

    CleanupType cleanup(this, &XMLBigDecimal::cleanUp);

    try
    {
        fRawDataLen = XMLString::stringLen(strValue);
        fRawData = (XMLCh*) fMemoryManager->allocate
        (
            ((fRawDataLen*2) + 2) * sizeof(XMLCh) //fRawData and fIntVal
        );
        memcpy(fRawData, strValue, fRawDataLen * sizeof(XMLCh));
        fRawData[fRawDataLen] = chNull;
        fIntVal = fRawData + fRawDataLen + 1;
        parseDecimal(strValue, fIntVal, fSign, (int&) fTotalDigits, (int&) fScale, fMemoryManager);
    }
    catch(const OutOfMemoryException&)
    {
        cleanup.release();

        throw;
    }

    cleanup.release();
}
Example #2
0
void ATDecimalOrDerivedImpl::setDecimal(const XMLCh* const value)
{
  _decimal = parseDecimal(value);
}
Example #3
0
/*
	\param filename the name of the file
*/
struct linearProgram* createLPFromFile(const char* filename) {
	assert(NULL != filename);
	
	mode = READ_COL;
	
	FILE* fp;
	char buffer[BUF_SIZE];
	char* s;
	int lines = 0;
	
	struct linearProgram* binaryLP;
	int numOfCols = 0;
	int numOfRows = 0;
	int currentConstraint = 0;
	char* type;
	
	if (NULL == (fp = fopen(filename, "r"))) {
		fprintf(stderr, "Can't open file %s\n",filename);
		printf(USAGE);
		exit(EXIT_FAILURE);
	}
	// need a helpful string for strtok
	char* help_me = allocate(BUF_SIZE, sizeof(*help_me));
	char* end_pos;
	char* test;
	char* brks;
	while(NULL != (s = fgets(buffer, sizeof(buffer), fp))) {
		end_pos = strpbrk(s, "#\n\r");
		if (NULL != end_pos) {
			*end_pos = '\0'; // terminate string here, now we can process this line		
		}
		
		/* Skip over leading space
		*/
		while(isspace(*s)) 
			s++;
		
		/* Skip over empty lines
		*/
		if (!*s) /* <=> (*s == '\0') */
			continue;
		
		if (mode == READ_COL) {
			lines++;
			numOfCols = parseDecimal(s);
			mode++;
		}
		else if (mode == READ_ROW) {
			lines++;
			numOfRows = parseDecimal(s);
			
			/* ready to allocate memory for binaryLP.coeffs, rhs-vector and types-enum */
			binaryLP = initializeLP(numOfCols, numOfRows);
			// ready to parse constraint matrix
			mode++;
		}
		else {
			assert(mode == READ_CONSTR);
			lines++;
			if (currentConstraint + 1 > binaryLP->row) {
				printf("Second decimal must correspond to number of constraints!\n");
				exit(EXIT_FAILURE);
			}
			if (NULL == (type = strpbrk(s, "<>="))) {
				printf("Constraint in line %d has wrong shape (<,> or = missing)!\n", lines);
				exit(EXIT_FAILURE);
			} // check which type of constraint
			setType(*type, currentConstraint, binaryLP->types);
			
			// TODO WTF
			strcpy(help_me, s);
			if (-1 == fillRow(binaryLP->coeffs[currentConstraint], binaryLP->col, strtok_r(s,"><=",&brks), " ")) {
				// free pointer:
				deleteLinearProgram(binaryLP);
				deallocate(help_me);
				printf("Incorrect number or format of coefficients in line %d.\n", lines);
				printf(USAGE);
				exit(EXIT_FAILURE);
			}
			//test = strtok(help_me, "><=");
			if(NULL == (test = strtok_r(NULL, " ><=", &brks))) {
				printf("No rhs in line %d.\n", lines);
				printf(USAGE);
				exit(EXIT_FAILURE);
			}
			if (!validateNumber(test)) {
				printf("No number on right-hand-side of line %d.\n", lines);
				exit(EXIT_FAILURE);
			}
			binaryLP->rhs[currentConstraint] = atof(test);
			if (NULL != (test = strtok(NULL, " "))) {
				printf("More than one value on right-hand-side of line %d!\n", lines);
				exit(EXIT_FAILURE);
			}
			currentConstraint++;
		}
	}
	if (currentConstraint < numOfRows) {
		printf("Too few rows!\n");
		exit(EXIT_FAILURE);
	}
	if (lines == 0) {
		printf("The file was empty! Nice try...\n");
		exit(EXIT_FAILURE);
	}
	free(help_me);
	
	fclose(fp);
	return binaryLP;
}
Example #4
0
static void gpioFieldCompleted(void)
{
    if (!post.gpio.buffer[0])
        return;
    post.gpio.mode = parseDecimal(post.gpio.buffer);
}
Example #5
0
static void tcpUpdateFieldCompleted(void)
{
    if (!post.tcpUpdate.buffer[0])
        return;
    Serial_setTCPPort(parseDecimal(post.tcpUpdate.buffer));
}
Example #6
0
/***
 * 3.2.3 decimal  
 *
 * . the preceding optional "+" sign is prohibited. 
 * . The decimal point is required. 
 * . Leading and trailing zeroes are prohibited subject to the following: 
 *   there must be at least one digit to the right and to the left of the decimal point which may be a zero.
 *
 ***/
XMLCh* XMLBigDecimal::getCanonicalRepresentation(const XMLCh*         const rawData
                                               ,       MemoryManager* const memMgr)
{

    XMLCh* retBuf = (XMLCh*) memMgr->allocate( (XMLString::stringLen(rawData)+1) * sizeof(XMLCh));
    ArrayJanitor<XMLCh> janName(retBuf, memMgr);
    int   sign, totalDigits, fractDigits;

    try
    {
        parseDecimal(rawData, retBuf, sign, totalDigits, fractDigits, memMgr);
    }
    catch (const NumberFormatException&)
    {
        return 0;
    }


    //Extra space reserved in case strLen is zero
    XMLSize_t strLen = XMLString::stringLen(retBuf);
    XMLCh* retBuffer = (XMLCh*) memMgr->allocate( (strLen + 4) * sizeof(XMLCh));

    if ( (sign == 0) || (totalDigits == 0))
    {
        retBuffer[0] = chDigit_0;
        retBuffer[1] = chPeriod;
        retBuffer[2] = chDigit_0;
        retBuffer[3] = chNull;
    }
    else
    {
        XMLCh* retPtr = retBuffer;

        if (sign == -1)
        {
            *retPtr++ = chDash;
        }

        if (fractDigits == totalDigits)   // no integer
        {           
            *retPtr++ = chDigit_0;
            *retPtr++ = chPeriod;
            XMLString::copyNString(retPtr, retBuf, strLen);
            retPtr += strLen;
            *retPtr = chNull;
        }
        else if (fractDigits == 0)        // no fraction
        {
            XMLString::copyNString(retPtr, retBuf, strLen);
            retPtr += strLen;
            *retPtr++ = chPeriod;
            *retPtr++ = chDigit_0;
            *retPtr   = chNull;
        }
        else  // normal
        {
            int intLen = totalDigits - fractDigits;
            XMLString::copyNString(retPtr, retBuf, intLen);
            retPtr += intLen;
            *retPtr++ = chPeriod;
            XMLString::copyNString(retPtr, &(retBuf[intLen]), fractDigits);
            retPtr += fractDigits;
            *retPtr = chNull;
        }

    }
            
    return retBuffer;
}