Quillion
Quillion

Reputation: 6476

Using php to Auto generate C++ code

SCROLL DOWN if you just want to see the question without much explanation

I have kind of a big application with a lot of values that are used as options. The control for the application is a website, application itself is C++ and all the options are stored inside MySQL so that both C++ and php can access it. So I am using php to read bunch of values from a text file, and auto generate SQL, C++ and php code, since the options are used everywhere and are so massive that it would take too long to implement them, and would be too much of a hassle to add an extra options in the future.

This is what one line of my text file looks like

`input_video_standard`|:|INT|:|NOT NULL DEFAULT '0'|:|profile->input.videoStandard|:|Video Standard|:|SD 576i@50Hz (B,G/PAL)|:|SD [email protected] (NTSC)|:|HD 720p@50Hz|:|HD [email protected]|:|HD 1080i@25Hz|:|HD [email protected]

Once I read in that line I use

$pieces = explode("|:|", $line);

This part is for generating C++ code now I have an array called variables, where I store name of the variable for C++ and what type it is.

$variables[] = array($pieces[1], $pieces[3]);

Once I loop through the text file and gather up all the data

I try to create a string which I will later on store inside a text file

$cpp .= "memset(prof,0,sizeof(prof));\n";
$cpp .= "sprintf(prof, \"CALL put_into_input(%d, 'test'";
for($i = 3; $i<count($variables); $i++)
{
    if(strpos($variables[0], "VARCHAR") === FALSE)
    {
        $cpp .= ", %d";
    }
    else
    {
        $cpp .= ", '%s'";
    }
}
$cpp .= "\nprofile-".">"."pfile";
for($i = 3; $i<count($variables); $i++)
{
    $cpp .= ",\n".$variables[1];
}
$cpp .= ");\n";

THE QUESTION

The problem is that when I try to concatenate string profile->input.videoStandard to my main string it simply saves it as Array, and I have noticed that php does not like -> as a string in general, which is why I used "-".">" wherever I could. How do I go about getting php of accepting -> as a string?

Thanks to anyone for their help.

Upvotes: 0

Views: 518

Answers (3)

PeeHaa
PeeHaa

Reputation: 72729

I have noticed that php does not like -> as a string in general.

Nothing wrong with a -> in a string.

Unless there is some $ in there too.

This will cause PHP to think you are trying to access a property of an object instance.

E.g. $object->something. You could just use single quotes in stead of double quotes to prevent PHP trying to parse the string. Other than that there shouldn't be any problem using -> in a string.

EDIT

You're code looks strange to me.

You are doing $variables[] = array($pieces[1], $pieces[3]);

which is the same as $variables = array(array($pieces[1], $pieces[3]));

So you only have $variables[0] and not $variables[1]

Upvotes: 2

Tim S.
Tim S.

Reputation: 13853

In PHP, variables within double quotes are parsed.

$var = "hello";
$str = "This is a variable: $var";

// The value of $str will be "This is a variable: hello"

When you use single quotes variables are not parsed.

$var = "hello";
$str = 'This is a variable: $var'; // Please note the single quotes here

// The value of $str will be "This is a variable: $var"

I think in your case, the -> are recognised as variables because that's the syntax used for object-oriented programming to get both properties and methods.

$object->property;
$object->method();

Personally I use single quotes whenever I don't need my string to be parsed for variables or regular expressions.

Single quote strings

Double quote strings

Upvotes: 1

Nanocom
Nanocom

Reputation: 3726

You could use apostrophes intead of quotes maybe ?

Upvotes: -2

Related Questions