コード例 #1
0
 double StringUtils::toDouble(const String& value)
 {
     if (value.empty())
         boost::throw_exception(NumberFormatException());
     if (value.length() > 1 && (value[0] == L'-' || value[0] == L'.') && !UnicodeUtil::isDigit(value[1]))
         boost::throw_exception(NumberFormatException());
     if (value[0] != L'-' && value[0] != L'.' && !UnicodeUtil::isDigit(value[0]))
         boost::throw_exception(NumberFormatException());
     return std::wcstod(value.c_str(), NULL);
 }
コード例 #2
0
 int32_t StringUtils::toInt(const String& value)
 {
     if (value.empty())
         boost::throw_exception(NumberFormatException());
     if (value.size() > 1 && value[0] == L'-' && !UnicodeUtil::isDigit(value[1]))
         boost::throw_exception(NumberFormatException());
     if (value[0] != L'-' && !UnicodeUtil::isDigit(value[0]))
         boost::throw_exception(NumberFormatException());
     return (int32_t)std::wcstol(value.c_str(), NULL, 10);
 }
コード例 #3
0
 int64_t StringUtils::toLong(const String& value)
 {
     if (value.empty())
         boost::throw_exception(NumberFormatException());
     if (value.size() > 1 && value[0] == L'-' && !UnicodeUtil::isDigit(value[1]))
         boost::throw_exception(NumberFormatException());
     if (value[0] != L'-' && !UnicodeUtil::isDigit(value[0]))
         boost::throw_exception(NumberFormatException());
     #if defined(_WIN32) || defined(_WIN64)
     return _wcstoi64(value.c_str(), 0, 10);
     #else
     return wcstoll(value.c_str(), 0, 10);
     #endif
 }
