Example #1
0
static void*
imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 )
{
    CV_Assert(!buf.empty() && buf.isContinuous());
    IplImage* image = 0;
    CvMat *matrix = 0;
    Mat temp, *data = &temp;
    String filename;

    ImageDecoder decoder = findDecoder(buf);
    if( !decoder )
        return 0;

    if( !decoder->setSource(buf) )
    {
        filename = tempfile();
        FILE* f = fopen( filename.c_str(), "wb" );
        if( !f )
            return 0;
        size_t bufSize = buf.cols*buf.rows*buf.elemSize();
        fwrite( buf.ptr(), 1, bufSize, f );
        fclose(f);
        decoder->setSource(filename);
    }

    if( !decoder->readHeader() )
    {
        decoder.release();
        if ( !filename.empty() )
        {
            if ( remove(filename.c_str()) != 0 )
            {
                CV_Error( CV_StsError, "unable to remove temporary file" );
            }
        }
        return 0;
    }

    CvSize size;
    size.width = decoder->width();
    size.height = decoder->height();

    int type = decoder->type();
    if( (flags & IMREAD_LOAD_GDAL) != IMREAD_LOAD_GDAL && flags != IMREAD_UNCHANGED )
    {
        if( (flags & CV_LOAD_IMAGE_ANYDEPTH) == 0 )
            type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type));

        if( (flags & CV_LOAD_IMAGE_COLOR) != 0 ||
           ((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) )
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3);
        else
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1);
    }

    if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT )
    {
        if( hdrtype == LOAD_CVMAT )
        {
            matrix = cvCreateMat( size.height, size.width, type );
            temp = cvarrToMat(matrix);
        }
        else
        {
            mat->create( size.height, size.width, type );
            data = mat;
        }
    }
    else
    {
        image = cvCreateImage( size, cvIplDepth(type), CV_MAT_CN(type) );
        temp = cvarrToMat(image);
    }

    bool code = decoder->readData( *data );
    decoder.release();
    if ( !filename.empty() )
    {
        if ( remove(filename.c_str()) != 0 )
        {
            CV_Error( CV_StsError, "unable to remove temporary file" );
        }
    }

    if( !code )
    {
        cvReleaseImage( &image );
        cvReleaseMat( &matrix );
        if( mat )
            mat->release();
        return 0;
    }

    return hdrtype == LOAD_CVMAT ? (void*)matrix :
        hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat;
}
Example #2
0
String StringUtil::UUDecode(const String& input) {
  if (!input.empty()) {
    return string_uudecode(input.data(), input.size());
  }
  return String();
}
Example #3
0
String StringUtil::ROT13(const String& input) {
  if (input.empty()) return input;
  return String(string_rot13(input.data(), input.size()),
                input.size(), AttachString);
}
Example #4
0
void read_mse1_card(Set& set, wxFileInputStream& f, wxTextInputStream& file) {
	CardP card(new Card(*set.game));
	while (!f.Eof()) {
		// read a line
		String line = file.ReadLine();
		if (line.empty()) continue;
		Char type = line.GetChar(0);
		line = line.substr(1);
		// interpret this line
		switch (type) {
			case 'A': {		// done
				set.cards.push_back(card);
				return;
			} case 'B': {	// name
				card->value(_("name")) = to_script(line);
				break;
			} case 'C': case 'D': { // image filename
				LocalFileName image_file = set.newFileName(_("image"),_("")); // a new unique name in the package
				if (wxCopyFile(line, set.nameOut(image_file), true)) {
					card->value(_("image")) = script_local_image_file(image_file);
				}
				break;
			} case 'E':	{	// super type
				card->value(_("super type")) = to_script(line);
				break;
			} case 'F': {	// sub type
				card->value(_("sub type")) = to_script(line);
				break;
			} case 'G': {	// casting cost
				card->value(_("casting cost")) = to_script(line);
				break;
			} case 'H': {	// rarity
				String rarity;
				if      (line == _("(U)")) rarity = _("uncommon");
				else if (line == _("(R)")) rarity = _("rare");
				else                       rarity = _("common");
				card->value(_("rarity")) = to_script(rarity);
				break;
			} case 'I': {	// power/thoughness
				size_t pos = line.find_first_of(_('/'));
				if (pos != String::npos) {
					card->value(_("power")) = to_script(line.substr(0, pos));
					card->value(_("toughness")) = to_script(line.substr(pos+1));
				}
				break;
			} case 'J': {	// rule text or part of text
				append_line(card->value(_("rule text")), line);
				break;
			} case 'K':	{	// flavor text or part of text
				append_line(card->value(_("flavor text")), line);
				break;
			} case 'L': {	// card color (if not default)
				// decode color
				String color;
				if      (line == _("1")) color = _("white");
				else if (line == _("2")) color = _("blue");
				else if (line == _("3")) color = _("black");
				else if (line == _("4")) color = _("red");
				else if (line == _("5")) color = _("green");
				else if (line == _("6")) color = _("colorless");
				else if (line == _("7")) color = _("land");
				else if (line == _("9")) color = _("multicolor");
				else                     color = _("colorless");
				card->value(_("card color")) = to_script(color);
				break;
			} default: {
				throw ParseError(_("Not a valid MSE1 file"));
			}
		}
	}
}
Example #5
0
String StringUtil::QuotedPrintableDecode(const String& input) {
  if (input.empty()) return input;
  int len = input.size();
  return string_quoted_printable_decode(input.data(), len, false);
}
Example #6
0
bool Zone_Location_Map::Read (void)
{
	int num, zone, n1, n2, count;
	double low, high;
	bool zone_flag, loc_flag;

	String text;
	Strings ranges;
	Str_Itr range_itr;
	Int_Map_Itr map_itr;

	Ints_Map_Stat zone_loc_stat;
	Integers empty, *loc_ptr;

	Data_Service *data = (Data_Service *) exe;
	zone_flag = data->System_Data_Flag (ZONE);
	loc_flag = data->System_Data_Flag (LOCATION);

	loc_ptr = 0;
	count = 0;

	while (Db_File::Read ()) {

		text = Record_String ();
		if (text.empty ()) continue;

		text.Parse (ranges);

		for (range_itr = ranges.begin (); range_itr != ranges.end (); range_itr++) {

			//---- create the zone index ----

			if (range_itr == ranges.begin ()) {
				zone = range_itr->Integer ();
				if (zone == 0) break;

				if (zone_flag) {
					map_itr = data->zone_map.find (zone);
					if (map_itr == data->zone_map.end ()) break;
					zone = map_itr->second;
				}
				zone_loc_stat = insert (Ints_Map_Data (zone, empty));

				loc_ptr = &zone_loc_stat.first->second;
				continue;
			}

			//---- parse location records ----

			if (!range_itr->Range (low, high)) continue;
			n1 = (int) low;
			n2 = (int) high;

			if (n1 == 0 && n2 == 0) continue;

			if (n1 > n2) {
				if (exe->Send_Messages ()) {
					exe->Write (1, String ("Location Range %d..%d is Illogical") % n1 % n2); 
				}
				return (false);
			}
			for (num = n1; num <= n2; num++) {
				if (loc_flag) {
					map_itr = data->location_map.find (num);
					if (map_itr == data->location_map.end ()) continue;
					loc_ptr->push_back (map_itr->second);
					count++;
				} else {
					loc_ptr->push_back (num);
					count++;
				}
			}
		}
	}
	exe->Print (2, String ("%s contains %d Zones and %d Locations") % File_Type () % Num_Zones () % count);
	return (true);
}
Example #7
0
void CmdVariable::PrintVariables(DebuggerClient &client, CArrRef variables,
                                 int frame, const String& text, int version) {
  bool global = frame == -1; // I.e. we were called from CmdGlobal, or the
  //client's current frame is the global frame, according to OnServer
  bool system = true;
  int i = 0;
  bool found = false;
  for (ArrayIter iter(variables); iter; ++iter) {
    String name = iter.first().toString();
    String value;
    if (version == 2) {
      // Using the new protocol, so variables contain only names.
      // Fetch the value separately.
      CmdVariable cmd(client.isStackTraceAsync()
        ? KindOfVariableAsync : KindOfVariable);
      cmd.m_frame = frame;
      cmd.m_variables = null_array;
      cmd.m_varName = name;
      cmd.m_filter = text;
      cmd.m_version = 2;
      auto rcmd = client.xend<CmdVariable>(&cmd);
      if (!rcmd->m_variables.empty()) {
        value = DebuggerClient::FormatVariable(rcmd->m_variables[name], 200);
        found = true;
      } else if (text.empty()) {
        // Not missing because filtered out, assume the value is too large.
        value = s_omitted;
        found = true;
      } else {
        if (name.find(text, 0, false) >= 0) {
          // Server should have matched it.
          // Assume missing because value is too large.
          value = s_omitted;
          found = true;
        } else {
          // The variable was filtered out on the server, using text.
          // Or it was just too large. Either way we let skip over it.
          continue;
        }
      }
    } else {
      value = DebuggerClient::FormatVariable(iter.second(), 200);
    }
    if (version == 0 && !text.empty()) {
      if (name.find(text, 0, false) >= 0) {
        client.print("%s = %s", name.data(), value.data());
        found = true;
      } else {
        String fullvalue = DebuggerClient::FormatVariable(value, -1);
        if (fullvalue.find(text, 0, false) >= 0) {
          client.print("%s = %s", name.data(), value.data());
          found = true;
        }
      }
    } else {
      if (global && system) {
        client.print("$%s = %s", name.data(), value.data());
      } else {
        client.output("$%s = %s", name.data(), value.data());
      }

      // we know s_http_response_header is the last system global
      if (global && name == s_http_response_header) {
        client.output("%s", "");
        system = false;
      }

      ++i;
      if (i % DebuggerClient::ScrollBlockSize == 0 &&
          client.ask("There are %zd more variables. Continue? [Y/n]",
                      variables.size() - i) == 'n') {
        break;
      }
    }
  }

  if (!text.empty() && !found) {
    client.info("(unable to find specified text in any variables)");
  }
}
    // --------------------------------------------
    void LightImporter::createLight ( 
        const COLLADAFW::Light* light,  
        MayaNode* mayaTransformNode )
    {
        // Check if the camera is already imported.
        const COLLADAFW::UniqueId& lightId = light->getUniqueId ();

        // Make the maya name unique and manage it in all necessary lists.
        String lightName = light->getName ();
        if ( lightName.empty () ) lightName = LIGHT_NAME;
        lightName = DocumentImporter::frameworkNameToMayaName ( lightName );
        const ExtraDataCallbackHandler& callbackHandler = getDocumentImporter ()->getMayaIdCallbackHandler ();
        String originalMayaId = getOriginalMayaId ( callbackHandler, lightId, COLLADASaxFWL15::HASH_ELEMENT_LIGHT );
        if ( !originalMayaId.empty () ) lightName = originalMayaId;
        lightName = generateUniqueDagNodeName ( lightName, mayaTransformNode );

        // Create a maya node object of the current node and push it into the map.
        MayaNode* mayaLightNode = new MayaNode ( lightId, lightName, mayaTransformNode );
        mMayaLightNodesMap [ lightId ] = mayaLightNode;

        FILE* file = getDocumentImporter ()->getFile ();

        MayaDM::Light* mayaLight = 0;
        COLLADAFW::Light::LightType lightType = light->getLightType ();
        switch ( lightType )
        {
        case COLLADAFW::Light::AMBIENT_LIGHT:
            {
                mayaLight = new MayaDM::AmbientLight ( file, lightName, mayaTransformNode->getNodePath () );
            }
            break;
        case COLLADAFW::Light::DIRECTIONAL_LIGHT:
            {
                mayaLight = new MayaDM::DirectionalLight ( file, lightName, mayaTransformNode->getNodePath () );
            }
            break;
        case COLLADAFW::Light::POINT_LIGHT:
            {
                mayaLight = new MayaDM::PointLight ( file, lightName, mayaTransformNode->getNodePath () );
                setPointLightAttributes ( light, mayaLight );
            }
            break;
        case COLLADAFW::Light::SPOT_LIGHT:
            {
                mayaLight = new MayaDM::SpotLight ( file, lightName, mayaTransformNode->getNodePath () );
                setSpotLightAttributes ( light, mayaLight );
            }
            break;
        default:
            std::cerr << "Unknown light type!" << std::endl;
            return;
        }

        // Add the original id attribute.
        String colladaId = light->getOriginalId ();
        if ( !colladaId.empty () )
        {
            MayaDM::addAttr ( file, COLLADA_ID_ATTRIBUTE_NAME, ATTRIBUTE_DATA_TYPE, ATTRIBUTE_TYPE_STRING );
            MayaDM::setAttr ( file, COLLADA_ID_ATTRIBUTE_NAME, ATTRIBUTE_TYPE, ATTRIBUTE_TYPE_STRING, colladaId );
        }
//         // TODO Add the attributes for all the extra tags.
//         setExtraData ( light->getExtraDataArray () );

        // Set the standard light color.
        COLLADAFW::Color color = light->getColor ();
        mayaLight->setColor ( MayaDM::float3 ( (float)color.getRed (), (float)color.getGreen (), (float)color.getBlue () ) );
        
        // Store the light in a map.
        appendLight ( lightId, mayaLight );
    }
