Raised This Month: $ Target: $400
 0% 

[INC] advanced.sp and fileIO.sp


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 09-07-2009 , 12:39   Re: [Extension] advanced.sp and fileIO.sp
Reply With Quote #1

Extensions are C++ plugins extending the functionality of SourceMod, these are includes, so I suggest you change your topic title from [Extension] to [INC] and rename advanced.sp to advanced.inc, so you can do #include <advanced>.

Also:
  • A lot of your loops use i < MaxClients, which should be i <= MaxClients.
  • ReplyToCommand should be used for commands, use PrintToChat for what you want.
  • DisplayCenterTextToAll and DisplayTextToAll are the same as PrintCenterTextAll and PrintToChatAll.
  • FindPlayer does the same as ProcessTargetString, but the latter is more versatile.
  • Use MaxClients instead of 24/25 in FindPlayer, FindPlayerBackwards, FindHighestClientID and CountPlayers.
Other than that I do have to agree it has a bunch of useful functions.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
Flynn
Senior Member
Join Date: Sep 2009
Old 09-07-2009 , 13:07   Re: [Extension] advanced.sp and fileIO.sp
Reply With Quote #2

Quote:
Originally Posted by DJ Tsunami View Post
Extensions are C++ plugins extending the functionality of SourceMod, these are includes, so I suggest you change your topic title from [Extension] to [INC] and rename advanced.sp to advanced.inc
It retains the information on the plugin if it's .sp, and also, backwards compatibility with my older plugins. There is nothing stopping people from removing the plugin info and renaming it to .inc.

Code:
#include "fileIO.sp"
Will work for including it.
Quote:
Originally Posted by DJ Tsunami View Post
  • A lot of your loops use i < MaxClients, which should be i <= MaxClients.
  • ReplyToCommand should be used for commands, use PrintToChat for what you want.
  • DisplayCenterTextToAll and DisplayTextToAll are the same as PrintCenterTextAll and PrintToChatAll.
  • FindPlayer does the same as ProcessTargetString, but the latter is more versatile.
  • Use MaxClients instead of 24/25 in FindPlayer, FindPlayerBackwards, FindHighestClientID and CountPlayers.
As said, these functions in advanced.sp are probably mostly redundant functions for more experienced coderss. FileIO simply makes use of the string operators inside advanced.sp for it's own usage.

The 25 cap was because sometimes it would overshoot the actual maxclients with MaxClients and cause IsClientInGame and IsFakeClient to complain/break/spaz-out (they seem to treat bad numbers as faulty and break rather than ignoring them, continuing on and returning false - which is annoying, to say the least).

Feel free to remove/delete unused or useless functions from advanced.sp.

fileIO makes use of:
CountString
CheckStringPrecise
CheckStringVague (which is superposition of StrContains - this is to ensure ordering is consistant)
CheckStringAdaptive
IsLetter (probably null - but I needed my own implementation to avoid any undefined behavior)
IsCharString
FindNumeric

All the others are optional/redundant functions. I've thrown them in, just in-case it's of use to people.

FURTHER NOTE: SaveArticleString/LoadArticleString has never actually been used. It's pretty much redundant and unneeded as performing FindStringStart, adding it's position to that from ReadString, and running ReadString from that position will do the legwork for you.

Last edited by Flynn; 09-07-2009 at 13:15.
Flynn is offline
Send a message via MSN to Flynn Send a message via Skype™ to Flynn
Flynn
Senior Member
Join Date: Sep 2009
Old 09-07-2009 , 14:19   Re: [INC] advanced.sp and fileIO.sp
Reply With Quote #3

How do I use fileIO to write a string to file?
Write
Code:
SetSave("filename"); //This can be a string instead, rather than a literal
WriteString("information"); //This can be a string instead, rather than a literal
Be sure to use SetSave before performing any writing actions (not for every one - just at the start).