コード例 #4
0
ファイル: Short.cpp プロジェクト: Himmele/Mindroid.cpp
sp<Short> Short::valueOf(const sp<String>& string, int32_t radix) {
    if (string == nullptr || string->isEmpty()) {
        throw NumberFormatException("String is null or empty");
    }
    if (::isspace(string->charAt(0))) {
        throw NumberFormatException(String::format("Invalid integral value: %s", string->c_str()));
    }
    // See http://man7.org/linux/man-pages/man3/strtol.3.html.
    char* endptr;
    int16_t value = ::strtol(string->c_str(), &endptr, radix);
    if (*endptr != '\0') {
        throw NumberFormatException(String::format("Invalid integral value: %s", string->c_str()));
    }
    return new Short(value);
}
コード例 #5
0
ファイル: Integer.cpp プロジェクト: BrentDouglas/calculator
Integer::Integer(const std::string& str) throw (NumberFormatException) 
	: bytes(str) {
	this->positive = true;
	if (this->bytes.length() == 0) {
		throw NumberFormatException();
	}

	if (this->bytes.at(0) == '-') {
		this->positive = false;
		this->bytes.erase(0,1);
	  if (this->bytes.length() == 0) {
	    throw NumberFormatException();
	  }
	} else {
		this->positive = true;
		if (this->bytes.at(0) == '+') {
			this->bytes.erase(0,1);
		  if (this->bytes.length() == 0) {
		    throw NumberFormatException();
		  }
		}
	}

	for (size_t i = 0; i < this->bytes.length(); ++i){
   	if (!isDigit(this->bytes.at(i))) {
			throw NumberFormatException();
		}
	}

	size_t zeros = this->bytes.find_first_not_of('0');
	if (zeros == std::string::npos) {
		this->bytes = "0";
		this->positive = true;
	} else {
		this->bytes.erase(0, zeros);
	}
}
コード例 #6
0
ファイル: JsonScanner.cpp プロジェクト: rokthewok/Argonaut
JsonToken JsonScanner::readNumberToken( ScannerState state, JsonTypes type, std::string & token ) {
	char c;
	
	while( true ) {
		c = m_reader->peekNextChar();

		if( isBlankOrNewline( c ) || c == '\0' || c == ',' ) {
			return JsonToken( type, token );
		} else {
			c = m_reader->getNextChar();
		}

		switch( state ) {
		case ScannerState::INTEGER:
			if( isdigit( c ) ) {
				token.push_back( c );
			} else if( c == '.' ) {
				state = ScannerState::REAL;
				type = JsonTypes::REAL;
				token.push_back( c );
			} else {
				throw NumberFormatException();
			}
			break;
		case ScannerState::REAL:
			if( isdigit( c ) ) {
				token.push_back( c );
			} else {
				throw NumberFormatException();
			}
			break;
		default:
			throw NumberFormatException();
		};
	}
}
コード例 #7
0
ファイル: RODFDetector.cpp プロジェクト: planetsumo/sumo
bool
RODFDetector::writeEmitterDefinition(const std::string& file,
                                     const std::map<SUMOTime, RandomDistributor<int>* >& dists,
                                     const RODFDetectorFlows& flows,
                                     SUMOTime startTime, SUMOTime endTime,
                                     SUMOTime stepOffset,
                                     bool includeUnusedRoutes,
                                     SUMOReal scale,
                                     bool insertionsOnly,
                                     SUMOReal defaultSpeed) const {
    OutputDevice& out = OutputDevice::getDevice(file);
    OptionsCont& oc = OptionsCont::getOptions();
    if (getType() != SOURCE_DETECTOR) {
        out.writeXMLHeader("additional", "additional_file.xsd");
    }
    // routes
    if (myRoutes != 0 && myRoutes->get().size() != 0) {
        const std::vector<RODFRouteDesc>& routes = myRoutes->get();
        out.openTag(SUMO_TAG_ROUTE_DISTRIBUTION).writeAttr(SUMO_ATTR_ID, myID);
        bool isEmptyDist = true;
        for (std::vector<RODFRouteDesc>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
            if ((*i).overallProb > 0) {
                isEmptyDist = false;
            }
        }
        for (std::vector<RODFRouteDesc>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
            if ((*i).overallProb > 0 || includeUnusedRoutes) {
                out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_REFID, (*i).routename).writeAttr(SUMO_ATTR_PROB, (*i).overallProb).closeTag();
            }
            if (isEmptyDist) {
                out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_REFID, (*i).routename).writeAttr(SUMO_ATTR_PROB, SUMOReal(1)).closeTag();
            }
        }
        out.closeTag(); // routeDistribution
    } else {
        WRITE_ERROR("Detector '" + getID() + "' has no routes!?");
        return false;
    }
    // insertions
    if (insertionsOnly || flows.knows(myID)) {
        // get the flows for this detector
        const std::vector<FlowDef>& mflows = flows.getFlowDefs(myID);
        // go through the simulation seconds
        int index = 0;
        for (SUMOTime time = startTime; time < endTime; time += stepOffset, index++) {
            // get own (departure flow)
            assert(index < (int)mflows.size());
            const FlowDef& srcFD = mflows[index];  // !!! check stepOffset
            // get flows at end
            RandomDistributor<int>* destDist = dists.find(time) != dists.end() ? dists.find(time)->second : 0;
            // go through the cars
            int carNo = (int)((srcFD.qPKW + srcFD.qLKW) * scale);
            for (int car = 0; car < carNo; ++car) {
                // get the vehicle parameter
                SUMOReal v = -1;
                std::string vtype;
                int destIndex = destDist != 0 && destDist->getOverallProb() > 0 ? (int) destDist->get() : -1;
                if (srcFD.isLKW >= 1) {
                    srcFD.isLKW = srcFD.isLKW - (SUMOReal) 1.;
                    v = srcFD.vLKW;
                    vtype = "LKW";
                } else {
                    v = srcFD.vPKW;
                    vtype = "PKW";
                }
                // compute insertion speed
                if (v <= 0 || v > 250) {
                    v = defaultSpeed;
                } else {
                    v = (SUMOReal)(v / 3.6);
                }
                // compute the departure time
                SUMOTime ctime = (SUMOTime)(time + ((SUMOReal) stepOffset * (SUMOReal) car / (SUMOReal) carNo));

                // write
                out.openTag(SUMO_TAG_VEHICLE);
                if (getType() == SOURCE_DETECTOR) {
                    out.writeAttr(SUMO_ATTR_ID, "emitter_" + myID + "_" + toString(ctime));
                } else {
                    out.writeAttr(SUMO_ATTR_ID, "calibrator_" + myID + "_" + toString(ctime));
                }
                if (oc.getBool("vtype")) {
                    out.writeAttr(SUMO_ATTR_TYPE, vtype);
                }
                out.writeAttr(SUMO_ATTR_DEPART, time2string(ctime));
                if (oc.isSet("departlane")) {
                    out.writeNonEmptyAttr(SUMO_ATTR_DEPARTLANE, oc.getString("departlane"));
                } else {
                    out.writeAttr(SUMO_ATTR_DEPARTLANE, TplConvert::_2int(myLaneID.substr(myLaneID.rfind("_") + 1).c_str()));
                }
                if (oc.isSet("departpos")) {
                    std::string posDesc = oc.getString("departpos");
                    if (posDesc.substr(0, 8) == "detector") {
                        SUMOReal position = myPosition;
                        if (posDesc.length() > 8) {
                            if (posDesc[8] == '+') {
                                position += TplConvert::_2SUMOReal(posDesc.substr(9).c_str());
                            } else if (posDesc[8] == '-') {
                                position -= TplConvert::_2SUMOReal(posDesc.substr(9).c_str());
                            } else {
                                throw NumberFormatException();
                            }
                        }
                        out.writeAttr(SUMO_ATTR_DEPARTPOS, position);
                    } else {
                        out.writeNonEmptyAttr(SUMO_ATTR_DEPARTPOS, posDesc);
                    }
                } else {
                    out.writeAttr(SUMO_ATTR_DEPARTPOS, myPosition);
                }
                if (oc.isSet("departspeed")) {
                    out.writeNonEmptyAttr(SUMO_ATTR_DEPARTSPEED, oc.getString("departspeed"));
                } else {
                    out.writeAttr(SUMO_ATTR_DEPARTSPEED, v);
                }
                if (oc.isSet("arrivallane")) {
                    out.writeNonEmptyAttr(SUMO_ATTR_ARRIVALLANE, oc.getString("arrivallane"));
                }
                if (oc.isSet("arrivalpos")) {
                    out.writeNonEmptyAttr(SUMO_ATTR_ARRIVALPOS, oc.getString("arrivalpos"));
                }
                if (oc.isSet("arrivalspeed")) {
                    out.writeNonEmptyAttr(SUMO_ATTR_ARRIVALSPEED, oc.getString("arrivalspeed"));
                }
                if (destIndex >= 0) {
                    out.writeAttr(SUMO_ATTR_ROUTE, myRoutes->get()[destIndex].routename);
                } else {
                    out.writeAttr(SUMO_ATTR_ROUTE, myID);
                }
                out.closeTag();
                srcFD.isLKW += srcFD.fLKW;
            }
        }
    }
    if (getType() != SOURCE_DETECTOR) {
        out.close();
    }
    return true;
}