Example #9
0
URL::URL(const String& sUrl) // default for all data members is okay.
{
	String sURL = sUrl;
	sURL.trim();

	// get the scheme
	size_t iBeginIndex = 0;
	size_t iEndIndex = sURL.indexOf( "://" );
	if ( iEndIndex != String::npos )
	{
		if ( iEndIndex > 0 )
		{
			scheme = sURL.substring( 0, iEndIndex ).toLowerCase();
		}
		iBeginIndex = iEndIndex + 3;
	}
	// get the userinfo
	iEndIndex = sURL.indexOf( '@', iBeginIndex );
	if ( iEndIndex != String::npos )
	{
		String sNamePass = sURL.substring( iBeginIndex, iEndIndex - iBeginIndex );
		iBeginIndex = sNamePass.indexOf( ':' );
		if ( iBeginIndex != String::npos )
		{
			if ( iBeginIndex > 0 )
			{
				principal = sNamePass.substring( 0, iBeginIndex );
			}
			if ( iBeginIndex < iEndIndex-1 )
			{
				credential = sNamePass.substring( iBeginIndex + 1 );
			}
		}
		else if ( !sNamePass.empty())
		{
			principal = sNamePass;
		}
		iBeginIndex = iEndIndex + 1;
	}
	// get host[:port]
	iEndIndex = sURL.indexOf( '/', iBeginIndex );
	if (iEndIndex == String::npos)
	{
		iEndIndex = sURL.length();
	}

	String hostPort = sURL.substring(iBeginIndex, iEndIndex - iBeginIndex);
	if (hostPort.empty())
	{
		OW_THROW(MalformedURLException, String("Invalid URL: " + sUrl).c_str());
	}
	size_t colonIdx = hostPort.indexOf( ':' );
	if ( colonIdx != String::npos )
	{
		host = hostPort.substring( 0, colonIdx );
		port = hostPort.substring( colonIdx + 1 );
	}
	else
	{
		host = hostPort;
	}
	if (host.empty())
	{
		OW_THROW(MalformedURLException, String("Invalid URL: " + sUrl).c_str());
	}

	// get namespaceName
	iBeginIndex = sURL.indexOf('/', iBeginIndex);
	if ( iBeginIndex != String::npos )
	{
		iEndIndex = sURL.indexOf("/:",  iBeginIndex);
		if (iEndIndex == String::npos)
		{
			iEndIndex = sURL.length();
		}
		namespaceName = sURL.substring( iBeginIndex+1, iEndIndex - (iBeginIndex+1) );

		// get modelPath
		iBeginIndex = sURL.indexOf("/:", iBeginIndex);
		if (iBeginIndex != String::npos)
		{
			modelPath = sURL.substring(iBeginIndex + 2); // this is the last piece, so grab everything to the end.
		}
	}
}
Example #10
0
//-----------------------------------------------------------------------------
// TemplateFileParsor::ParsePrintData
//-----------------------------------------------------------------------------
void TemplateFileParsor::ParsePrintData(String *pstrLine, int iLine)
{
  pstrLine->Trim();
  String strToPrint;
  int iRc = pstrLine->ExtractToken('"', '"', &strToPrint);

  CTemplateProcessor VarProc(CTemplateProcessor::EMPTY_STRING);
  if( m_pVarEval )
    VarProc.AddEvaluator(m_pVarEval);
  CEnvVariableEvaluator EnvVar;
  VarProc.AddEvaluator(&EnvVar);

  // Variable substitution in quoted strings !!
  VarProc.Process(&strToPrint);

  uint uiPos = 0;
  // looking for new line characters.
  for( uiPos = strToPrint.find("\\n"); uiPos != String::npos; uiPos = strToPrint.find("\\n"))
    strToPrint.replace(uiPos, 2, "\n");

  // looking for carriage return characters.
  for( uiPos = strToPrint.find("\\r"); uiPos != String::npos; uiPos = strToPrint.find("\\r"))
    strToPrint.replace(uiPos, 2, "\r");

  // looking for tab characters.
  for( uiPos = strToPrint.find("\\t"); uiPos != String::npos; uiPos = strToPrint.find("\\t"))
    strToPrint.replace(uiPos, 2, "\t");

  if( 1 == iRc )
  {
    while( !strToPrint.empty() )
    {
      String strTxt, strTmp;
      // Search for next format directive
      bool bParse = true;
      while( bParse )
      {
        iRc = strToPrint.ExtractToken('%', &strTmp);
        // Skip directive indicator if it's a '%%' pair
        if( strToPrint.StartWith('%') )
        {
          strTmp += '%';
          strToPrint.erase(0, 1);
        }
        else
          bParse = false;
        strTxt += strTmp;
      }

      if( !strTxt.empty() )
      {
        // Text to print before format directive
        TemplateTokenPtr ptrToken(new TemplateToken);
        ptrToken->m_iTemplateLine = iLine;
        ptrToken->m_TokenType = TemplateToken::PRINT_TEXT;
        ptrToken->m_strData = strTxt;
        ptrToken->m_Format.Set("%s");
        m_vecToken.push_back(ptrToken);
      }

      if( 1 == iRc )
      {
        // A format directive was found
        SuppressComments(pstrLine);
        String strArgument;
        int iRc2 = pstrLine->ExtractToken(',', &strArgument);
        if( 0 == iRc2 )
        {
          cerr << "Error: Argument missing at line " << iLine << " in file " << m_strCurrentTemplateFile << "." << endl;
          exit(1);
        }
        // Remove blanks
        strArgument.Trim();

        // Check argument type
        if( strArgument.Match("[nxs:*<*>]*") )
        {
          // This is a array type argument
          // Erase '[' and ']' enclosers
          strArgument.erase(0, 1); // '['
          uint uiMatchingPos = strArgument.find(']');
          strArgument.erase(uiMatchingPos, 1); // ']'

          // Insert a loop/end-loop couple
          TemplateTokenPtr ptrLoopToken(new TemplateToken);
          ptrLoopToken->m_iTemplateLine = iLine;
          ptrLoopToken->m_TokenType = TemplateToken::LOOP_OVER;
          ptrLoopToken->m_strParam1 = "loopvar";

          // Extract loop part of the argument
          String strLastPart;
          int iLastPart = strArgument.rfind('>');
          strLastPart = strArgument.substr(iLastPart+1);
          ptrLoopToken->m_strParam2 = strArgument.substr(0, iLastPart+1);
          ptrLoopToken->m_iEndBlockPos = m_vecToken.size() + 1;
          // Add 'loop' Token
          m_vecToken.push_back(ptrLoopToken);

          // Replace loop element with variable access
          String strTmp;
          strArgument.ExtractTokenRight('<', '>', &strTmp);
          strArgument += "$(loopvar_name)" + strLastPart;
        }
        else if( strArgument.Match("*[$(*)]*") )
        {
          uint uiMatchingPos = strArgument.find("[$(");
          uint uiLastPos = strArgument.find("]", uiMatchingPos);
          String strBeforePattern = strArgument.substr(0, uiMatchingPos);
          String strAfterPattern = strArgument.substr(uiLastPos + 1);
          String strMatchedPattern = strArgument.substr(uiMatchingPos + 1, uiLastPos - uiMatchingPos - 1);

          // Insert a loop/end-loop couple
          TemplateTokenPtr ptrLoopToken(new TemplateToken);
          ptrLoopToken->m_iTemplateLine = iLine;
          ptrLoopToken->m_TokenType = TemplateToken::LOOP_OVER;
          ptrLoopToken->m_strParam1 = "loopvar";

          ptrLoopToken->m_strParam2 = strMatchedPattern;
          ptrLoopToken->m_iEndBlockPos = m_vecToken.size() + 1;
          // Add 'loop' Token
          m_vecToken.push_back(ptrLoopToken);
          // Rebuild argument for next token (inside loop)
          strArgument = strBeforePattern + "$(loopvar_name)" + strAfterPattern;
        }

        TemplateTokenPtr ptrToken(new TemplateToken);
        ptrToken->m_iTemplateLine = iLine;
        ptrToken->m_TokenType = TemplateToken::PRINT_DATA;

        if( false == BuildDataFragments(ptrToken.ObjectPtr(), strArgument) )
          // If no variable are presents in the argument string then fill strData member
          ptrToken->m_strData = strArgument;
        
        // Extend format string up to next format directive
        int iPos = strToPrint.find('%');
        // Parse format
        ptrToken->m_Format.Set('%' + strToPrint.substr(0, iPos));
        // Search for format type in first 10 characters
        DataBuf::Type eType = GetTypeFromFormat(ptrToken->m_Format.Type(), ptrToken->m_Format.Modifier());
        if( DataBuf::NO_TYPE == eType )
        {
          cerr << "Error: Bad type specification at line " << iLine << " in file " << m_strCurrentTemplateFile << "." << endl;
          exit(1);
        }
        ptrToken->m_eOutputType = eType;
        ptrToken->m_strPrintFmt = ptrToken->m_Format.Get();

        m_vecToken.push_back(ptrToken);
        strToPrint.erase(0, iPos);
      }
    }
  }
  else
  {
    cerr << "Error: Missed '\"' at line " << iLine << " in file " << m_strCurrentTemplateFile << "." << endl;
    exit(1);
  }
}
Example #11
0
int main(int argc, char** argv)
{
    try
    {
        Params params(argc, argv);

        String modelFile = params("embed");
        String trainFile = params("train");
        String evalFile = params("eval");
        const double Lambda = 1e-4;
        const double Eta = 0.1;

        int epochs = valueOf<int>(params("epochs", "5"));

        std::cout << "Model file: " << modelFile << std::endl;

        SumEmbeddedDatasetReader reader(modelFile);

        long l0 = sgdtk::currentTimeSeconds();

        auto trainingSet = reader.load(trainFile);

#ifdef __DEBUG
        // Shuffle deterministically
        WeightHacks::shuffle(trainingSet);
#else
        std::random_shuffle(trainingSet.begin(), trainingSet.end());
#endif
        double elapsed = sgdtk::currentTimeSeconds() - l0;
        std::cout << "Training data ("  << trainingSet.size() << " examples) + loaded in " <<  elapsed <<  "s" << std::endl;

        trainingSet.resize(30000);

        std::vector<FeatureVector*> evalSet;

        if (!evalFile.empty())
        {

            evalSet = reader.load(evalFile);

#ifdef __DEBUG
            // Shuffle deterministically
            WeightHacks::shuffle(evalSet);
#else
            std::random_shuffle(evalSet.begin(), evalSet.end());
#endif
            evalSet.resize(3590);
        }

        int vSz = reader.getLargestVectorSeen();
        Learner* learner = nullptr;
        if (params("type", "nbow") == "nbow")
        {
            learner =
                    new SGDLearner(new LogLoss, Lambda, Eta, new NeuralNetModelFactory<>({
                            new FullyConnectedLayer(100, vSz), new TanhLayer(), new FullyConnectedLayer(1, 100), new TanhLayer()
                    }));
        }

        else
        {
            learner =
                    new SGDLearner(new LogLoss(), Lambda, Eta, new LinearModelFactory());
        }

        Model* model = learner->create(&vSz);
        double totalTrainingElapsed = 0.;

        for (int i = 0; i < epochs; ++i)
        {
            std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
            std::cout << "EPOCH: " << (i + 1) << std::endl;

            Metrics metrics;
            double t0 = sgdtk::currentTimeSeconds();

            learner->trainEpoch(model, trainingSet);//trainingSet.subList(0, 1000));
            double elapsedThisEpoch = sgdtk::currentTimeSeconds() - t0;
            std::cout << "Epoch training time " << elapsedThisEpoch << "s" << std::endl;
            totalTrainingElapsed += elapsedThisEpoch;

            learner->eval(model, trainingSet, metrics);
            showMetrics(metrics, "Training Set Eval Metrics");
            metrics.clear();

            if (!evalSet.empty())
            {
                //evaluate(evalSet, model);
                learner->eval(model, evalSet, metrics);
                showMetrics(metrics, "Test Set Eval Metrics");
                metrics.clear();
            }

        }

        std::cout << "Total training time " << totalTrainingElapsed << "s" << std::endl;

        for (auto x : trainingSet)
        {
            delete x;
        }
        for (auto x : evalSet)
        {
            delete x;
        }
        delete learner;
        delete model;
    }


    catch (Exception& ex)
    {
        std::cout << "Exception: " << ex.getMessage() << std::endl;
    }

    return 0;
}
Example #12
0
void PerfPrep::Program_Control (void)
{
	String key;
	Strings list;
	Str_Itr str_itr;

	//---- open network files ----

	Data_Service::Program_Control ();
	
	Read_Select_Keys ();
	Read_Flow_Time_Keys ();

	Print (2, String ("%s Control Keys:") % Program ());

	new_file_flag = System_File_Flag (NEW_PERFORMANCE);

	//---- open the merge performance ----
	
	key = Get_Control_String (MERGE_PERFORMANCE_FILE);

	if (!key.empty ()) {
		merge_file.File_Type ("Merge Performance File");
		Print (1);

		if (Check_Control_Key (MERGE_PERFORMANCE_FORMAT)) {
			merge_file.Dbase_Format (Get_Control_String (MERGE_PERFORMANCE_FORMAT));
		}
		merge_file.Open (Project_Filename (key));
		merge_flag = true;

		if (!new_file_flag) {
			Error ("A New Performance File is required for Merge Processing");
		}
	}

	//---- open the base performance ----
	
	key = Get_Control_String (BASE_PERFORMANCE_FILE);

	if (!key.empty ()) {
		base_file.File_Type ("Base Performance File");
		Print (1);

		if (Check_Control_Key (BASE_PERFORMANCE_FORMAT)) {
			base_file.Dbase_Format (Get_Control_String (BASE_PERFORMANCE_FORMAT));
		}
		base_file.Open (Project_Filename (key));
		base_flag = true;

		if (!new_file_flag) {
			Error ("A New Performance File is required for Base Processing");
		}
	}

	//---- open the merge turn delay ----

	turn_flag = System_File_Flag (TURN_DELAY);

	if (turn_flag) {
		key = Get_Control_String (MERGE_TURN_DELAY_FILE);

		if (!key.empty ()) {
			turn_file.File_Type ("Merge Turn Delay File");
			Print (1);

			if (Check_Control_Key (MERGE_TURN_DELAY_FORMAT)) {
				turn_file.Dbase_Format (Get_Control_String (MERGE_TURN_DELAY_FORMAT));
			}
			turn_file.Open (Project_Filename (key));
			turn_merge_flag = true;

			if (!System_File_Flag (NEW_TURN_DELAY)) {
				Error ("A New Turn Delay File is required for Merge Processing");
			}
		}
	}

	//---- processing method ----

	Print (1);
	key = Get_Control_Text (PROCESSING_METHOD);

	if (!key.empty ()) {
		method = Combine_Code (key);

		if (method != UPDATE_TIMES) {
			if (merge_flag || turn_merge_flag) {
				if (method == WEIGHTED_LINK_AVG || method == REPLACE_AVERAGE) {

					//---- merge weighting factor ----

					factor = Get_Control_Double (MERGE_WEIGHTING_FACTOR);
				}
			} else {
				//Error (String ("Processing Method %s requires Merge Files") % key);
			}
		}
	}

	//---- read the smoothing parameters ----

	smooth_flag = smooth_data.Read_Control ();

	if (smooth_flag) {
		if (!smooth_data.Num_Input (time_periods.Num_Periods ())) {
			Error ("Smooth Parameters are Illogical");
		}
		if (!new_file_flag) {
			Error ("A New Performance File is required for Data Smoothing");
		}
	}

	//---- set min travel time ----

	min_time_flag = Get_Control_Flag (SET_MIN_TRAVEL_TIME);

	if (min_time_flag && !new_file_flag) {
		Error ("A New Performance File is required for Minimum Travel Times");
	}

	//---- set merge transit data ----

	key = Get_Control_Text (MERGE_TRANSIT_DATA);

	if (!key.empty ()) {
		transit_flag = true;

		if (!System_File_Flag (TRANSIT_STOP) || !System_File_Flag (TRANSIT_ROUTE) ||
			!System_File_Flag (TRANSIT_SCHEDULE) || !System_File_Flag (TRANSIT_DRIVER)) {

			Error ("Transit Network Files are Required for Transit Loading");
		}
		if (!new_file_flag) {
			Error ("A New Performance File is required to Merge Transit Data");
		}
		key.Parse (list);
		for (str_itr = list.begin (); str_itr != list.end (); str_itr++) {
			if (str_itr->Starts_With ("VEH")) {
				transit_veh_flag = true;
			} else if (str_itr->Starts_With ("PER")) {
				transit_person_flag = true;
			} else if (str_itr->Starts_With ("PCE") || str_itr->Starts_With ("CAR_EQ")) {
				transit_pce_flag = true;
			}
		}
		if (!System_File_Flag (VEHICLE_TYPE) && transit_pce_flag) {
			Warning ("Vehicle Type File is Required for Transit PCE Loading");
		}
		if (!System_File_Flag (RIDERSHIP) && transit_person_flag) {
			Warning ("Ridership File is Required for Transit Person Loading");
		}
		if (!merge_flag) {
			method = Combine_Code (Get_Control_Text (PROCESSING_METHOD));
		}
	}

	//---- open the time constraint file ----
	
	key = Get_Control_String (TIME_CONSTRAINT_FILE);

	if (!key.empty ()) {
		constraint_file.File_Type ("Time Constraint File");
		Print (1);

		if (Check_Control_Key (TIME_CONSTRAINT_FORMAT)) {
			constraint_file.Dbase_Format (Get_Control_String (TIME_CONSTRAINT_FORMAT));
		}
		constraint_file.Open (Project_Filename (key));
		constraint_flag = true;
	}

	//---- open the time ratio file ----
	
	key = Get_Control_String (NEW_TIME_RATIO_FILE);

	if (!key.empty ()) {
		time_ratio_file.File_Type ("New Time Ratio File");

		time_ratio_file.Create (Project_Filename (key));
		time_ratio_flag = true;
	}

	//---- open the deleted record file ----
	
	key = Get_Control_String (NEW_DELETED_RECORD_FILE);

	if (!key.empty ()) {
		deleted_file.File_Type ("New Deleted Record File");
		Print (1);

		if (Check_Control_Key (NEW_DELETED_RECORD_FORMAT)) {
			deleted_file.Dbase_Format (Get_Control_String (NEW_DELETED_RECORD_FORMAT));
		}
		deleted_file.Create (Project_Filename (key));
		del_file_flag = true;
	}

	List_Reports ();

	first_delete = deleted_flag = (Report_Flag (DELETED_RECORDS) || del_file_flag);
}
Example #13
0
Array FrameInjection::getStackFrame(bool withSelf, bool withThis) {
  Array frame = Array::Create();

  if (m_prev) {
    String file = m_prev->getFileName();
    if (!file.empty() && m_prev->m_line) {
      frame.set(s_file, file, true);
      frame.set(s_line, m_prev->m_line, true);
    }
  } else if (m_flags & PseudoMain) {
    // Stop at top, don't include top file
    return Array();
  }

  if (m_flags & PseudoMain) {
    frame.set(s_function, "include", true);
    frame.set(s_args, Array::Create(getFileName()), true);
  } else {
    const char *c = strstr(m_name, "::");
    const char *f = m_name;
    if (c) {
      f = c + 2;
      frame.set(s_class, getClassName()->copy(), true);
      if (ObjectData *obj = getObjectV()) {
        if (withThis) {
          frame.set(s_object, Object(obj), true);
        }
        frame.set(s_type, "->", true);
      } else {
        frame.set(s_type, "::", true);
      }
    }
    ASSERT(f);
    switch (f[0]) {
    case ParserBase::CharClosure:
      frame.set(s_function, s_closureBrackets, true);
      break;
    case ParserBase::CharContinuationFromClosure:
      frame.set(s_function, s_closureGenBrackets, true);
      break;
    default:
      if (const char *c = strstr(f, "$$")) {
        frame.set(s_function, String(f, c - f, CopyString), true);
      } else {
        if (isEvalFrame()) {
          frame.set(s_function, String(f, CopyString), true);
        } else {
          frame.set(s_function, f, true);
        }
      }
      break;
    }

    Array args = getArgs();
    if (!args.isNull()) {
      frame.set(s_args, args, true);
    } else {
      frame.set(s_args, Array::Create(), true);
    }
  }
  return frame;
}
Example #14
0
EditGameDialog::EditGameDialog(const String &domain, const String &desc)
	: OptionsDialog(domain, "GameOptions") {
	// Retrieve all game specific options.
	const EnginePlugin *plugin = 0;
	// To allow for game domains without a gameid.
	// TODO: Is it intentional that this is still supported?
	String gameId(ConfMan.get("gameid", domain));
	if (gameId.empty())
		gameId = domain;
	// Retrieve the plugin, since we need to access the engine's MetaEngine
	// implementation.
	EngineMan.findGame(gameId, &plugin);
	if (plugin) {
		_engineOptions = (*plugin)->getExtraGuiOptions(domain);
	} else {
		warning("Plugin for target \"%s\" not found! Game specific settings might be missing", domain.c_str());
	}

	// GAME: Path to game data (r/o), extra data (r/o), and save data (r/w)
	String gamePath(ConfMan.get("path", _domain));
	String extraPath(ConfMan.get("extrapath", _domain));
	String savePath(ConfMan.get("savepath", _domain));

	// GAME: Determine the description string
	String description(ConfMan.get("description", domain));
	if (description.empty() && !desc.empty()) {
		description = desc;
	}

	// GUI:  Add tab widget
	TabWidget *tab = new TabWidget(this, "GameOptions.TabWidget");

	//
	// 1) The game tab
	//
	tab->addTab(_("Game"));

	// GUI:  Label & edit widget for the game ID
	if (g_system->getOverlayWidth() > 320)
		new StaticTextWidget(tab, "GameOptions_Game.Id", _("ID:"), _("Short game identifier used for referring to saved games and running the game from the command line"));
	else
		new StaticTextWidget(tab, "GameOptions_Game.Id", _c("ID:", "lowres"), _("Short game identifier used for referring to saved games and running the game from the command line"));
	_domainWidget = new DomainEditTextWidget(tab, "GameOptions_Game.Domain", _domain, _("Short game identifier used for referring to saved games and running the game from the command line"));

	// GUI:  Label & edit widget for the description
	if (g_system->getOverlayWidth() > 320)
		new StaticTextWidget(tab, "GameOptions_Game.Name", _("Name:"), _("Full title of the game"));
	else
		new StaticTextWidget(tab, "GameOptions_Game.Name", _c("Name:", "lowres"), _("Full title of the game"));
	_descriptionWidget = new EditTextWidget(tab, "GameOptions_Game.Desc", description, _("Full title of the game"));

	// Language popup
	_langPopUpDesc = new StaticTextWidget(tab, "GameOptions_Game.LangPopupDesc", _("Language:"), _("Language of the game. This will not turn your Spanish game version into English"));
	_langPopUp = new PopUpWidget(tab, "GameOptions_Game.LangPopup", _("Language of the game. This will not turn your Spanish game version into English"));
	_langPopUp->appendEntry(_("<default>"), (uint32)Common::UNK_LANG);
	_langPopUp->appendEntry("", (uint32)Common::UNK_LANG);
	const Common::LanguageDescription *l = Common::g_languages;
	for (; l->code; ++l) {
		if (checkGameGUIOptionLanguage(l->id, _guioptionsString))
			_langPopUp->appendEntry(l->description, l->id);
	}

	// Platform popup
	if (g_system->getOverlayWidth() > 320)
		_platformPopUpDesc = new StaticTextWidget(tab, "GameOptions_Game.PlatformPopupDesc", _("Platform:"), _("Platform the game was originally designed for"));
	else
		_platformPopUpDesc = new StaticTextWidget(tab, "GameOptions_Game.PlatformPopupDesc", _c("Platform:", "lowres"), _("Platform the game was originally designed for"));
	_platformPopUp = new PopUpWidget(tab, "GameOptions_Game.PlatformPopup", _("Platform the game was originally designed for"));
	_platformPopUp->appendEntry(_("<default>"));
	_platformPopUp->appendEntry("");
	const Common::PlatformDescription *p = Common::g_platforms;
	for (; p->code; ++p) {
		_platformPopUp->appendEntry(p->description, p->id);
	}

	//
	// 2) The engine tab (shown only if there are custom engine options)
	//
	if (_engineOptions.size() > 0) {
		tab->addTab(_("Engine"));

		addEngineControls(tab, "GameOptions_Engine.", _engineOptions);
	}

	//
	// 3) The graphics tab
	//
	_graphicsTabId = tab->addTab(g_system->getOverlayWidth() > 320 ? _("Graphics") : _("GFX"));

	if (g_system->getOverlayWidth() > 320)
		_globalGraphicsOverride = new CheckboxWidget(tab, "GameOptions_Graphics.EnableTabCheckbox", _("Override global graphic settings"), 0, kCmdGlobalGraphicsOverride);
	else
		_globalGraphicsOverride = new CheckboxWidget(tab, "GameOptions_Graphics.EnableTabCheckbox", _c("Override global graphic settings", "lowres"), 0, kCmdGlobalGraphicsOverride);

	addGraphicControls(tab, "GameOptions_Graphics.");

	//
	// 4) The audio tab
	//
	tab->addTab(_("Audio"));

	if (g_system->getOverlayWidth() > 320)
		_globalAudioOverride = new CheckboxWidget(tab, "GameOptions_Audio.EnableTabCheckbox", _("Override global audio settings"), 0, kCmdGlobalAudioOverride);
	else
		_globalAudioOverride = new CheckboxWidget(tab, "GameOptions_Audio.EnableTabCheckbox", _c("Override global audio settings", "lowres"), 0, kCmdGlobalAudioOverride);

	addAudioControls(tab, "GameOptions_Audio.");
	addSubtitleControls(tab, "GameOptions_Audio.");

	//
	// 5) The volume tab
	//
	if (g_system->getOverlayWidth() > 320)
		tab->addTab(_("Volume"));
	else
		tab->addTab(_c("Volume", "lowres"));

	if (g_system->getOverlayWidth() > 320)
		_globalVolumeOverride = new CheckboxWidget(tab, "GameOptions_Volume.EnableTabCheckbox", _("Override global volume settings"), 0, kCmdGlobalVolumeOverride);
	else
		_globalVolumeOverride = new CheckboxWidget(tab, "GameOptions_Volume.EnableTabCheckbox", _c("Override global volume settings", "lowres"), 0, kCmdGlobalVolumeOverride);

	addVolumeControls(tab, "GameOptions_Volume.");

	//
	// 6) The MIDI tab
	//
	_globalMIDIOverride = NULL;
	if (!_guioptions.contains(GUIO_NOMIDI)) {
		tab->addTab(_("MIDI"));

		if (g_system->getOverlayWidth() > 320)
			_globalMIDIOverride = new CheckboxWidget(tab, "GameOptions_MIDI.EnableTabCheckbox", _("Override global MIDI settings"), 0, kCmdGlobalMIDIOverride);
		else
			_globalMIDIOverride = new CheckboxWidget(tab, "GameOptions_MIDI.EnableTabCheckbox", _c("Override global MIDI settings", "lowres"), 0, kCmdGlobalMIDIOverride);

		addMIDIControls(tab, "GameOptions_MIDI.");
	}

	//
	// 7) The MT-32 tab
	//
	_globalMT32Override = NULL;
	if (!_guioptions.contains(GUIO_NOMIDI)) {
		tab->addTab(_("MT-32"));

		if (g_system->getOverlayWidth() > 320)
			_globalMT32Override = new CheckboxWidget(tab, "GameOptions_MT32.EnableTabCheckbox", _("Override global MT-32 settings"), 0, kCmdGlobalMT32Override);
		else
			_globalMT32Override = new CheckboxWidget(tab, "GameOptions_MT32.EnableTabCheckbox", _c("Override global MT-32 settings", "lowres"), 0, kCmdGlobalMT32Override);

		addMT32Controls(tab, "GameOptions_MT32.");
	}

	//
	// 8) The Paths tab
	//
	if (g_system->getOverlayWidth() > 320)
		tab->addTab(_("Paths"));
	else
		tab->addTab(_c("Paths", "lowres"));

	// These buttons have to be extra wide, or the text will be truncated
	// in the small version of the GUI.

	// GUI:  Button + Label for the game path
	if (g_system->getOverlayWidth() > 320)
		new ButtonWidget(tab, "GameOptions_Paths.Gamepath", _("Game Path:"), 0, kCmdGameBrowser);
	else
		new ButtonWidget(tab, "GameOptions_Paths.Gamepath", _c("Game Path:", "lowres"), 0, kCmdGameBrowser);
	_gamePathWidget = new StaticTextWidget(tab, "GameOptions_Paths.GamepathText", gamePath);

	// GUI:  Button + Label for the additional path
	if (g_system->getOverlayWidth() > 320)
		new ButtonWidget(tab, "GameOptions_Paths.Extrapath", _("Extra Path:"), _("Specifies path to additional data used by the game"), kCmdExtraBrowser);
	else
		new ButtonWidget(tab, "GameOptions_Paths.Extrapath", _c("Extra Path:", "lowres"), _("Specifies path to additional data used by the game"), kCmdExtraBrowser);
	_extraPathWidget = new StaticTextWidget(tab, "GameOptions_Paths.ExtrapathText", extraPath, _("Specifies path to additional data used by the game"));

	_extraPathClearButton = addClearButton(tab, "GameOptions_Paths.ExtraPathClearButton", kCmdExtraPathClear);

	// GUI:  Button + Label for the save path
	if (g_system->getOverlayWidth() > 320)
		new ButtonWidget(tab, "GameOptions_Paths.Savepath", _("Save Path:"), _("Specifies where your saved games are put"), kCmdSaveBrowser);
	else
		new ButtonWidget(tab, "GameOptions_Paths.Savepath", _c("Save Path:", "lowres"), _("Specifies where your saved games are put"), kCmdSaveBrowser);
	_savePathWidget = new StaticTextWidget(tab, "GameOptions_Paths.SavepathText", savePath, _("Specifies where your saved games are put"));

	_savePathClearButton = addClearButton(tab, "GameOptions_Paths.SavePathClearButton", kCmdSavePathClear);

	// Activate the first tab
	tab->setActiveTab(0);
	_tabWidget = tab;

	// Add OK & Cancel buttons
	new ButtonWidget(this, "GameOptions.Cancel", _("Cancel"), 0, kCloseCmd);
	new ButtonWidget(this, "GameOptions.Ok", _("OK"), 0, kOKCmd);
}
Example #15
0
int main( int argc, const char** argv )
{
    CvCapture* capture = 0;
    Mat frame, frameCopy, image;
    const String scaleOpt = "--scale=";
    size_t scaleOptLen = scaleOpt.length();
    const String cascadeOpt = "--cascade=";
    size_t cascadeOptLen = cascadeOpt.length();
    const String nestedCascadeOpt = "--nested-cascade";
    size_t nestedCascadeOptLen = nestedCascadeOpt.length();
    String inputName;

    CascadeClassifier cascade, nestedCascade;
    double scale = 1;

    for( int i = 1; i < argc; i++ )
    {
        if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 )
            cascadeName.assign( argv[i] + cascadeOptLen );
        else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 )
        {
            if( argv[i][nestedCascadeOpt.length()] == '=' )
                nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 );
            if( !nestedCascade.load( nestedCascadeName ) )
                cerr << "WARNING: Could not load classifier cascade for nested objects" << endl;
        }
        else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 )
        {
            if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale < 1 )
                scale = 1;
        }
        else if( argv[i][0] == '-' )
        {
            cerr << "WARNING: Unknown option %s" << argv[i] << endl;
        }
        else
            inputName.assign( argv[i] );
    }


    IplImage* motion = 0;


    if( !cascade.load( cascadeName ) )
    {
        cerr << "ERROR: Could not load classifier cascade" << endl;
        cerr << "Usage: facedetect [--cascade=\"<cascade_path>\"]\n"
            "   [--nested-cascade[=\"nested_cascade_path\"]]\n"
            "   [--scale[=<image scale>\n"
            "   [filename|camera_index]\n" ;
        return -1;
    }

    if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') )
        capture = cvCaptureFromCAM( inputName.empty() ? 0 : inputName.c_str()[0] - '0' );
    else if( inputName.size() )
    {
        image = imread( inputName, 1 );
        if( image.empty() )
            capture = cvCaptureFromAVI( inputName.c_str() );
    }
    else
        image = imread( "lena.jpg", 1 );

    cvNamedWindow( "result", 1 );
    cvNamedWindow( "Motion", 1 );

    if( capture )
    {
        for(;;)
        {
            IplImage* iplImg = cvQueryFrame( capture );
            frame = iplImg;


            if( !motion )
            {
                motion = cvCreateImage( cvSize(iplImg->width,iplImg->height), 8, 3 );
                cvZero( motion );
                motion->origin = iplImg->origin;
            }


            if( frame.empty() )
                break;
            if( iplImg->origin == IPL_ORIGIN_TL )
                frame.copyTo( frameCopy );
            else
                flip( frame, frameCopy, 0 );

            detectAndDraw( frameCopy, iplImg, cascade, nestedCascade, scale, motion);

            if( waitKey( 10 ) >= 0 )
                goto _cleanup_;
        }

        waitKey(0);
_cleanup_:
        cvReleaseCapture( &capture );
    }/*
    else
    {
        if( !image.empty() )
        {
            detectAndDraw( image, cascade, nestedCascade, scale, motion );
            waitKey(0);
        }
        else if( !inputName.empty() )
        {
            FILE* f = fopen( inputName.c_str(), "rt" );
            if( f )
            {
                char buf[1000+1];
                while( fgets( buf, 1000, f ) )
                {
                    int len = (int)strlen(buf), c;
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '\0';
                    cout << "file " << buf << endl;
                    image = imread( buf, 1 );
                    if( !image.empty() )
                    {
                        detectAndDraw( image, cascade, nestedCascade, scale, motion );
                        c = waitKey(0);
                        if( c == 27 || c == 'q' || c == 'Q' )
                            break;
                    }
                }
                fclose(f);
            }
        }
    }*/
    cvDestroyWindow( "Motion" );
    cvDestroyWindow("result");

    return 0;
}
//-----------------------------------------------------------------------------
GpuProgramPtr ProgramManager::createGpuProgram(Program* shaderProgram, 
											   ProgramWriter* programWriter,
											   const String& language,
											   const String& profiles,
											   const StringVector& profilesList,
											   const String& cachePath)
{

	

	std::stringstream sourceCodeStringStream;
	_StringHash stringHash;
	uint32 programHashCode;
	String programName;

	// Generate source code.
	programWriter->writeSourceCode(sourceCodeStringStream, shaderProgram);	

	// Generate program hash code.
	programHashCode = static_cast<uint32>(stringHash(sourceCodeStringStream.str()));

	// Generate program name.
	programName = StringConverter::toString(programHashCode);
	
	if (shaderProgram->getType() == GPT_VERTEX_PROGRAM)
	{
		programName += "_VS";
	}
	else if (shaderProgram->getType() == GPT_FRAGMENT_PROGRAM)
	{
		programName += "_FS";
	}

	HighLevelGpuProgramPtr pGpuProgram;

	// Try to get program by name.
	pGpuProgram = HighLevelGpuProgramManager::getSingleton().getByName(programName);

	// Case the program doesn't exist yet.
	if (pGpuProgram.isNull())
	{
		// Create new GPU program.
		pGpuProgram = HighLevelGpuProgramManager::getSingleton().createProgram(programName,
			ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, language, shaderProgram->getType());

		// Case cache directory specified -> create program from file.
		if (cachePath.empty() == false)
		{
			const String  programFileName = cachePath + programName + "." + language;	
			std::ifstream programFile;
			bool		  writeFile = true;


			// Check if program file already exist.
			programFile.open(programFileName.c_str());

			// Case no matching file found -> we have to write it.
			if (!programFile)
			{			
				writeFile = true;
			}
			else
			{
				writeFile = false;
				programFile.close();
			}

			// Case we have to write the program to a file.
			if (writeFile)
			{
				std::ofstream outFile(programFileName.c_str());

				if (!outFile)
					return GpuProgramPtr();

				outFile << sourceCodeStringStream.str();
				outFile.close();
			}

			pGpuProgram->setSourceFile(programFileName);
		}

		// No cache directory specified -> create program from system memory.
		else
		{
			pGpuProgram->setSource(sourceCodeStringStream.str());
		}
		
		
		pGpuProgram->setParameter("entry_point", shaderProgram->getEntryPointFunction()->getName());

		// HLSL program requires specific target profile settings - we have to split the profile string.
		if (language == "hlsl")
		{
			StringVector::const_iterator it = profilesList.begin();
			StringVector::const_iterator itEnd = profilesList.end();
			
			for (; it != itEnd; ++it)
			{
				if (GpuProgramManager::getSingleton().isSyntaxSupported(*it))
				{
					pGpuProgram->setParameter("target", *it);
					break;
				}
			}
		}
		
		pGpuProgram->setParameter("profiles", profiles);
		pGpuProgram->load();
	
		// Case an error occurred.
		if (pGpuProgram->hasCompileError())
		{
			pGpuProgram.setNull();
			return GpuProgramPtr(pGpuProgram);
		}

		// Add the created GPU program to local cache.
		if (pGpuProgram->getType() == GPT_VERTEX_PROGRAM)
		{
			mVertexShaderMap[programName] = pGpuProgram;			
		}
		else if (pGpuProgram->getType() == GPT_FRAGMENT_PROGRAM)
		{
			mFragmentShaderMap[programName] = pGpuProgram;	
		}				
	}
	
	return GpuProgramPtr(pGpuProgram);
}
Example #17
0
void NewFormat::Program_Control (void)
{
	bool type_flag, flat_flag, time_flag;
	String key;

	//---- open the support files ----

	Data_Service::Program_Control ();

	if (Check_Control_Key (VERSION4_PLAN_FILE) && Check_Control_Key (TRIP_SORT_TYPE)) {
		Warning ("Plan Sorting is Limited to Version 5 Plan files");
		Show_Message (1);
	}	
	Print (2, String ("%s Control Keys:") % Program ());

	//---- copy existing fields ----

	copy_flag = Get_Control_Flag (COPY_EXISTING_FIELDS);

	if (copy_flag) {
		if (System_File_Flag (LOCATION) && System_File_Flag (NEW_LOCATION)) {
			Location_File *file = (Location_File *) System_File_Handle (LOCATION);
			new_loc_file = (Location_File *) System_File_Handle (NEW_LOCATION);
			new_loc_file->Add_User_Fields (file);
		}
		if (System_File_Flag (ZONE) && System_File_Flag (NEW_ZONE)) {
			Zone_File *file = (Zone_File *) System_File_Handle (ZONE);
			new_zone_file = (Zone_File *) System_File_Handle (NEW_ZONE);
			new_zone_file->Add_User_Fields (file);
		}
	}

	//---- copy existing configuration ----

	if (System_File_Flag (LINK_DELAY) && System_File_Flag (NEW_LINK_DELAY)) {
		Link_Delay_File *file = (Link_Delay_File *) System_File_Header (LINK_DELAY);
		if (file->Turn_Flag ()) {
			file = (Link_Delay_File *) System_File_Header (NEW_LINK_DELAY);
			file->Clear_Fields ();
			file->Turn_Flag (true);
			file->Set_Nesting (true);
			file->Create_Fields ();
			file->Write_Header ();
		}
	}
	if (System_File_Flag (PERFORMANCE) && System_File_Flag (NEW_PERFORMANCE)) {
		Performance_File *file = (Performance_File *) System_File_Header (PERFORMANCE);
		if (file->Turn_Flag ()) {
			file = (Performance_File *) System_File_Header (NEW_PERFORMANCE);
			file->Clear_Fields ();
			file->Turn_Flag (true);
			file->Set_Nesting (true);
			file->Create_Fields ();
			file->Write_Header ();
		}
	}
	
	//---- flatten output flag ----

	flat_flag = Get_Control_Flag (FLATTEN_OUTPUT_FLAG);

	if (flat_flag) {
		if (System_File_Flag (NEW_SHAPE)) {
			System_File_Header (NEW_SHAPE)->Flatten_File ();
		}
		if (System_File_Flag (NEW_PARKING)) {
			System_File_Header (NEW_PARKING)->Flatten_File ();
		}
		if (System_File_Flag (NEW_SIGNAL)) {
			System_File_Header (NEW_SIGNAL)->Flatten_File ();
		}
		if (System_File_Flag (NEW_TIMING_PLAN)) {
			System_File_Header (NEW_TIMING_PLAN)->Flatten_File ();
		}
		if (System_File_Flag (NEW_PHASING_PLAN)) {
			System_File_Header (NEW_PHASING_PLAN)->Flatten_File ();
		}
		if (System_File_Flag (NEW_TRANSIT_ROUTE)) {
			System_File_Header (NEW_TRANSIT_ROUTE)->Flatten_File ();
		}
		if (System_File_Flag (NEW_TRANSIT_SCHEDULE)) {
			System_File_Header (NEW_TRANSIT_SCHEDULE)->Flatten_File ();
		}
		if (System_File_Flag (NEW_TRANSIT_DRIVER)) {
			System_File_Header (NEW_TRANSIT_DRIVER)->Flatten_File ();
		}
		if (System_File_Flag (NEW_ROUTE_NODES)) {
			System_File_Header (NEW_ROUTE_NODES)->Flatten_File ();
		}
		if (System_File_Flag (NEW_HOUSEHOLD)) {
			System_File_Header (NEW_HOUSEHOLD)->Flatten_File ();
		}
		if (System_File_Flag (NEW_LINK_DELAY)) {
			System_File_Header (NEW_LINK_DELAY)->Flatten_File ();
		}
		if (System_File_Flag (NEW_PERFORMANCE)) {
			System_File_Header (NEW_PERFORMANCE)->Flatten_File ();
		}
		if (System_File_Flag (NEW_PLAN)) {
			System_File_Header (NEW_PLAN)->Flatten_File ();
		}
	}

	//---- toll file ----

	key = Get_Control_String (TOLL_FILE);

	if (!key.empty ()) {
		toll_file.Open (Project_Filename (key));
		toll_flag = true;
	}

	//---- activity file ----

	key = Get_Control_String (ACTIVITY_FILE);

	if (!key.empty ()) {
		activity_file.Open (Project_Filename (key));
		activity_flag = true;

		if (!System_File_Flag (NEW_TRIP)) {
			Error ("A New Trip file is required to convert an Activity File");
		}
		if (!System_File_Flag (VEHICLE)) {
			Error ("A Vehicle file is required to convert an Activity File");
		}
	}

	//---- person file ----

	key = Get_Control_String (PERSON_FILE);

	if (!key.empty ()) {
		person_file.Open (Project_Filename (key));
		person_flag = true;
	}

	//---- snapshot file ----

	key = Get_Control_String (SNAPSHOT_FILE);

	if (!key.empty ()) {
		Print (1);
		snap_file.Open (Project_Filename (key));
		snap_flag = true;
	}

	//---- new snapshot file ----

	key = Get_Control_String (NEW_SNAPSHOT_FILE);

	if (!key.empty ()) {
		new_snap_file.Compress_Flag (Set_Control_Flag (NEW_SNAPSHOT_COMPRESSION));

		if (new_snap_file.Compress_Flag ()) {
			new_snap_file.Dbase_Format (BINARY);
		} else {
			if (Check_Control_Key (NEW_SNAPSHOT_FORMAT)) {
				new_snap_file.Dbase_Format (Get_Control_String (NEW_SNAPSHOT_FORMAT));
			}
			new_snap_file.Location_Flag (snap_file.Location_Flag ());
			new_snap_file.Cell_Flag (snap_file.Cell_Flag ());
			new_snap_file.Status_Flag (snap_file.Status_Flag ());
		}
		new_snap_file.Create (Project_Filename (key));

		Get_Control_Flag (NEW_SNAPSHOT_COMPRESSION);
	} else if (snap_flag) {
		Error ("A New Snapshot File is required for Output");
	}

	//---- version 4 time format ----

	time_units = Units_Code (Get_Control_Text (VERSION4_TIME_FORMAT));
	time_flag = Check_Control_Key (VERSION4_TIME_FORMAT);

	if (time_flag) {
		int num;
		Field_Ptr fld_ptr;

		if (System_File_Flag (TRIP)) {
			Trip_File *file = (Trip_File *) System_File_Base (TRIP);
			if (file->Version () <= 40) {
				num = file->Optional_Field (START_FIELD_NAMES);
				if (num >= 0) {
					fld_ptr = file->Field (num);
					fld_ptr->Units (time_units);
				}
				num = file->Optional_Field (END_FIELD_NAMES);
				if (num >= 0) {
					fld_ptr = file->Field (num);
					fld_ptr->Units (time_units);
				}
				num = file->Optional_Field (DURATION_FIELD_NAMES);
				if (num >= 0) {
					fld_ptr = file->Field (num);
					fld_ptr->Units (time_units);
				}
			}
		}
		if (activity_flag) {
			num = activity_file.Optional_Field ("START_MIN", "START", "START_TIME", "STARTTIME");
			if (num >= 0) {
				fld_ptr = activity_file.Field (num);
				fld_ptr->Units (time_units);
			}
			num = activity_file.Optional_Field ("START_MAX", "START", "START_TIME", "STARTTIME");
			if (num >= 0) {
				fld_ptr = activity_file.Field (num);
				fld_ptr->Units (time_units);
			}
			num = activity_file.Optional_Field ("END_MIN", "END", "END_TIME", "ENDTIME");
			if (num >= 0) {
				fld_ptr = activity_file.Field (num);
				fld_ptr->Units (time_units);
			}
			num = activity_file.Optional_Field ("END_MAX", "END", "END_TIME", "ENDTIME");
			if (num >= 0) {
				fld_ptr = activity_file.Field (num);
				fld_ptr->Units (time_units);
			}
			num = activity_file.Optional_Field ("TIME_MIN", "DURATION");
			if (num >= 0) {
				fld_ptr = activity_file.Field (num);
				fld_ptr->Units (time_units);
			}
			num = activity_file.Optional_Field ("TIME_MAX", "DURATION");
			if (num >= 0) {
				fld_ptr = activity_file.Field (num);
				fld_ptr->Units (time_units);
			}
		}
	}

	//---- plan file ----

	if (Check_Control_Key (VERSION4_PLAN_FILE)) {
		if (System_File_Flag (PLAN)) {
			Error ("A plan file and Version 4 plan file must be processed separateley");
		}
		if (!System_File_Flag (NEW_PLAN)) {
			Error ("A new plan file is required to convert a Version 4 plan file");
		}
		Print (1);
		plan_flag = true;
		old_plan.File_Type ("Version4 Plan File");

		//---- get the list type ----

		key = Get_Control_String (NODE_LIST_PATHS);

		if (!key.empty ()) {
			type_flag = key.Bool ();
		} else {
			type_flag = true;
		}
		old_plan.Node_Based_Flag (type_flag);

		//---- get the traveler scale ----

		key = Get_Control_String (TRAVELER_SCALING_FACTOR);

		if (!key.empty ()) {
			scale = key.Integer ();

			if (scale < 2 || scale > 100) {
				Control_Key_Error (TRAVELER_SCALING_FACTOR, "(2..100)");
			}
			old_plan.Traveler_Scale (scale);
		}

		//---- get the file format ----

		if (Check_Control_Key (VERSION4_PLAN_FORMAT)) {
			old_plan.File_Format (Get_Control_String (VERSION4_PLAN_FORMAT));
		}

		//---- open the file and print the parameters ----

		if (!old_plan.Open (Project_Filename (Get_Control_String (VERSION4_PLAN_FILE)))) {
			File_Error ("Opening Version4 Plan File", old_plan.Filename ());
		}
		Get_Control_Text (VERSION4_PLAN_FORMAT);

		type_flag = old_plan.Node_Based_Flag ();

		if (type_flag) {
			Print (1, "Plan File contains Node List Paths");
			if (!System_File_Flag (NODE) || !System_File_Flag (LINK) || !System_File_Flag (PARKING)) {
				Error ("A Node, Link and Parking Files are Required for Node-Based Plan Files");
			}
			AB_Map_Flag (true);
		} else {
			Print (1, "Plan File contains Link List Paths");
		}
		Get_Control_Integer (TRAVELER_SCALING_FACTOR);

	} else if (snap_flag && snap_file.Version () <= 40) {

		//---- get the traveler scale ----

		scale= Get_Control_Integer (TRAVELER_SCALING_FACTOR);

		if (scale < 2 || scale > 100) {
			Control_Key_Error (TRAVELER_SCALING_FACTOR, "(2..100)");
		}
	}

	//---- Version 4 Route Header and Node files ----

	if (Check_Control_Key (VERSION4_ROUTE_HEADER)) {
		if (!System_File_Flag (NEW_ROUTE_NODES)) {
			Error ("A New Route Nodes File is Required");
		}
		int i, num;
		String field;
		Field_Ptr fld_ptr;

		if (!time_flag) time_units = MINUTES;

		Print (1);
		route_flag = true;

		Route_Nodes_File *file = (Route_Nodes_File *) System_File_Base (NEW_ROUTE_NODES);

		key = Get_Control_String (VERSION4_ROUTE_HEADER);

		route_header.File_Type ("Version4 Route Header");
		route_header.File_ID ("Header");

		route_header.Open (Project_Filename (key));

		for (i=1; i <= 24; i++) {
			field ("HEADWAY_%d") % i;
			num = route_header.Optional_Field (field.c_str ());
			if (num < 0) break;

			fld_ptr = route_header.Field (num);
			fld_ptr->Units (time_units);

			field ("OFFSET_%d") % i;
			num = route_header.Optional_Field (field.c_str ());
			if (num >= 0) {
				fld_ptr = route_header.Field (num);
				fld_ptr->Units (time_units);
			}
			field ("TTIME_%d") % i;
			num = route_header.Optional_Field (field.c_str ());
			if (num >= 0) {
				fld_ptr = route_header.Field (num);
				fld_ptr->Units (time_units);
			}
		}
		route_periods = i - 1;
		if (route_periods != file->Num_Periods ()) {
			file->Num_Periods (route_periods);
			file->Clear_Fields ();
			file->Create_Fields ();
			file->Write_Header ();
		}
		key = Get_Control_String (VERSION4_ROUTE_NODES);

		if (key.empty ()) {
			Error ("A Version4 Route Nodes file is Required");
		}
		route_nodes.File_Type ("Version4 Route Nodes");
		route_nodes.File_ID ("Nodes");

		route_nodes.Open (Project_Filename (key));
	}
}
Example #18
0
int main(int argc, char **argv)
{
    cv::CommandLineParser parser(argc, argv, keys);

    if (parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }

    String modelFile = parser.get<String>("model");
    String imageFile = parser.get<String>("image");

    if (!parser.check())
    {
        parser.printErrors();
        return 0;
    }

    String classNamesFile = parser.get<String>("c_names");
    String resultFile = parser.get<String>("result");

    //! [Create the importer of TensorFlow model]
    Ptr<dnn::Importer> importer;
    try                                     //Try to import TensorFlow AlexNet model
    {
        importer = dnn::createTorchImporter(modelFile);
    }
    catch (const cv::Exception &err)        //Importer can throw errors, we will catch them
    {
        std::cerr << err.msg << std::endl;
    }
    //! [Create the importer of Caffe model]

    if (!importer)
    {
        std::cerr << "Can't load network by using the mode file: " << std::endl;
        std::cerr << modelFile << std::endl;
        exit(-1);
    }

    //! [Initialize network]
    dnn::Net net;
    importer->populateNet(net);
    importer.release();                     //We don't need importer anymore
    //! [Initialize network]

    //! [Prepare blob]
    Mat img = imread(imageFile), input;
    if (img.empty())
    {
        std::cerr << "Can't read image from the file: " << imageFile << std::endl;
        exit(-1);
    }

    cv::Size inputImgSize = cv::Size(512, 512);

    if (inputImgSize != img.size())
        resize(img, img, inputImgSize);       //Resize image to input size

    if(img.channels() == 3)
        cv::cvtColor(img, input, cv::COLOR_BGR2RGB);

    input.convertTo(input, CV_32F, 1/255.0);

    dnn::Blob inputBlob = dnn::Blob::fromImages(input);   //Convert Mat to dnn::Blob image batch
    //! [Prepare blob]

    //! [Set input blob]
    net.setBlob("", inputBlob);        //set the network input
    //! [Set input blob]

    cv::TickMeter tm;
    tm.start();

    //! [Make forward pass]
    net.forward();                          //compute output
    //! [Make forward pass]

    tm.stop();

    //! [Gather output]

    String oBlob = net.getLayerNames().back();
    if (!parser.get<String>("o_blob").empty())
    {
        oBlob = parser.get<String>("o_blob");
    }

    dnn::Blob prob = net.getBlob(oBlob);   //gather output of "prob" layer

    Mat& result = prob.matRef();

    BlobShape shape = prob.shape();

    if (!resultFile.empty()) {
        CV_Assert(result.isContinuous());

        ofstream fout(resultFile.c_str(), ios::out | ios::binary);
        fout.write((char*)result.data, result.total() * sizeof(float));
        fout.close();
    }

    std::cout << "Output blob shape " << shape  << std::endl;
    std::cout << "Inference time, ms: " << tm.getTimeMilli()  << std::endl;

    if (parser.has("show"))
    {
        std::vector<String> classNames;
        if(!classNamesFile.empty()) {
            classNames = readClassNames(classNamesFile.c_str());
            if (classNames.size() > prob.channels())
                classNames = std::vector<String>(classNames.begin() + classNames.size() - prob.channels(),
                                                 classNames.end());
        }
        Mat segm, legend;
        colorizeSegmentation(prob, segm, legend, classNames);

        Mat show;
        addWeighted(img, 0.2, segm, 0.8, 0.0, show);

        imshow("Result", show);
        if(classNames.size())
            imshow("Legend", legend);
        waitKey();
    }

    return 0;
} //main
Example #19
0
void HHVM_FUNCTION(header, const String& str, bool replace /* = true */,
                   int http_response_code /* = 0 */) {
  if (HHVM_FN(headers_sent)()) {
    raise_warning("Cannot modify header information - headers already sent");
  }

  String header = HHVM_FN(rtrim)(str);

  // new line safety check
  if (header.find('\n') >= 0 || header.find('\r') >= 0) {
    raise_error("Header may not contain more than a single header, "
                "new line detected");
    return;
  }

  Transport *transport = g_context->getTransport();
  if (transport && header.size()) {
    const char *header_line = header.data();

    // handle single line of status code
    if ((header.size() >= 5 && strncasecmp(header_line, "HTTP/", 5) == 0) ||
        (header.size() >= 7 && strncasecmp(header_line, "Status:", 7) == 0)) {
      int code = 200;
      const char *reason = nullptr;
      for (const char *ptr = header_line + 5; *ptr; ptr++) {
        if (*ptr == ' ' && *(ptr + 1) != ' ') {
          code = atoi(ptr + 1);
          for (ptr++; *ptr; ptr++) {
            if (*ptr == ' ' && *(ptr + 1) != ' ') {
              reason = ptr + 1;
              break;
            }
          }
          break;
        }
      }
      if (code) {
        transport->setResponse(code, reason);
      }
      return;
    }

    const char *colon_offset = strchr(header_line, ':');
    String newHeader;
    if (colon_offset) {
      if (!strncasecmp(header_line, "Content-Type",
                       colon_offset - header_line)) {
        const char *ptr = colon_offset+1, *mimetype = NULL;
        while (*ptr == ' ') ptr++;
        mimetype = ptr;
        if (strncmp(mimetype, "text/", 5) == 0 &&
            strstr(mimetype, "charset=") == NULL) {
          newHeader = header + ";charset=utf-8";
        }
      }
    }
    if (replace) {
      transport->replaceHeader(newHeader.empty() ? header : newHeader);
    } else {
      transport->addHeader(newHeader.empty() ? header : newHeader);
    }
    if (http_response_code) {
      transport->setResponse(http_response_code);
    }
  }
}
Example #20
0
SSL *SSLSocket::createSSL(SSL_CTX *ctx) {
  ERR_clear_error();

  /* look at options in the stream and set appropriate verification flags */
  if (m_context[s_verify_peer].toBoolean()) {
    /* turn on verification callback */
    SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verifyCallback);

    /* CA stuff */
    String cafile = m_context[s_cafile].toString();
    String capath = m_context[s_capath].toString();

    if (!cafile.empty() || !capath.empty()) {
      if (!SSL_CTX_load_verify_locations(ctx, cafile.data(), capath.data())) {
        raise_warning("Unable to set verify locations `%s' `%s'",
                      cafile.data(), capath.data());
        return nullptr;
      }
    }

    int64_t depth = m_context[s_verify_depth].toInt64();
    if (depth) {
      SSL_CTX_set_verify_depth(ctx, depth);
    }
  } else {
    SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, nullptr);
  }

  /* callback for the passphrase (for localcert) */
  if (!m_context[s_passphrase].toString().empty()) {
    SSL_CTX_set_default_passwd_cb_userdata(ctx, this);
    SSL_CTX_set_default_passwd_cb(ctx, passwdCallback);
  }

  String cipherlist = m_context[s_ciphers].toString();
  if (cipherlist.empty()) {
    cipherlist = "DEFAULT";
  }
  SSL_CTX_set_cipher_list(ctx, cipherlist.data());

  String certfile = m_context[s_local_cert].toString();
  if (!certfile.empty()) {
    String resolved_path_buff = File::TranslatePath(certfile);
    if (!resolved_path_buff.empty()) {
      /* a certificate to use for authentication */
      if (SSL_CTX_use_certificate_chain_file(ctx, resolved_path_buff.data())
          != 1) {
        raise_warning("Unable to set local cert chain file `%s'; Check "
                      "that your cafile/capath settings include details of "
                      "your certificate and its issuer", certfile.data());
        return nullptr;
      }

      if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff.data(),
                                      SSL_FILETYPE_PEM) != 1) {
        raise_warning("Unable to set private key file `%s'",
                      resolved_path_buff.data());
        return nullptr;
      }

      SSL *tmpssl = SSL_new(ctx);
      X509 *cert = SSL_get_certificate(tmpssl);
      if (cert) {
        EVP_PKEY *key = X509_get_pubkey(cert);
        EVP_PKEY_copy_parameters(key, SSL_get_privatekey(tmpssl));
        EVP_PKEY_free(key);
      }
      SSL_free(tmpssl);

      if (!SSL_CTX_check_private_key(ctx)) {
        raise_warning("Private key does not match certificate!");
      }
    }
  }

  SSL *ssl = SSL_new(ctx);
  if (ssl) {
    SSL_set_ex_data(ssl, GetSSLExDataIndex(), this); /* map SSL => stream */
  }
  return ssl;
}
Example #21
0
String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys) {

	String colon = ":";
	String end_statement = "";

	if (!p_indent.empty()) {
		colon += " ";
		end_statement += "\n";
	}

	switch (p_var.get_type()) {

		case Variant::NIL: return "null";
		case Variant::BOOL: return p_var.operator bool() ? "true" : "false";
		case Variant::INT: return itos(p_var);
		case Variant::REAL: return rtos(p_var);
		case Variant::POOL_INT_ARRAY:
		case Variant::POOL_REAL_ARRAY:
		case Variant::POOL_STRING_ARRAY:
		case Variant::ARRAY: {

			String s = "[";
			s += end_statement;
			Array a = p_var;
			for (int i = 0; i < a.size(); i++) {
				if (i > 0) {
					s += ",";
					s += end_statement;
				}
				s += _make_indent(p_indent, p_cur_indent + 1) + _print_var(a[i], p_indent, p_cur_indent + 1, p_sort_keys);
			}
			s += end_statement + _make_indent(p_indent, p_cur_indent) + "]";
			return s;
		};
		case Variant::DICTIONARY: {

			String s = "{";
			s += end_statement;
			Dictionary d = p_var;
			List<Variant> keys;
			d.get_key_list(&keys);

			if (p_sort_keys)
				keys.sort();

			for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {

				if (E != keys.front()) {
					s += ",";
					s += end_statement;
				}
				s += _make_indent(p_indent, p_cur_indent + 1) + _print_var(String(E->get()), p_indent, p_cur_indent + 1, p_sort_keys);
				s += colon;
				s += _print_var(d[E->get()], p_indent, p_cur_indent + 1, p_sort_keys);
			}

			s += end_statement + _make_indent(p_indent, p_cur_indent) + "}";
			return s;
		};
		default: return "\"" + String(p_var).json_escape() + "\"";
	}
}
Example #22
0
bool SSLSocket::applyVerificationPolicy(X509 *peer) {
  /* verification is turned off */
  if (!m_context[s_verify_peer].toBoolean()) {
    return true;
  }

  if (peer == nullptr) {
    raise_warning("Could not get peer certificate");
    return false;
  }

  int err = SSL_get_verify_result(m_data->m_handle);
  switch (err) {
  case X509_V_OK:
    /* fine */
    break;
  case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
    if (m_context[s_allow_self_signed].toBoolean()) {
      /* allowed */
      break;
    }
    /* not allowed, so fall through */
  default:
    raise_warning("Could not verify peer: code:%d %s", err,
                  X509_verify_cert_error_string(err));
    return false;
  }

  /* if the cert passed the usual checks, apply our own local policies now */

  /* Does the common name match ? (used primarily for https://) */
  String cnmatch = m_context[s_CN_match].toString();
  if (!cnmatch.empty()) {
    X509_NAME *name = X509_get_subject_name(peer);
    char buf[1024];
    int name_len = X509_NAME_get_text_by_NID(name, NID_commonName, buf,
                                             sizeof(buf));

    if (name_len < 0) {
      raise_warning("Unable to locate peer certificate CN");
      return false;
    } else if (name_len != (int)strlen(buf)) {
      raise_warning("Peer certificate CN=`%.*s' is malformed", name_len, buf);
      return false;
    }

    bool match = (strcmp(cnmatch.c_str(), buf) == 0);
    if (!match && strlen(buf) > 3 && buf[0] == '*' && buf[1] == '.') {
      /* Try wildcard */
      if (strchr(buf+2, '.')) {
        const char* cnmatch_str = cnmatch.c_str();
        const char *tmp = strstr(cnmatch_str, buf+1);
        match = tmp && strcmp(tmp, buf+2) && tmp == strchr(cnmatch_str, '.');
      }
    }

    if (!match) {
      /* didn't match */
      raise_warning("Peer certificate CN=`%.*s' did not match expected CN=`%s'",
                    name_len, buf, cnmatch.c_str());
      return false;
    }
  }

  return true;
}
Example #23
0
String StringUtil::HtmlEncodeExtra(const String& input, QuoteStyle quoteStyle,
                                   const char *charset, bool nbsp,
                                   Array extra) {
  if (input.empty()) return input;

  assert(charset);
  int flags = STRING_HTML_ENCODE_UTF8;
  if (nbsp) {
    flags |= STRING_HTML_ENCODE_NBSP;
  }
  if (RuntimeOption::Utf8izeReplace) {
    flags |= STRING_HTML_ENCODE_UTF8IZE_REPLACE;
  }
  if (!*charset || strcasecmp(charset, "UTF-8") == 0) {
  } else if (strcasecmp(charset, "ISO-8859-1") == 0) {
    flags &= ~STRING_HTML_ENCODE_UTF8;
  } else {
    throw_not_implemented(charset);
  }

  const AsciiMap *am;
  AsciiMap tmp;

  switch (quoteStyle) {
    case QuoteStyle::FBUtf8Only:
      am = &mapNothing;
      flags |= STRING_HTML_ENCODE_HIGH;
      break;
    case QuoteStyle::FBUtf8:
      am = &mapBothQuotes;
      flags |= STRING_HTML_ENCODE_HIGH;
      break;
    case QuoteStyle::Both:
      am = &mapBothQuotes;
      break;
    case QuoteStyle::Double:
      am = &mapDoubleQuotes;
      break;
    case QuoteStyle::No:
      am = &mapNoQuotes;
      break;
    default:
      am = &mapNothing;
      raise_error("Unknown quote style: %d", (int)quoteStyle);
  }

  if (quoteStyle != QuoteStyle::FBUtf8Only && extra.toBoolean()) {
    tmp = *am;
    am = &tmp;
    for (ArrayIter iter(extra); iter; ++iter) {
      String item = iter.second().toString();
      char c = item.data()[0];
      tmp.map[c & 64 ? 1 : 0] |= 1uLL << (c & 63);
    }
  }

  int len = input.size();
  char *ret = string_html_encode_extra(input.data(), len,
                                       (StringHtmlEncoding)flags, am);
  if (!ret) {
    raise_error("HtmlEncode called on too large input (%d)", len);
  }
  return String(ret, len, AttachString);
}
TextureDefinition::TextureDefinition (const std::string& textureSize, IProgressCallback* progress) :
		_textureSize(textureSize), _cnt(0)
{
	if (textureSize != "big" && textureSize != "small") {
		System.exit("invalid texturesize value given: " + textureSize + ". Valid values are: auto, big, small", 1);
	}

	ExecutionTime e("Texture definition loading for " + textureSize);

	const std::string& path = FS.getTexturesDir();
	const DirectoryEntries& entries = FS.listDirectory(path);
	LUA lua;
	for (DirectoryEntries::const_iterator i = entries.begin(); i != entries.end(); ++i) {
		const std::string filename = path + *i;
		if (!FS.hasExtension(filename, "lua"))
			continue;
		if (!lua.load(filename)) {
			Log::error(LOG_CLIENT, "failed to load textures from %s", filename.c_str());
			continue;
		}
		lua.getGlobalKeyValue("textures" + textureSize);

		while (lua.getNextKeyValue()) {
			String id = lua.getKey();
			if (id.empty()) {
				lua.pop();
				System.exit("Invalid texture entry found - invalid id", 1);
			}

			id = id.cutBeforeLastMatch("/");

			if (progress != nullptr) {
				progress->progressStep();
			}
			const std::string name = lua.getValueStringFromTable("image");
			const float x0 = lua.getValueFloatFromTable("x0");
			const float y0 = lua.getValueFloatFromTable("y0");
			const float x1 = lua.getValueFloatFromTable("x1");
			const float y1 = lua.getValueFloatFromTable("y1");
			const int trimmedWidth = lua.getValueIntegerFromTable("trimmedwidth");
			const int trimmedHeight = lua.getValueIntegerFromTable("trimmedheight");
			const int untrimmedWidth = lua.getValueIntegerFromTable("untrimmedwidth");
			const int untrimmedHeight = lua.getValueIntegerFromTable("untrimmedheight");
			const int trimmedOffsetX = lua.getValueIntegerFromTable("trimmedoffsetx");
			const int trimmedOffsetY = lua.getValueIntegerFromTable("trimmedoffsety");
			const TextureDefinitionTrim trim(trimmedWidth, trimmedHeight,
					untrimmedWidth, untrimmedHeight, trimmedOffsetX, trimmedOffsetY);
			const TextureDefinitionCoords r = { x0, y0, x1, y1 };

			if (id.contains(TEXTURE_DIRECTION)) {
				const String rightID = id.replaceAll(TEXTURE_DIRECTION, TEXTURE_DIRECTION_RIGHT);
				create(name, rightID, r, trim, false);
				const String leftID = id.replaceAll(TEXTURE_DIRECTION, TEXTURE_DIRECTION_LEFT);
				create(name, leftID, r, trim, true);
			} else {
				const bool mirror = lua.getValueBoolFromTable("mirror");
				create(name, id, r, trim, mirror);
			}
			lua.pop();
			++_cnt;
		}
	}

	if (_textureDefs.empty())
		Log::info(LOG_CLIENT, "could not load any texture definition");
	Log::info(LOG_CLIENT, "loaded %i texture definitions", (int)_textureDefs.size());
}
Example #25
0
String StringUtil::UUEncode(const String& input) {
  if (input.empty()) return input;
  return string_uuencode(input.data(), input.size());
}
    //-----------------------------------------------------------------------
    void FileSystemArchive::findFiles(const String& pattern, bool recursive, 
        bool dirs, StringVector* simpleList, FileInfoList* detailList)
    {
        long lHandle, res;
        struct _finddata_t tagData;

        // pattern can contain a directory name, separate it from mask
        size_t pos1 = pattern.rfind ('/');
        size_t pos2 = pattern.rfind ('\\');
        if (pos1 == pattern.npos || ((pos2 != pattern.npos) && (pos1 < pos2)))
            pos1 = pos2;
        String directory;
        if (pos1 != pattern.npos)
            directory = pattern.substr (0, pos1 + 1);

        String full_pattern = concatenate_path(mName, pattern);

        lHandle = _findfirst(full_pattern.c_str(), &tagData);
        res = 0;
        while (lHandle != -1 && res != -1)
        {
            if ((dirs == ((tagData.attrib & _A_SUBDIR) != 0)) &&
				( !ms_IgnoreHidden || (tagData.attrib & _A_HIDDEN) == 0 ) &&
                (!dirs || !is_reserved_dir (tagData.name)))
            {
                if (simpleList)
                {
                    simpleList->push_back(directory + tagData.name);
                }
                else if (detailList)
                {
                    FileInfo fi;
                    fi.archive = this;
                    fi.filename = directory + tagData.name;
                    fi.basename = tagData.name;
                    fi.path = directory;
                    fi.compressedSize = tagData.size;
                    fi.uncompressedSize = tagData.size;
                    detailList->push_back(fi);
                }
            }
            res = _findnext( lHandle, &tagData );
        }
        // Close if we found any files
        if(lHandle != -1)
            _findclose(lHandle);

        // Now find directories
        if (recursive)
        {
            String base_dir = mName;
            if (!directory.empty ())
            {
                base_dir = concatenate_path(mName, directory);
                // Remove the last '/'
                base_dir.erase (base_dir.length () - 1);
            }
            base_dir.append ("/*");

            // Remove directory name from pattern
            String mask ("/");
            if (pos1 != pattern.npos)
                mask.append (pattern.substr (pos1 + 1));
            else
                mask.append (pattern);

            lHandle = _findfirst(base_dir.c_str (), &tagData);
            res = 0;
            while (lHandle != -1 && res != -1)
            {
                if ((tagData.attrib & _A_SUBDIR) &&
					( !ms_IgnoreHidden || (tagData.attrib & _A_HIDDEN) == 0 ) &&
                    !is_reserved_dir (tagData.name))
                {
                    // recurse
                    base_dir = directory;
                    base_dir.append (tagData.name).append (mask);
                    findFiles(base_dir, recursive, dirs, simpleList, detailList);
                }
                res = _findnext( lHandle, &tagData );
            }
            // Close if we found any files
            if(lHandle != -1)
                _findclose(lHandle);
        }
    }
