Raised This Month: $32 Target: $400
 8% 

Module: Parser (JSON Reader) Win32


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 04-08-2014 , 11:35   Module: Parser (JSON Reader) Win32
Reply With Quote #1

Parser (JSON Reader)



Download
View CPP File

This module will allow you to parse files more easily, like listed below.
This is very recommended when you want to precache files, enumerate swears and set entity variables such viewmodel.
Linux version will be done ASAP.


configuration.ini file.

PHP Code:
{
   
"encoding" "UTF8",
   
"latitude" : -21.512312,
   
"maxplayers" 128,

   
"plugins" : [
      
"This1",
      
"This2"
   
]

parser.sma file.

PHP Code:
#include <amxmodx>
#include <parser>

public plugin_init()
{
    new 
parserFile:fileId;
    new 
gameName[16], configsDir[128], fileName[256];
    new 
encodingString[32], Float:cityLatitudemaxPlayers;

    
get_modname(gameNamecharsmax(gameName));
    
get_localinfo("amxx_configsdir"configsDircharsmax(configsDir));
    
formatex(fileNamecharsmax(fileName), "%s/%s/configuration.ini"gameNameconfigsDir);

    
/** If file does not exist, plugin_init forward will stop here.
      */
    
fileId parserLoadFile(fileName);

    
/** Reads all plugins.
      */
    
parserEnumData(fileId"plugins");

    
parserGetString(fileId"encoding"encodingStringcharsmax(encodingString));
    
cityLatitude parserGetFloat(fileId"latitude");
    
maxPlayers parserGetInt(fileId"maxplayers");

    
server_print("Encoding = %s, Latitude = %f, MaxPlayers = %d"encodingStringcityLatitudemaxPlayers);
}

public 
parserEnumForward(parserFile:fileIndexkeyName[], bufferSet[])
{
    
/** Prints to server all plugins.
      */
    
server_print(bufferSet);

parser.inc file.

PHP Code:
#if !defined PARSER_INC

#define PARSER_INC

#include <amxmodx>

#if AMXX_VERSION_NUM >= 175
 #pragma reqlib parser

 #if !defined AMXMODX_NOAUTOLOAD
  #pragma loadlib parser
 #endif
#else
 #pragma library parser
#endif

/** Example of file to be parsed.
  *
  *   {
  *       "encoding" : "utf-8",
  *       "maxPlayers" : 32,
  *       "latitude" : 51.213341,
  *
  *       "plugins" : [
  *           "admin.amxx",
  *           "admincmd.amxx",
  *           "adminvote.amxx"
  *       ]
  *   }
  */

/** Prints all items from a named category.
  *
  * @param fileIndex        The file that the information is taken from.
  * @param keyName    The key.
  * @param bufferSet    The buffer taken.
  *
  * @return        Does not matter.
  */
forward parserEnumForward(parserFile:fileIndexkeyName[], bufferSet[]);

/** Parses a file.
  *
  * @param fileName    The file. If you need a path, the game name should be added too.
  *
  * @return        Does not matter.
  */
native parserFile:parserLoadFile(const fileName[]);

/** Enumerates all data from a parsed file regarding the key name.
  *
  * @param fileIndex        The file that the information is taken from.
  * @param keyName    The key.
  *
  * @return        Does not matter.
  */
native parserEnumData(const parserFile:fileIndex, const keyName[]);

/** Retrieves a string by key.
  *
  * @param fileIndex        The file that the information is taken from.
  * @param keyName    The key.
  * @param stringBuffer    The buffer to be filled.
  * @param stringSize    The size of buffer
  *
  * @return        Does not matter.
  */
native parserGetString(const parserFile:fileIndex, const keyName[], stringBuffer[], const stringSize);

/** Retrieves a numerical value by key.
  *
  * @param fileIndex        The file that the information is taken from.
  * @param keyName    The key.
  *
  * @return        The numerical value.
  */
native parserGetInt(const parserFile:fileIndex, const keyName[]);

/** Retrieves a float value by key.
  *
  * @param fileIndex        The file that the information is taken from.
  * @param keyName    The key.
  *
  * @return        The float value.
  */
native Float:parserGetFloat(const parserFile:fileIndex, const keyName[]);

#endif 
__________________

Last edited by claudiuhks; 04-08-2014 at 11:49.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 04-08-2014 , 12:23   Re: Module: Parser (JSON Reader) Win32
Reply With Quote #2

GJ, very nice work
TheDS1337 is offline
Neeeeeeeeeel.-
Some Guy Yellin'
Join Date: Jul 2010
Location: Argentina
Old 04-08-2014 , 12:56   Re: Module: Parser (JSON Reader) Win32
Reply With Quote #3

Good Job man, I like it!
__________________
Neeeeeeeeeel.- is offline
Send a message via Skype™ to Neeeeeeeeeel.-
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-08-2014 , 13:11   Re: Module: Parser (JSON Reader) Win32
Reply With Quote #4

Don't remember if the same format exactly, but I'm thinking importing in AMXX text parser functionality from SourceMod, as even if we won't apply this on current configuration file, I believe it's useful to have unified way for plugin's configs.
__________________

Last edited by Arkshine; 04-08-2014 at 13:11.
Arkshine is offline
hichamera
Senior Member
Join Date: Feb 2014
Location: Algeria
Old 04-08-2014 , 14:06   Re: Module: Parser (JSON Reader) Win32
Reply With Quote #5

GOOd Man Nice ;)
__________________

Try & Try & Try Nothing Is impossible
hichamera is offline
Send a message via Skype™ to hichamera
DWIGHTpN
Senior Member
Join Date: Jan 2013
Location: Romania.
Old 04-08-2014 , 16:02   Re: Module: Parser (JSON Reader) Win32
Reply With Quote #6

Good job .
Quote:
This is very recommended when you want to precache files, enumerate swears




I'm happy to hear this .
DWIGHTpN is offline
OvidiuS
Chillaxin'
Join Date: Dec 2009
Location: Serbia
Old 04-08-2014 , 19:21   Re: Module: Parser (JSON Reader) Win32
Reply With Quote #7

Pretty good job ;)
Is there any chance that this module can parse json from string array?
OvidiuS is offline
Send a message via Skype™ to OvidiuS
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 04-08-2014 , 19:50   Re: Module: Parser (JSON Reader) Win32
Reply With Quote #8