How do I use fileIO to get a string from file?
Write
Code:
SetLoad("filename"); //This can be a string instead, rather than a literal
new Find = FindStringStart("name of string"); //Other arguments are set to default
if(Find == -1)
{
 ReplyToCommand(client,"Could not find your string");
 return;
}

new String:buffer[256]; //Doesn't have to be this big so long as you know the string definitely won't exceed the storage
ReadString(buffer,Find); //Reads in the string from the given position
ReplyToCommand(client,"The string is %s",buffer);
How would I read information between two strings?
Tricky, but relatively straight-forward

Code:
SetLoad("filename");

new Start = FindStringEnd("first");
if(Start == -1){return false;} //Didn't find it

new End = FindStringStart("last");
if(End == -1){return false;} //Didn't find it

new String:buffer[256]; //Again, doesn't have to be this big

//CS is short for 'count string' - which is what ReadString returns
new CS = 0;
for(;CS != -1;)
{
  CS = ReadString(buffer,Start); //Note there is a third variable, that if set to true, removes the newline - useful for string comparisons!
  if(CS == -1)
  {
    return false; //Error reading the string
  }
  Start = Start + CS;
  ReplyToCommand(client,"The string is %s",buffer);
  if(Start > End)
  {
    return true; //We read all the strings in that position
  }
}
return false; //We hit an unknown error
How do I remove specific lines or strings from file?
Depends. If you know which lines, you would do...
Code:
new arr[5]
arr[0] = 4;
arr[1] = 3;
arr[2] = 10;
arr[3] = 11;
arr[4] = -1;//The last one in the array is always -1 so DeleteLines knows it's reached the end.

SetFile("filename");
DeleteLines(arr);
If you don't know the line numbers though, fear not!

FileIO has it's own function for that does it for you...
Code:
DeleteString(guessed_name); //This can be as precise or as vague a target as you want, however, be wary, as it will delete the first match.
How do I write an article to file?

This is straight-forward. You'll need the name, and the items to be put in.
Note, items have to be of the same datatype in order to be written - this is to ensure that fileIO knows what kind of data to expect, and reduces potential errors.

If you have mixed types (int/floats), use the SaveArticleFloat, and write the integer as a float, then perform the correct extraction process yourself when reloading the data.

To Write an Article, you would write...
Code:
new arr[4]

arr[0] = kills;
arr[1] = deaths;
arr[2] = times_peed_the_bed;
arr[3] = your_house_number;

SetSave("filename");
SaveArticleInt("I know where you live",arr,4);
And uuh... done. Thats it.

How do I read an Article from file?

Assuming you maintain consistency on how you write information to file, you extract it pretty much the same way.

Because not always people know where the article is, whether or not it exists or the name it's under (rule of Entropic decay), the LoadArticle functions are designed to search with a vague guess of a title with the expectancy of finding the right title when searching.

The last argument, P (stands for Position) takes the Position in file, so if you already know where it isn't, you can make it look for it where it most likely is.

Don't worry if there is a string (a non-article) that has the same name; LoadArticle functions won't try to read information if the next line contains a letter character. It won't resume search in-case there has been a serious error (like incorrect appending of strings after an Article Title etc).
Code:
new arr[4];
new String:full_title[128]
new String:guessed_title[64]

guessed_title = "where you live";

SetLoad("filename");
new N = LoadArticleInt(guessed_name,full_title,arr,4,0); //0 specifies the start
if(N == -1){return false;} //We didn't find the article

ReplyToCommand(client,"You earnt %d kills",arr[0]);
ReplyToCommand(client,"You suffered %d deaths",arr[1]);
ReplyToCommand(client,"You peed the bed %d times",arr[2]);
ReplyToCommand(client,"And your house number is %d",arr[3]);

ReplyToCommand(client,"Hey there, %s",full_title);
Thats pretty much all you need to know.

Last edited by Flynn; 09-10-2009 at 13:19. Reason: Updated missing important bits o' code
Flynn is offline
Send a message via MSN to Flynn Send a message via Skype™ to Flynn
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:20.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode