Raised This Month: $51 Target: $400
 12% 

[INC] advanced.sp and fileIO.sp


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Flynn
Senior Member
Join Date: Sep 2009
Old 09-07-2009 , 11:54   [INC] advanced.sp and fileIO.sp
Reply With Quote #1

These aren't so much as plugins that you use in-game that you include into your own plugins as additional functions.

Advanced .sp offers 'advanced' or additional functions that higher level functions use themselves. A lot of these functions in Advanced.sp are probably redundant to most coders (although it may contain some useful functions less-experienced ones can use).

This plugin is required in order to compile fileIO.sp.

fileIO.sp is the true goldmine in this one...

I developed fileIO.sp as a result of getting tired of writing and re-writing the same old tosh in order to be able to read and write information to files by greatly simplifying the process to nothing bar a few commands, with automated handling for errors.

Because I never officially intended it for public release, it may have bugs in it and parts that are hard to understand, but it's had several months worth of usage, so it's probably far more developed than most functions.

fileIO can;
Add the MapName to the front of a given filename
Write Strings to files
Write Articles to file (more on this later)
Find Strings in files
Read Strings from file
Read Articles from file (more on this later, again)
Delete Lines from file
Delete Articles from file (more on this later, again)
Append Strings to file

What is an Article? Why should I care?

An Article is typically a piece, or several pieces of information with a title to it. That is what an Article is, and you've guessed it; it saves the information under a title (as a reference).

For example, say you have a player that you want to store the kills, deaths, ragequits and kudos points. You'd use the player name (or Steam ID) as the title, and the ints are put into an array, EG

Code:
arr[4]
arr[0] = Kills
arr[1] = Deaths
arr[2] = Ragequits
arr[3] = Kudos
SetSave("filename.txt");
WriteArticleInt(player_name,arr,4); //The 4 being the number of 'articles'
Extraction is easier... Say you want to get it back but you don't know the precise name?

Code:
new String:guessed_name[64];
new String:real_name[128];
new arr[4];

guessed_name = "Fly"
LoadArticleInt(guessed_name,real_name,arr,4,0);//0 is the starting position in the file - you might use find string to find the start of the given string first before you load articles - meaning you can seperate articles into 'sections'
Failure returns a standard -1, otherwise the starting position of the string after the article.

fileIO has one major flaw; it doesn't use buildpaths, meaning it always saves to root directory. I haven't had time to try to understand the mostly vague nature of buildpath, and haven't really needed it per se. If anyone is willing to explain it to me, I'll tack it in.

Feel free to criticise, make suggestions or point out bugs. I can't guarantee I'll update anything though (although I am sure during plugin revisions I will).

If you are unfamilar with the functions usage or need the basics explained, feel free to ask and I will explain.
Attached Files
File Type: zip advanced and fileIO.zip (4.5 KB, 176 views)

Last edited by Flynn; 09-07-2009 at 12:55.
Flynn is offline
Send a message via MSN to Flynn Send a message via Skype™ to Flynn
exvel
SourceMod Donor
Join Date: Jun 2006
Location: Russia
Old 09-07-2009 , 12:37   Re: [Extension] advanced.sp and fileIO.sp
Reply With Quote #2

Flynn, looks useful. But it's not an extension. Maybe make it as INC file?
__________________
For admins: My plugins

For developers: Colors library
exvel is offline
Send a message via ICQ to exvel
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 #3

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 #4

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 #5

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
olj
Veteran Member
Join Date: Jun 2009
Old 09-07-2009 , 14:33   Re: [INC] advanced.sp and fileIO.sp
Reply With Quote #6

Nice. +
__________________
olj is offline
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 00:03.


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