Libxml2: Outputting XML element with attribute and content - c

I'm using the libxml2 XMLTextWriter API (of which an official example is provided here) to output XML, but can't find any examples or see how to produce an element with both attributes and content, like so:
<MyElement myAttrib="x">Content</MyElement>
Surprisingly, I'm not seeing any questions on SO that address this. Maybe because people just output XML themselves rather than using a library.
The C code I have so far is:
if (xmlTextWriterStartElement(writer, BAD_CAST "MyElement") < 0
|| xmlTextWriterWriteAttribute(writer, BAD_CAST "myAttrib", "x") < 0
|| somehow print out content < 0
|| xmlTextWriterEndElement(writer) < 0)
{
// Handle error
}

It looks like xmlTextWriterWriteFormatString or xmlTextWriterWriteString will do the trick. Somehow I missed those at first
when looking through the API details.
Rather than delete, I'll leave here as this info might be useful for others looking for this info quickly.
Example:
if (xmlTextWriterStartElement(writer, BAD_CAST "MyElement") < 0
|| xmlTextWriterWriteAttribute(writer, BAD_CAST "myAttrib", "x") < 0
|| xmlTextWriterWriteString(writer, "Content") < 0
|| xmlTextWriterEndElement(writer) < 0)
{
// Handle error
}
Update: Tested and confirmed this works.

Related

How to check if args[1] is in a range of numbers

I tried the following code, the bot doesn't error but the command replies with the true outcome only, even when the args[1] is out of the range.
This is the code I tried
if(Number(-10) << parseInt(args[1]) << Number(10)) {
message.channel.send("yes")
} else {
message.channel.send("no")
}
I also tried using the parseInt function on the values of 10 and -10, but same issue, the bot replies with yes only
Unlike Python, you cannot do 2 comparisons in the same "block" of code.
The code 1 < 2 < 3 is actually doing (1 < 2) < 3 which actually works out as true < 3 and as JS uses false=0, true=1, you are now checking if 1 < 3, not if 2 is.
So, in your case, it will always return true as either false(0) OR true(1) will be less than 10.
Just change it to -10 < parseInt(args[1]) && parseInt(args[1]) < 10 with an undefined/length check before it.
For example:
if (args.length >= 2 && -10 < parseInt(args[1]) && parseInt(args[1]) < 10) {
message.channel.send("yes");
} else {
message.channel.send("no");
}
JS won't throw any error of IndexOutOfRange as other languages like C#, Java.
It'll return undefined instead.
Please below snippet for how it may happen.
If your logic is to check a value in between -10 and 10, you have to change a little bit with an & operator.
Cheers.

Darker Google userscript not working in Tampermonkey 4.7

