AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Module Coding (https://forums.alliedmods.net/forumdisplay.php?f=9)
-   -   Module: Parser (JSON Reader) Win32 (https://forums.alliedmods.net/showthread.php?t=238340)

claudiuhks 04-08-2014 11:35

Module: Parser (JSON Reader) Win32
 
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 


TheDS1337 04-08-2014 12:23

Re: Module: Parser (JSON Reader) Win32
 
GJ, very nice work

Neeeeeeeeeel.- 04-08-2014 12:56

Re: Module: Parser (JSON Reader) Win32
 
Good Job man, I like it!

Arkshine 04-08-2014 13:11

Re: Module: Parser (JSON Reader) Win32
 
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.

hichamera 04-08-2014 14:06

Re: Module: Parser (JSON Reader) Win32
 
GOOd Man Nice ;)

DWIGHTpN 04-08-2014 16:02

Re: Module: Parser (JSON Reader) Win32
 
Good job :).
Quote:

This is very recommended when you want to precache files, enumerate swears




I'm happy to hear this :P.

OvidiuS 04-08-2014 19:21

Re: Module: Parser (JSON Reader) Win32
 
Pretty good job ;)
Is there any chance that this module can parse json from string array?

claudiuhks 04-08-2014 19:50

Re: Module: Parser (JSON Reader) Win32
 
Quote:

Originally Posted by OvidiuS (Post 2122193)
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?

meTaLiCroSS 04-08-2014 21:55

Re: Module: Parser (JSON Reader) Win32
 
Quote:

Originally Posted by claudiuhks (Post 2122204)
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

claudiuhks 04-08-2014 22:02

Re: Module: Parser (JSON Reader) Win32
 
Quote:

Originally Posted by meTaLiCroSS (Post 2122238)
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.


All times are GMT -4. The time now is 00:08.

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