Compilation warning while using codepad.org and devcpp - pointers

I am writting a program to check weather a given string is a palindrome. When I am trying to compile the code I got the below warning as
pandridom_with_space.cpp [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive]
I know we can ignore this warning but I want to know how I can modify my code to remove this warning.
bool isPalindrome(const char*p,int len)
{
if((p==NULL)||(len<1))
return false;
int i=0,j=len-1;
while(p[i]!=0 && i<j)
{
while((i<j)&&(p[i] == " "))// <<<===== here I am getting warning.
i++;
while((i<j)&&(p[j] == " "))// <<<===== here I am getting warning.
j--;
if(p[i]!=p[j])
return false;
i++;
j--;
}
return true;
}

p[i] == " "
p[i] is a char (which is an integer type), and " " is a (const, since it's C++) char array that is converted for the comparison to a pointer to its first element.
You meant to compare it to a space character, ' '. (Note the single quotes for a character literal, double quotes are for string literals.)

Related

How to read this string into jsoncpp 's Json::Value

I have such a json string :
{"status":0,"bridge_id":"bridge.1","b_party":"85267191234","ref_id":"20180104151432001_0","function":{"operator_profile":{"operator":"aaa.bbb"},"subscriber_profile":{"is_allowed":true,"type":8},"name":"ServiceAuthen.Ack"},"node_id":"aaa.bbb.collector.1"}
how can I read it into jsoncpp lib 's Json::Value object ?
I found such code by searching stackoverflow :
std::string strJson = "{\"mykey\" : \"myvalue\"}"; // need escape the quotes
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse( strJson.c_str(), root ); //parse process
if ( !parsingSuccessful )
{
std::cout << "Failed to parse"
<< reader.getFormattedErrorMessages();
return 0;
}
std::cout << root.get("mykey", "A Default Value if not exists" ).asString() << std::endl;
return 0;
but how to convert my string to this form ?
{\"mykey\" : \"myvalue\"}
thank you for any help .
You don't.
The slash characters are escape characters used to represent a " in C++ source code (without them the " would mean "This is the end of this C++ string literal").
The JSON (which isn't C++ source code) should not have the escape characters in it.

Why I can't pass a string as parameter of a function?

I'm writing a code and I can't use a String as parameter of a function, the Arduino keeps resetting. This is my original code thad do not works:
Serial.print(readLine("routes.txt", 1)); // calling the function
String readLine(String fileName, unsigned int lineNum)
{
if (!SD.exists(fileName))
{
Serial.println("- " + fileName + " do not exists!");
return ("FAILURE");
}
[continue the code...]
This code works:
Serial.print(readLine(1)); // calling the function
String readLine(unsigned int lineNum)
{
if (!SD.exists("routes.txt"))
{
Serial.println("- " + "routes.txt" + " do not exists!");
return ("FAILURE");
}
[continue the code...]
Anyone to help me?
Try assigning the value "routes.txt" into a variable of type String first and then use this variable as a parameter when calling the method.
The compiler might assume the value is a char-array instead of a String-value.

How to count statements in C ignoring the comments

int Emptylines(FILE *fp);
int Numberofstatements(FILE *fp);
int main() {
FILE *fp = NULL;
FILE *fp1 = NULL;
int n1, n2;
char fname[255], fname1[255];
printf("Enter file name for reading");
fflush(stdin);
scanf("%s", &fname);
fp = fopen(fname, "r");
if (fp == NULL) {
printf("File with name %s couldn't be open", fname);
exit(1);
}
n1 = Emptylines(fp); // this is for empty lines
n2 = Numberofstatements(fp);
printf("Insert file name for writing");
fflush(stdin);
scanf("%s", &fname1);
fp1 = fopen(fname1, "w+");
fprintf(fp1, "The number of empty lines=%d", n1);
fprintf(fp1, "The number of statements=%d", n2);
fclose(fp);
fclose(fp1);
return 0;
}
int Numberofstatements(FILE *fp) {
char line[128];
int nofstatements = 0;
while (fgets(line, sizeof line, fp) != NULL) {
if (strstr(line, "if") != 0)
nofstatements++;
}
return nofstatements;
}
I need to count all statements like if, do, while, break, etc. as well as empty lines and then save the result in a new file. I succeed in counting the empty lines but I have no idea how to count the statements. I tried 2 different ways but both failed.
I also need to ignore comments while reading the code, so if there is a for, while, etc. in the comments it shouldn't be counted.
A very basic answer addressing the fundamental issue (although there are others).
When you call int Numberofstatements(FILE *fp) you already reached the end of file in int Emptylines(FILE *fp); so you must add the statement
rewind(fp);
before trying to parse the file for a second time. Good luck with developing this.
OP asks: "Any ideas ?"
To do properly, suggest reading 1 char at a time. Keep track if you are in 1) on an include line, 2) inside a " " 3) inside a ' ' 4) in a // comment 5) inside a /* comment or 6) just plain code (watch for escape sequences). When in plain code look for the keywords do, while, etc. and all the while count the '\n'.
To do correctly - this is not an easy task - about 10x the code you have posted.
Sample beginning of a state machine.
state = plaincode;
while ((c = getc()) != EOF) {
switch (state) {
slashslash_commnet:
if (c == '\n) state = plaincode;
break;
plaincode:
if (c == '/') {
c2 = getc();
if (c2 == '/') { state = slashslash_commnet; break; }
else if (c2 == '*') { state = slashstar_commnet: break; }
else unget(c2);
else if (c == '\"') {
...

how can i use sqlite rawQuery?

when I call select in other function, there is problem in line number 3.
Is it wrong?
public String[] select(int n){
db = helper.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM info WHERE number='" + n + "'", null);
}
The rawQuery() looks good, though usually you wouldn't quote integers as 'string literal'.
However, a non-void method must return a value and your method doesn't return anything. Add return null; to make it compile; implement a loop that builds a string array to return a non-null value.

Where to find or duplicate code that produces HttpRequestValidationException

I have some PageMethods (static methods in a page marked with <WebMethod>) defined on some pages and call them using an ajax call. This POST to the server apparently doesn't trigger the ASP.NET code that would raise HttpRequestValidationException if the data sent is deemed possible XSS, so I'd like to duplicate that checking code to run it in my page methods.
Anyone know the details of that code or where I can find it? I looked in the MS AntiXss library, but it only does encoding, not actually checking input, AFAIK.
Edit: Or point me in the direction of code or a library that does some similar checking.
Analyzing the stack trace when a System.Web.HttpRequestValidationException is raised we can find out what code is throwing it.
System.Web.HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (IdentifierTextBox="
at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection)
Using Reflector we find that ValidateString is calling: RequestValidator.Current.IsValidRequestString, which in turn calls CrossSiteScriptingValidation.IsDangerousString which is:
internal static bool IsDangerousString(string s, out int matchIndex)
{
matchIndex = 0;
int startIndex = 0;
while (true)
{
int num2 = s.IndexOfAny(startingChars, startIndex);
if (num2 < 0)
{
return false;
}
if (num2 == (s.Length - 1))
{
return false;
}
matchIndex = num2;
char ch = s[num2];
if (ch != '&')
{
if ((ch == '<') && ((IsAtoZ(s[num2 + 1]) || (s[num2 + 1] == '!')) || ((s[num2 + 1] == '/') || (s[num2 + 1] == '?'))))
{
return true;
}
}
else if (s[num2 + 1] == '#')
{
return true;
}
startIndex = num2 + 1;
}
}

Resources