Example #27
0
String StringUtil::StripHTMLTags(const String& input,
                                 const String& allowable_tags /* = "" */) {
  if (input.empty()) return input;
  return string_strip_tags(input.data(), input.size(),
                           allowable_tags.data(), allowable_tags.size(), false);
}
Example #28
0
Builder::Builder(Settings* appCfg, Platform* platform, const String& buildTarget, String outPath)
{
	_appCfg		= appCfg;
	_platform		= platform;

	_platformID	= platform->getPlatformID();
	_buildTarget	= buildTarget;

	_sectionName	= "bundler";
	_platformSectionName = String("bundler/") + _platformID;

	Ref<StreamLocator> locator = appCfg->getSource()->getLocator();

	// Obtain build host
	_buildHost = g_Bundler->getBuildHost();

	// Check if app.cfg is valid
	if (appCfg->get("app_name").empty())
		NIT_THROW_FMT(EX_SYNTAX, "'app_name' not found in '%s'", appCfg->getName());

	if (appCfg->get(_platformSectionName + "/app_compat_ver").empty())
		NIT_THROW_FMT(EX_SYNTAX, "'app_compat_ver' not found in '%s'", _platformSectionName.c_str());

	// Setup out_path
	outPath = _appCfg->get(_sectionName + "/out_path", outPath, false);
	outPath = _appCfg->get(_platformSectionName + "/out_path", outPath, false);

	if (outPath.empty())
		NIT_THROW_FMT(EX_INVALID_PARAMS, "'out_path' not specified");

	if (!FileUtil::isAbsolutePath(outPath))
		outPath = locator->makeUrl(outPath);

	FileUtil::createDir(outPath);
	_outPath = new FileLocator("$out_path", outPath, false, true);
	_outPath->load();

	// Setup dump_path
	String dumpPath = StringUtil::format("%s/%s_dump", outPath.c_str(), buildTarget.c_str());

	FileUtil::createDir(dumpPath);
	_dumpPath = new FileLocator("$dump_path", dumpPath, false, true);
	_dumpPath->load();

	// Obtain revision number from subversion
	String appCfgFilename, appCfgPath;
	StringUtil::splitFilename(_appCfg->getUrl(), appCfgFilename, appCfgPath);
	if (!appCfgPath.empty()) 
		appCfgPath.resize(appCfgPath.size()-1);

	_revision = g_Bundler->svnRevision(appCfgPath);

	if (_revision.empty())
		NIT_THROW_FMT(EX_SYSTEM, "can't get revision from '%s'", appCfgPath.c_str());

	if (_revision.find_first_not_of("0123456789") != _revision.npos)
	{
		LOG(0, "*** warning: '%s' is not up-to-date revision: %s\n", appCfgPath.c_str(), _revision.c_str());

		// ':' is not appropriate letter for a filename
		for (uint i=0; i<_revision.length(); ++i)
			if (_revision[i] == ':')
				_revision[i] = 't';
	}

	// Include files specified by bundler_include
	_packLocator	= new CustomArchive("$pack_root");

	StringVector includes;
	_appCfg->find("bundler/bundler_include", includes);

	for (uint i=0; i<includes.size(); ++i)
	{
		if (_appCfgInclude)
			NIT_THROW_FMT(EX_NOT_SUPPORTED, "only one 'budnler_include' supported yet");

		_appCfgInclude = Settings::load(locator->locate(includes[i]));

		if (_appCfgInclude == NULL)
			NIT_THROW_FMT(EX_NOT_FOUND, "can't include '%s'", includes[i].c_str());
	}

	// Setuup pack_path
	if (_appCfgInclude)
	{
		collectPackLocators(_appCfgInclude->getSection(_sectionName));
		collectPackLocators(_appCfgInclude->getSection(_platformSectionName));
	}

	collectPackLocators(_appCfg->getSection(_sectionName));
	collectPackLocators(_appCfg->getSection(_platformSectionName));

	// Collect packs list
	collect("*");
}
Example #29
0
Variant StringUtil::Explode(const String& input, const String& delimiter,
                            int limit /* = 0x7FFFFFFF */) {
  if (delimiter.empty()) {
    throw_invalid_argument("delimiter: (empty)");
    return false;
  }

  Array ret(Array::Create());

  if (input.empty()) {
    if (limit >= 0) {
      ret.append("");
    }
    return ret;
  }

  if (limit > 1) {
    int pos = input.find(delimiter);
    if (pos < 0) {
      ret.append(input);
    } else {
      int len = delimiter.size();
      int pos0 = 0;
      do {
        ret.append(input.substr(pos0, pos - pos0));
        pos += len;
        pos0 = pos;
      } while ((pos = input.find(delimiter, pos)) >= 0 && --limit > 1);

      if (pos0 <= input.size()) {
        ret.append(input.substr(pos0));
      }
    }
  } else if (limit < 0) {
    int pos = input.find(delimiter);
    if (pos >= 0) {
      std::vector<int> positions;
      int len = delimiter.size();
      int pos0 = 0;
      int found = 0;
      do {
        positions.push_back(pos0);
        positions.push_back(pos - pos0);
        pos += len;
        pos0 = pos;
        found++;
      } while ((pos = input.find(delimiter, pos)) >= 0);

      if (pos0 <= input.size()) {
        positions.push_back(pos0);
        positions.push_back(input.size() - pos0);
        found++;
      }
      unsigned nelems = std::max(found + limit, 0);
      for (unsigned i = 0; i < nelems * 2; i += 2) {
        ret.append(input.substr(positions[i], positions[i+1]));
      }
    } // else we have negative limit and delimiter not found
  } else {
    ret.append(input);
  }

  return ret;
}
Example #30
0
Variant php_mysql_do_connect_on_link(MySQL* mySQL, String server,
                                     String username, String password,
                                     String database, int client_flags,
                                     bool persistent, bool async,
                                     int connect_timeout_ms,
                                     int query_timeout_ms) {
  if (connect_timeout_ms < 0) {
    connect_timeout_ms = mysqlExtension::ConnectTimeout;
  }
  if (query_timeout_ms < 0) {
    query_timeout_ms = MySQL::GetDefaultReadTimeout();
  }
  if (server.empty()) server = MySQL::GetDefaultServer();
  if (username.empty()) username = MySQL::GetDefaultUsername();
  if (password.empty()) password = MySQL::GetDefaultPassword();
  if (database.empty()) database = MySQL::GetDefaultDatabase();

  // server format: hostname[:port][:/path/to/socket]
  // ipv6 hostname:port is of the form [1:2:3:4:5]:port
  String host, socket;
  int port;

  auto slash_pos = server.find('/');
  if (slash_pos != std::string::npos) {
    socket = server.substr(slash_pos);
    server = server.substr(0, slash_pos - 1);
  }

  HostURL hosturl(std::string(server), MySQL::GetDefaultPort());
  if (hosturl.isValid()) {
    host = hosturl.getHost();
    port = hosturl.getPort();
  } else {
    host = server;
    port = MySQL::GetDefaultPort();
  }

  if (socket.empty()) {
    socket = MySQL::GetDefaultSocket();
  }

  if (mySQL == nullptr && persistent) {
    mySQL = MySQL::GetPersistent(host, port, socket, username, password,
                                 client_flags);
  }

  if (mySQL == nullptr) {
    mySQL = new MySQL(host.c_str(), port, username.c_str(), password.c_str(),
                      database.c_str());
  }

  if (mySQL->getState() == MySQLState::INITED) {
    if (async) {
#ifdef FACEBOOK
      if (!mySQL->async_connect(host, port, socket, username, password,
                                database)) {
        MySQL::SetDefaultConn(mySQL); // so we can report errno by mysql_errno()
        mySQL->setLastError("mysql_real_connect_nonblocking_init");
        return false;
      }
#else
      throw NotImplementedException("mysql_async_connect_start");
#endif
    } else {
      if (!mySQL->connect(host, port, socket, username, password,
                          database, client_flags, connect_timeout_ms)) {
        MySQL::SetDefaultConn(mySQL); // so we can report errno by mysql_errno()
        mySQL->setLastError("mysql_connect");
        return false;
      }
    }
  } else {
    if (!mySQL->reconnect(host, port, socket, username, password,
                          database, client_flags, connect_timeout_ms)) {
      MySQL::SetDefaultConn(mySQL); // so we can report errno by mysql_errno()
      mySQL->setLastError("mysql_connect");
      return false;
    }
  }

  if (persistent) {
    MySQL::SetPersistent(host, port, socket, username, password,
                         client_flags, mySQL);
  }
  MySQL::SetDefaultConn(mySQL);
  return Resource(mySQL);
}