Quote:
Originally Posted by OvidiuS View Post
Pretty good job ;)
Is there any chance that this module can parse json from string array?
I don't understand what you mean. Can you give a proper example?
__________________
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
meTaLiCroSS
Gaze Upon My Hat
Join Date: Feb 2009
Location: Viña del Mar, Chile
Old 04-08-2014 , 21:55   Re: Module: Parser (JSON Reader) Win32
Reply With Quote #9

Quote:
Originally Posted by claudiuhks View Post
I don't understand what you mean. Can you give a proper example?
seriously?

PHP Code:
<?php
$json 
'{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($jsontrue));

?>
Code:
object(stdClass)#1 (5) {     ["a"] => int(1)     ["b"] => int(2)     ["c"] => int(3)     ["d"] => int(4)     ["e"] => int(5) }  array(5) {     ["a"] => int(1)     ["b"] => int(2)     ["c"] => int(3)     ["d"] => int(4)     ["e"] => int(5) }
https://forums.alliedmods.net/showth...highlight=JSON

edit: Would be so much better if you port Exolent's JSON include into a module instead of what you've done, because it's "so" specific, and would have more accesibility
__________________
Quote:
Originally Posted by joropito View Post
You're right Metalicross

Last edited by meTaLiCroSS; 04-08-2014 at 21:56.
meTaLiCroSS is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 04-08-2014 , 22:02   Re: Module: Parser (JSON Reader) Win32
Reply With Quote #10

Quote:
Originally Posted by meTaLiCroSS View Post
Would be so much better if you port Exolent's JSON include into a module instead of what you've done, because it's "so" specific, and would have more accesibility
Well, I don't really want to copy someone's code. I only wanted a basic JSON reader. Why should I link some already existing natives to a module? Better to use Exolent's existing code if you want to do more than this module can do.

Anyways, I still don't understand what Ovidius meant. Don't tell me you want to parse {"a":1,"b":2,"c":["x","y","z"]} string since this line can be found into a file.
__________________

Last edited by claudiuhks; 04-08-2014 at 22:05.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
Reply


Thread Tools
Display Modes

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 20:30.


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