I'm trying to install the Darker Google userscript in Tampermonkey 4.7 (for Safari 12), but it's not working.
Since I'm a total newbie on Tampermonkey I don't really know where to put my hands on.
In the Tampermonkey's dashboard I see that this userscript does not match any particular website, while for example Darker Facebook shows "*.facebook.com" and it works, but perhaps this is just a bad guess.
The beginning of the userscript is:
(function() {var css = "";
css += [
"/* Darker Google by Zigboom Designs */",
"",
"#namespace url(http://www.w3.org/1999/xhtml);"
].join("\n");
if (false ||
(document.location.href.indexOf("http://blogsearch.google") == 0) ||
(document.location.href.indexOf("http://books.google") == 0) ||
(document.location.href.indexOf("http://209.85.165.104") == 0) ||
(document.location.href.indexOf("http://translate.google") == 0) ||
(document.location.href.indexOf("http://video.google") == 0) ||
(document.location.href.indexOf("https://encrypted.google") == 0) ||
(document.location.href.indexOf("https://translate.google") == 0) ||
(document.location.href.indexOf("http://scholar.google") == 0) ||
(document.location.href.indexOf("https://scholar.google") == 0) ||
(document.location.href.indexOf("http://images.google") == 0) ||
(document.location.href.indexOf("https://images.google") == 0) ||
(document.location.href.indexOf("https://www.google.com/fonts") == 0) ||
(new RegExp("^https?://www\\.google\\.[a-z.]*/(?!calendar|nexus|adsense|analytics|maps).*$")).test(document.location.href))
which makes me thing it should match any *.google.com website... but it doesn't.
Its because the userscript is not running in the page. Add the following line, save it and reload the page.
// #match *://*.google.com/*
The above makes the userscript to run on Google sites.
For match-patterns, refer here

Testing intermediate variables in a large file using Frama-c

I am trying to use Frama-c to check some properties of a C function that I have. The function is quite large and there are some intermediate variables that I need to check. (I am following this and this manual)
My program has the following structure:
There are 15 return statements spread throughout the program.
The variables I need to check are assigned values at several places in the program, depending on the path of the program.
my_function(){
intermediate var 1=xx;
//#assert var 1>some_value;
intermediate var 2=yy;
return var 4;
intermediate var 1=xx;
//#assert var 1>some_value;
return var 4;
intermediate var 2=xx;
intermediate var 1=yy;
//#assert var 1>some_value;
return var 4;
}
Explanation: I need to check certain properties related to var 1, var 2 and var 4. I tried 2 approaches.
use assert whenever var 1 is set as above.
Problem with this was that Frama-C checks only the first assert.
use annotations in the beginning.
/*# requires \valid(var 1);
ensures var 1 > some_value;
*/
In this case, Frama-C returns an error.
Question: How can I check the properties for intermediate problems? Is there a sample program?
*I haven't included my original function as it is very long.
As Virgile has mentioned, your question is not very clear, but I assume you are trying to validate some properties of var1 and var2.
This book provides some nice examples and I think the following should help you.
int abs(int val){
int res;
if(val < 0){
//# assert val < 0 ;
res = - val;
//# assert \at(val, Pre) >= 0 ==> res == val && \at(val, Pre) < 0 ==> res == -val;
} else {
//# assert !(val < 0) ;
res = val;
//# assert \at(val, Pre) >= 0 ==> res == val && \at(val, Pre) < 0 ==> res == -val;
}
return res;
}
The author has used the concept of Hoare triples in this scenario, where you check (assert) a certain property by asserting its requirements (pre-condition) for a property and check if a property holds after the corresponding statements are executed.
Hope this helps.

Actionscript 3 dynamic multidimensional array

Okay so this is my code
var no:int = 0;
var elem:int = 0;
loadedData = myLoader.data.split(/\r\n|\n|\r/);
for (var i:int = 0; i < loadedData.length; i++)
{
if (loadedData[i] != '')
{
if (loadedData[i] != ',')
{
if(patterns[no] == undefined) patterns[no] = [];
trace(no);
trace(elem);
obstacleData[i] = loadedData[i].split(",");
trace(patterns[no]);
patterns[no][elem] = obstacleData[i][0];
patterns[no][elem + 1] = obstacleData[i][1];
elem += 2;
trace('Pattern' , no, ': ', patterns[no]);
}
else if (loadedData[i] == ',')
{
no += 1;
elem = 0;
patterns[no] = 0;
}
}
}
The problem is at this line
patterns[no][elem] = obstacleData[i][0];
I'm getting the error: [Fault] exception, information=ReferenceError: Error #1056: Cannot create property 0 on Number.
I've read around on this and multidimensional arrays just seem to be so much more complicated on AS3 compared to other languages where you just create a multidimensional array and it works.
I feel like I've done something really obvious wrong and will feel like an idiot upon someone telling me but I really need help on this one since I'm new to this whole AS3 way of creating multidimensional arrays.
Just in case the output for the code when run is as so:
0
0
Pattern 0 : 300,60
0
2
300,60
Pattern 0 : 300,60,350,90
1
0
0 <--- This might be the problem?
[Fault] exception, information=ReferenceError: Error #1056: Cannot create property 0 on Number.
Maybe the error is caused by the line
patterns[no] = 0;
I think you want to assign an empty array or maybe null and check for null later.

A better way to properly implement string 'in' array

suppose you have an array with a number of strings in ActionScript3 and you want to test if a test string is "in" that array. "in" only works against the index with Arrays in AS3 (which is totally retardo if you ask me), though it does work with ojects, but we're not talking about objects.
Can someone improve (reduce) on this code I'm using now? I'm hoping to avoid defining a utility function - I'd like a nice elegant one-liner.
myArray.filter(function(item:*, i:int, a:Array) { return (item == testString); }).length
Since 0 == false we can use it in a test. Do note that testString's scope is defined in the containing function, encapsulated by the closure.
if (allowedProfiles.filter(function(item:*, i:int, a:Array) { return (item == name); }).length){ // yay! }
Use the Array.indexOf() method to see that the index of the string in the array is not -1 (not found):
var myArray:Array = ["hello", "world"];
trace(myArray.indexOf("hello")); // == 0;
trace(myArray.indexOf("goodbye")); // == -1
Why not just use indexOf()?
if(myArray.indexOf("testString") != -1) { // it's in there

Resources