Source language: Translate to:

Sanitize a string to be use in actions arguments

Questions about NeoBook PlugIns

Moderator: Neosoft Support

Sanitize a string to be use in actions arguments

Postby dec » Fri Jul 15, 2011 6:15 pm

Hi plugins developers!

Sorry if this question has been made before. I want to sanitize all actions arguments (when actions have edit forms) replacing, for example, the " (quote) char for the appropiate [#34] variable.

In fact I do this every time, but, two question now: I need to replace another chars? And the other question is: I need to do this in every action form edit? The last question is not to much important (I think now) but the first question yes, in order to apply them.

Thanks in advance!
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Postby NeoScripter » Fri Jul 15, 2011 9:02 pm

Answer to Question #1: Typically when I require the usage of a quotation mark in NeoBook I manually add the "[#34]", but if you wish to do so yourself you can use the code below.

In Delphi you can use the following code to replace text in a string, of course you would set "StringWithQuotes" to Edit1.Text/Etc and then set the action parameter to the result from StringReplace().
Code: Select all
var
  StringWithQuotes, NewString : String;
begin
  StringWithQuotes := '"This is a quote"';
  NewString  := StringReplace(StringWithQuotes, '"', '[#34]', [rfReplaceAll]); //Replaces any quotation marks with "[#34]".
end;


If you prefer programming in C++, see the code below (this is an entire project);
Code: Select all
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{   
    string String("\"This is a quote\""); //Displays as "This is a quote".
    string StringToReplace("\""); //This is the string that we replace; \" = ".
    string ReplaceWith( "[#34]" ); //This is the string that we replace with.
    string::size_type Position = 0;
   
    while ( (Position = String.find(StringToReplace, Position)) != string::npos ) { //Loops and Replaces quotation marks with "[#34]"
        String.replace( Position, StringToReplace.size(), ReplaceWith );            //so "This is a quote" becomes; [#34]This is a quote[#34].
        Position++;
    }
   
    cout << String << endl; //Displays the string on the command-line, it should output; [#34]This is a quote[#34].
    system("PAUSE");
    return EXIT_SUCCESS;
}


Note; In C++ \" is equivalent to a quotation mark.

Answer to Question #2: You may want to replace other special ASCII characters (or their codes) such as;
Code: Select all
Output    ASCII Value
[         [#91]
]         [#93]
|         [#124]
Carriage  [#13]
Return
Line Feed [#10]
Tab       [#9]


Answer to Question #3: You will have to do this for every individual edit control, that may, at some point contain a quotation mark.

My apologies, if this was not the reply that you were looking for :(

Regards,

Alex Rothschild
User avatar
NeoScripter
 
Posts: 36
Joined: Fri Aug 20, 2010 3:58 pm

Postby dec » Sat Jul 16, 2011 3:36 am

Hi there!

Thanks for your answers NeoScripter. So I understand that maybe no others characters must be replace, and, in fact, the quote char is the unique char to be replace, because, is in conflict with the Neobook actions prototype, because quotes are used to separate action params.

In other words, if you want to write something like this on Neobook:

Code: Select all
GotoPage ""My Page""


You get an error said "This action require one parameter" in runtime, and, in designtime, you lose data when edit the above "wrong write" action. For the other hand, you no get any error if you write, for example, a "tab" into one action param. Maybe the action dont work, but, this not cause the error mentioned above.

Well, maybe another mates can said anything else.

Thanks you very much NeoScripter!
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain

Postby NeoScripter » Sat Jul 16, 2011 7:29 am

dec wrote:Hi there!

Thanks for your answers NeoScripter. So I understand that maybe no others characters must be replace, and, in fact, the quote char is the unique char to be replace, because, is in conflict with the Neobook actions prototype, because quotes are used to separate action params.
Yes, the quotation mark is the only action parameter expected to be replaced. Some developers may wish to replace the "[#13]" code with a carriage return so that users may show multi-line message boxes/etc.

dec wrote:In other words, if you want to write something like this on Neobook:

Code: Select all
GotoPage ""My Page""


You get an error said "This action require one parameter" in runtime, and, in designtime, you lose data when edit the above "wrong write" action. For the other hand, you no get any error if you write, for example, a "tab" into one action param. Maybe the action dont work, but, this not cause the error mentioned above.

Well, maybe another mates can said anything else.

Thanks you very much NeoScripter!
Ah yes, I have lost data quite a few times due to the fact of accidentally using quotation marks (I'm used to the NeoBook editor replacing them for me) and not having them replaced by "[#34]" and then, editing the command again. This is perhaps, why it is a good Idea to check action parameters for quotation marks before passing them as a command to the editor in design-time. Quotation marks are really the only thing to worry about other than people hard-coding variables sometimes, expecting that if they enter "Z" the editor will replace their text with [Z] (which it does, for NeoBook's commands), I really would not bother implementing this check but if you wish you may use the code below to do so (requires that StrUtils is in your uses clause);
Code: Select all
var
  Action : AnsiString;
begin
  Action := 'Z';
  if AnsiStartsStr('[', Action)
  then Exit //The variable starts with "[" and is most likely proper, exit the procedure.
  else Insert('[', Action, 1); //The variable does not start with "[" so it's safe to assume that they forgot "]" as well, let's add them.
  Insert(']', Action, Length(Action)+2); //Adds "]" to the end of the String.
  Edit1.Text := Action; //Edit1 will now contain "[Z]".
end;

You may wish to check that both the start and end do not contain brackets, you can do so with little modification to the code above (the below requires that you have StrUtils in your uses);
Code: Select all
var
  Action : AnsiString;
  FS : Int64;
begin
  Action := '[Z';
  if AnsiStartsStr('[', Action)
  then FS := 1337 //The variable starts with "[" and so set FS to 1337.
  else Insert('[', Action, 1); //The variable does not start with "[" so let's add the starting bracket.
  if AnsiEndsStr(']', Action)
  then FS := -1337 //The variable ends with "]" and so set FS to -1337.
  else Insert(']', Action, Length(Action)+2); //The variable does not end with "]" so let's add the ending bracket.
  Edit1.Text := Action; //Edit1 will now contain "[Z]".
end;
User avatar
NeoScripter
 
Posts: 36
Joined: Fri Aug 20, 2010 3:58 pm

Postby dec » Sat Jul 16, 2011 7:39 am

Hi,

Thanks for your explaining NeoScripter!
User avatar
dec
 
Posts: 1663
Joined: Wed Nov 16, 2005 12:48 am
Location: Spain


Return to PlugIn Discussions

Who is online

Users browsing this forum: No registered users and 1 guest