Advertisement
Hauke

HSA 2.2.0

Aug 11th, 2013
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 10.66 KB | None | 0 0
  1. // HSA 2.2.0 include by Hauke Marquardt alias |-|auke - 12.08.2013
  2. /*
  3. License:
  4.         This code is free: you can redistribute it and/or modify
  5.         it under the terms of the GNU General Public License as published by
  6.         the Free Software Foundation, either version 3 of the License, or
  7.         (at your option) any later version.
  8.  
  9.         This code is distributed in the hope that it will be useful,
  10.         but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.         GNU General Public License for more details.
  13.  
  14.         You should have received a copy of the GNU General Public License
  15.         along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.         You´re not allowed to copy any code from this, into your code without
  17.         naming authors name in credits!
  18. */
  19.  
  20. #if defined _hsa_included
  21.     #endinput
  22. #endif
  23.  
  24. #define _hsa_included
  25.  
  26. #if !defined _hsa_max_file_name_offset
  27.     #define _hsa_max_file_name_offset       0
  28. #endif
  29.  
  30. #if !defined _hsa_max_files_cache_offset
  31.     #define _hsa_max_files_cache_offset     0
  32. #endif
  33.  
  34. #if !defined _hsa_entries_per_file_offset
  35.     #define _hsa_entries_per_file_offset    0
  36. #endif
  37.  
  38. #if !defined _hsa_max_key_lenght_offset
  39.     #define _hsa_max_key_lenght_offset      0
  40. #endif
  41.  
  42. #if !defined _hsa_max_value_lenght_offset
  43.     #define _hsa_max_value_lenght_offset    0
  44. #endif
  45.  
  46. #define _MAX_FILE_NAME_LENGHT               96 + _hsa_max_file_name_offset
  47. #define _MAX_FILES                          20 + _hsa_max_files_cache_offset
  48. #define _MAX_ENTRIES                        50 + _hsa_entries_per_file_offset
  49. #define _MAX_KEY_LENGHT                     24 + _hsa_max_key_lenght_offset
  50. #define _MAX_VALUE_LENGHT                   128 + _hsa_max_value_lenght_offset
  51. #define _MAX_LINE_CACHE                     _MAX_KEY_LENGHT + _MAX_VALUE_LENGHT + 3
  52.  
  53. #if !defined _h_library_norm_included
  54.     #define PAWN_TYPE_INT                   1
  55.     #define PAWN_TYPE_FLOAT                 2
  56.     #define PAWN_TYPE_ARRAY                 3
  57.     #define _h_library_norm_included
  58. #endif
  59.  
  60. #define getInt:%1(%2)                       _GetInt(%1,%2)
  61. #define getFloat:%1(%2)                     _GetFloat(%1,%2)
  62. #define getString:%1(%2)                    _GetArray(%1,%2)
  63. #define setInt:%1(%2,%3)                    _SetInt(%1,%2,%3)
  64. #define setFloat:%1(%2,%3)                  _SetFloat(%1,%2,%3)
  65. #define setString:%1(%2,%3)                 _SetArray(%1,%2,%3)
  66. #define Save(%1)                            _WriteFile(%1)
  67. #define SaveAll(%1)                         _WriteAll(%1)
  68.  
  69. new _CachedFiles[ _MAX_FILES ][ _MAX_FILE_NAME_LENGHT ];
  70. enum _LineCache
  71. {
  72.     _Key[ _MAX_KEY_LENGHT ],
  73.     _Value[ _MAX_VALUE_LENGHT ]
  74. }
  75. new _FileCache[ _MAX_FILES ][ _MAX_ENTRIES ][ _LineCache ];
  76.  
  77. stock _GetInt ( File[ ] , Key[ ] ) {
  78.     new Result[ _MAX_VALUE_LENGHT ];
  79.     _GetData ( File , Key , Result , PAWN_TYPE_INT );
  80.     return strval ( Result );
  81. }
  82.  
  83. stock Float:_GetFloat ( File[ ] , Key[ ] ) {
  84.     new Result[ _MAX_VALUE_LENGHT ];
  85.     _GetData ( File , Key , Result , PAWN_TYPE_FLOAT );
  86.     return floatstr ( Result );
  87. }
  88.  
  89. stock _GetArray ( File[ ] , Key[ ] ) {
  90.     new Result[ _MAX_VALUE_LENGHT ];
  91.     _GetData ( File , Key , Result , PAWN_TYPE_ARRAY );
  92.     return Result;
  93. }
  94.  
  95. stock _SetInt ( File[ ] , Key[ ] , Value ) {
  96.     new uValue[ _MAX_LINE_CACHE ];
  97.     format ( uValue , _MAX_LINE_CACHE , "%d" , Value );
  98.     _SetData ( File , Key , uValue );
  99. }
  100.  
  101. stock _SetFloat ( File[ ] , Key[ ] , Float:Value ) {
  102.     new uValue[ _MAX_LINE_CACHE ];
  103.     format ( uValue , _MAX_LINE_CACHE , "%f" , Value );
  104.     _SetData ( File , Key , uValue );
  105. }
  106.  
  107. stock _SetArray ( File[ ] , Key[ ] , Value[ ] ) {
  108.     _SetData ( File , Key , Value );
  109. }
  110.  
  111. stock _GetData ( File[ ] , Key[ ] , Result[ ] , Type ) {
  112.     new FileName[ _MAX_FILE_NAME_LENGHT ] , File:file;
  113.     format ( FileName , _MAX_FILE_NAME_LENGHT , "%s.hsa" , File );
  114.     if ( _GetFileCacheIndex ( File ) == -1 ) {
  115.         new CacheSlot = _GetFreeCacheSlot ( ) , Line[ _MAX_LINE_CACHE  ] , readLine , rIdx = -1;
  116.         format ( _CachedFiles[ CacheSlot ] , _MAX_FILE_NAME_LENGHT , File );
  117.         file = fopen ( FileName , io_readwrite );
  118.         while ( fread ( file , Line ) ) {
  119.             Line[ strlen ( Line ) -2 ] = '\0';
  120.             strmid ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , Line , 0 , strfind ( Line , "=" , false ) , _MAX_KEY_LENGHT );
  121.             strmid ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , Line , strfind ( Line , "=" , false ) + 1 , strlen ( Line ) , _MAX_VALUE_LENGHT );
  122.             if ( !strcmp ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , Key ) && strlen ( _FileCache[ CacheSlot ][ readLine ][ _Key ] ) )
  123.                 rIdx = readLine;
  124.             readLine++;
  125.         }
  126.         fclose ( file );
  127.         if ( rIdx != -1 )
  128.             return format ( Result , _MAX_VALUE_LENGHT , _FileCache[ CacheSlot ][ rIdx ][ _Value ] );
  129.         else {
  130.             format ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , _MAX_KEY_LENGHT , Key );
  131.             switch ( Type ) {
  132.                 case PAWN_TYPE_INT:
  133.                     format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , "0" );
  134.                 case PAWN_TYPE_FLOAT:
  135.                     format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , "0.0" );
  136.                 case PAWN_TYPE_ARRAY:
  137.                     format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , "" );
  138.             }
  139.         }
  140.         return _FileCache[ CacheSlot ][ readLine ][ _Value ];
  141.     }
  142.     else {
  143.         new CacheSlot = _GetFileCacheIndex ( File ) , Idx;
  144.         for ( ; Idx < _MAX_ENTRIES; Idx++ )
  145.             if ( !strcmp ( _FileCache[ CacheSlot ][ Idx ][ _Key ] , Key ) && strlen ( _FileCache[ CacheSlot ][ Idx ][ _Key ] ) )
  146.                 return format ( Result , _MAX_VALUE_LENGHT , _FileCache[ CacheSlot ][ Idx ][ _Value ] );
  147.         new AppendEntry = _GetFreeEntry ( CacheSlot );
  148.         if ( AppendEntry == -1 )
  149.             return print ( "HSA ERROR: Entries set too low!" );
  150.         format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Key ] , _MAX_KEY_LENGHT , Key );
  151.         switch ( Type ) {
  152.             case PAWN_TYPE_INT:
  153.                 format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] , _MAX_VALUE_LENGHT , "0" );
  154.             case PAWN_TYPE_FLOAT:
  155.                 format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] , _MAX_VALUE_LENGHT , "0.0" );
  156.             case PAWN_TYPE_ARRAY:
  157.                 format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] , _MAX_VALUE_LENGHT , "" );
  158.         }
  159.         return format ( Result , _MAX_VALUE_LENGHT , _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] );
  160.     }
  161. }
  162.  
  163. stock _SetData ( File[ ] , Key[ ] , Value[ ] )
  164. {
  165.     new FileName[ _MAX_FILE_NAME_LENGHT ] , File:file;
  166.     format ( FileName , _MAX_FILE_NAME_LENGHT , "%s.hsa" , File );
  167.     if ( _GetFileCacheIndex ( File ) == -1 ) {
  168.         new CacheSlot = _GetFreeCacheSlot ( ) , Line[ _MAX_LINE_CACHE  ] , readLine , rIdx = -1;
  169.         format ( _CachedFiles[ CacheSlot ] , _MAX_FILE_NAME_LENGHT , File );
  170.         file = fopen ( FileName , io_readwrite );
  171.         while ( fread ( file , Line ) ) {
  172.             Line[ strlen ( Line ) -2 ] = '\0';
  173.             strmid ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , Line , 0 , strfind ( Line , "=" , false ) , _MAX_KEY_LENGHT );
  174.             strmid ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , Line , strfind ( Line , "=" , false ) + 1 , strlen ( Line ) , _MAX_VALUE_LENGHT );
  175.             if ( !strcmp ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , Key ) && strlen ( _FileCache[ CacheSlot ][ readLine ][ _Key ] ) )
  176.                 rIdx = readLine;
  177.             readLine++;
  178.         }
  179.         fclose ( file );
  180.         if ( rIdx != -1 )
  181.             return format ( _FileCache[ CacheSlot ][ rIdx ][ _Value ] , _MAX_VALUE_LENGHT , Value );
  182.         else {
  183.             format ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , _MAX_KEY_LENGHT , Key );
  184.             return format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , Value );
  185.         }
  186.     }
  187.     else {
  188.         new CacheSlot = _GetFileCacheIndex ( File ) , Idx;
  189.         for ( ; Idx < _MAX_ENTRIES; Idx++ )
  190.             if ( !strcmp ( _FileCache[ CacheSlot ][ Idx ][ _Key ] , Key ) && strlen ( _FileCache[ CacheSlot ][ Idx ][ _Key ] ) )
  191.                 return format ( _FileCache[ CacheSlot ][ Idx ][ _Value ] , _MAX_VALUE_LENGHT , Value );
  192.         new AppendEntry = _GetFreeEntry ( CacheSlot );
  193.         if ( AppendEntry == -1 )
  194.             return print ( "HSA ERROR: Entries set too low!" );
  195.         format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Key ] , _MAX_KEY_LENGHT , Key );
  196.         return format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] , _MAX_VALUE_LENGHT , Value );
  197.     }
  198. }
  199.  
  200. stock _WriteFile ( File[ ] ) {
  201.     new FileName[ _MAX_FILE_NAME_LENGHT ] , File:file , Line[ _MAX_LINE_CACHE ] , CacheSlot = _GetFileCacheIndex ( File );
  202.     format ( FileName , _MAX_FILE_NAME_LENGHT , "%s.hsa" , File );
  203.     if ( _GetFileCacheIndex ( File ) == -1 )
  204.         return -1;
  205.     fremove ( FileName );
  206.     file = fopen ( FileName , io_append );
  207.     for ( new Idx; Idx < _MAX_ENTRIES; Idx++ ) {
  208.         if ( strlen ( _FileCache[ CacheSlot ][ Idx ][ _Key ] ) < 1 )
  209.             break;
  210.         format ( Line , _MAX_LINE_CACHE , "%s=%s\r\n" , _FileCache[ CacheSlot ][ Idx ][ _Key ] , _FileCache[ CacheSlot ][ Idx ][ _Value ] );
  211.         fwrite ( file , Line );
  212.     }
  213.     return fclose ( file );
  214. }
  215.  
  216. stock _WriteAll ( ) {
  217.     for ( new Slot; Slot < _MAX_FILES; Slot++ )
  218.     {
  219.         if ( strlen ( _CachedFiles[ Slot ] ) < 1 )
  220.             continue;
  221.         new FileName[ _MAX_FILE_NAME_LENGHT ] , File:file , Line[ _MAX_LINE_CACHE ];
  222.         format ( FileName , _MAX_FILE_NAME_LENGHT , "%s.hsa" , _CachedFiles[ Slot ] );
  223.         fremove ( FileName );
  224.         file = fopen ( FileName , io_append );
  225.         for ( new Idx; Idx < _MAX_ENTRIES; Idx++ ) {
  226.             if ( strlen ( _FileCache[ Slot ][ Idx ][ _Key ] ) < 1 )
  227.                 break;
  228.             format ( Line , _MAX_LINE_CACHE , "%s=%s\r\n" , _FileCache[ Slot ][ Idx ][ _Key ] , _FileCache[ Slot ][ Idx ][ _Value ] );
  229.             fwrite ( file , Line );
  230.         }
  231.         fclose ( file );
  232.     }
  233. }
  234.  
  235. stock _FreeCache ( ) {
  236.     for ( new Slot; Slot < _MAX_FILES; Slot++ )
  237.         _FreeSlot ( Slot );
  238. }
  239.  
  240. stock _FreeSlot ( Slot ) {
  241.     for ( new Idx; Idx < _MAX_ENTRIES; Idx++ ) {
  242.         _FileCache[ Slot ][ Idx ][ _Key ][ 0 ] = '\0';
  243.         _FileCache[ Slot ][ Idx ][ _Value ][ 0 ] = '\0';
  244.     }
  245. }
  246.  
  247. stock _GetFreeEntry ( Slot )
  248. {
  249.     for ( new Idx; Idx < _MAX_ENTRIES; Idx++ )
  250.         if ( strlen ( _FileCache[ Slot ][ Idx ][ _Key ] ) < 1 )
  251.             return Idx;
  252.     return -1;
  253. }
  254.  
  255. stock _GetFileCacheIndex ( File[ ] ) {
  256.     for ( new Idx; Idx < _MAX_FILES; Idx++ )
  257.         if ( !strcmp ( _CachedFiles[ Idx ] , File ) && strlen ( _CachedFiles[ Idx ] ) )
  258.             return Idx;
  259.     return -1;
  260. }
  261.  
  262. stock _GetFreeCacheSlot ( )
  263. {
  264.     for ( new index; index < _MAX_FILES; index++ )
  265.         if ( strlen ( _CachedFiles[ index ] ) == 0 )
  266.             return index;
  267.     return -1;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement