View difference between Paste ID: BMn4JAUT and ScLKZLcF
SHOW: | | - or go back to the newest paste.
1-
// HSA 2.1 (crypt) include by Hauke Marquardt alias |-|auke - 04.04.2012
1+
// HSA 2.1.1 (crypt) include by Hauke Marquardt alias |-|auke - 06.04.2012
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
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           	32 + _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
#define _STRING_KEY                         72635
53
#define _STRING_KEY_OFFSET                  28364
54
#define _ENCRYPT                            0
55
#define _DECRYPT                            1
56
57
#if !defined _h_library_norm_included
58
    #define PAWN_TYPE_INT                   1
59
    #define PAWN_TYPE_FLOAT                 2
60
    #define PAWN_TYPE_ARRAY                 3
61
    #define _h_library_norm_included
62
#endif
63
64
#define getInt:%1(%2)                   	_GetInt(%1,%2)
65
#define getFloat:%1(%2)                 	_GetFloat(%1,%2)
66
#define getString:%1(%2)                	_GetArray(%1,%2)
67
#define setInt:%1(%2,%3)                	_SetInt(%1,%2,%3)
68
#define setFloat:%1(%2,%3)              	_SetFloat(%1,%2,%3)
69
#define setString:%1(%2,%3)     			_SetArray(%1,%2,%3)
70
#define Save(%1)                         	_WriteFile(%1)
71
#define SaveAll(%1)                         _WriteAll(%1)
72
73
new _CachedFiles[ _MAX_FILES ][ _MAX_FILE_NAME_LENGHT ];
74
enum _LineCache
75
{
76
	_Key[ _MAX_KEY_LENGHT ],
77
	_Value[ _MAX_VALUE_LENGHT ]
78
}
79
new _FileCache[ _MAX_FILES ][ _MAX_ENTRIES ][ _LineCache ];
80
81
stock _GetInt ( File[ ] , Key[ ] ) {
82
    new Result[ _MAX_VALUE_LENGHT ];
83
    _GetData ( File , Key , Result , PAWN_TYPE_INT );
84
    return strval ( Result );
85
}
86
87
stock Float:_GetFloat ( File[ ] , Key[ ] ) {
88
    new Result[ _MAX_VALUE_LENGHT ];
89
    _GetData ( File , Key , Result , PAWN_TYPE_FLOAT );
90
    return floatstr ( Result );
91
}
92
93
stock _GetArray ( File[ ] , Key[ ] ) {
94
    new Result[ _MAX_VALUE_LENGHT ];
95
    _GetData ( File , Key , Result , PAWN_TYPE_ARRAY );
96
    return Result;
97
}
98
99
stock _SetInt ( File[ ] , Key[ ] , Value ) {
100
    new uValue[ _MAX_LINE_CACHE ];
101
    format ( uValue , _MAX_LINE_CACHE , "%d" , Value );
102
    _SetData ( File , Key , uValue );
103
}
104
105
stock _SetFloat ( File[ ] , Key[ ] , Float:Value ) {
106
    new uValue[ _MAX_LINE_CACHE ];
107
    format ( uValue , _MAX_LINE_CACHE , "%f" , Value );
108
    _SetData ( File , Key , uValue );
109
}
110
111-
stock _SetArray ( File[ ] , Key[ ] , Value[ _MAX_LINE_CACHE ] ) {
111+
stock _SetArray ( File[ ] , Key[ ] , Value[ ] ) {
112
    _SetData ( File , Key , Value );
113
}
114
115
stock _GetData ( File[ ] , Key[ ] , Result[ ] , Type ) {
116
    new FileName[ _MAX_FILE_NAME_LENGHT ] , File:file;
117
    format ( FileName , _MAX_FILE_NAME_LENGHT , "%s.hsa" , File );
118
    if ( _GetFileCacheIndex ( File ) == -1 ) {
119
		new CacheSlot = _GetFreeCacheSlot ( ) , Line[ _MAX_LINE_CACHE  ] , readLine , rIdx = -1;
120
		format ( _CachedFiles[ CacheSlot ] , _MAX_FILE_NAME_LENGHT , File );
121
        file = fopen ( FileName , io_readwrite );
122
		while ( fread ( file , Line ) ) {
123
	        Line[ strlen ( Line ) -2 ] = '\0';
124
	        strmid ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , Line , 0 , strfind ( Line , "=" , false ) , _MAX_KEY_LENGHT );
125
	        strmid ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , Line , strfind ( Line , "=" , false ) + 1 , strlen ( Line ) , _MAX_VALUE_LENGHT );
126
	        format ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , _MAX_KEY_LENGHT , _StringModificator ( _DECRYPT , _FileCache[ CacheSlot ][ readLine ][ _Key ] ) );
127
	        format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , _StringModificator ( _DECRYPT , _FileCache[ CacheSlot ][ readLine ][ _Value ] ) );
128
	        if ( !strcmp ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , Key ) && strlen ( _FileCache[ CacheSlot ][ readLine ][ _Key ] ) )
129
				rIdx = readLine;
130
	        readLine++;
131
		}
132
		fclose ( file );
133
		if ( rIdx != -1 )
134
			return format ( Result , _MAX_VALUE_LENGHT , _FileCache[ CacheSlot ][ rIdx ][ _Value ] );
135
		else {
136
		    format ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , _MAX_KEY_LENGHT , Key );
137
		    switch ( Type ) {
138
		        case PAWN_TYPE_INT:
139
		            format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , "0" );
140
		        case PAWN_TYPE_FLOAT:
141
		            format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , "0.0" );
142
		        case PAWN_TYPE_ARRAY:
143
		            format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , "" );
144
		    }
145
		}
146
		return _FileCache[ CacheSlot ][ readLine ][ _Value ];
147
 	}
148
    else {
149
		new CacheSlot = _GetFileCacheIndex ( File ) , Idx;
150
		for ( ; Idx < _MAX_ENTRIES; Idx++ )
151
		    if ( !strcmp ( _FileCache[ CacheSlot ][ Idx ][ _Key ] , Key ) && strlen ( _FileCache[ CacheSlot ][ Idx ][ _Key ] ) )
152
		        return format ( Result , _MAX_VALUE_LENGHT , _FileCache[ CacheSlot ][ Idx ][ _Value ] );
153
	    new AppendEntry = _GetFreeEntry ( CacheSlot );
154
	    if ( AppendEntry == -1 )
155
	        return print ( "HSA ERROR: Entries set too low!" );
156
  		format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Key ] , _MAX_KEY_LENGHT , Key );
157
        switch ( Type ) {
158
	        case PAWN_TYPE_INT:
159
	            format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] , _MAX_VALUE_LENGHT , "0" );
160
	        case PAWN_TYPE_FLOAT:
161
	            format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] , _MAX_VALUE_LENGHT , "0.0" );
162
	        case PAWN_TYPE_ARRAY:
163
	            format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] , _MAX_VALUE_LENGHT , "" );
164
	    }
165
	    return format ( Result , _MAX_VALUE_LENGHT , _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] );
166
	}
167
}
168
169
stock _SetData ( File[ ] , Key[ ] , Value[ ] )
170
{
171
    new FileName[ _MAX_FILE_NAME_LENGHT ] , File:file;
172
    format ( FileName , _MAX_FILE_NAME_LENGHT , "%s.hsa" , File );
173
    if ( _GetFileCacheIndex ( File ) == -1 ) {
174
        new CacheSlot = _GetFreeCacheSlot ( ) , Line[ _MAX_LINE_CACHE  ] , readLine , rIdx = -1;
175
		format ( _CachedFiles[ CacheSlot ] , _MAX_FILE_NAME_LENGHT , File );
176
        file = fopen ( FileName , io_readwrite );
177
		while ( fread ( file , Line ) ) {
178
	        Line[ strlen ( Line ) -2 ] = '\0';
179
	        strmid ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , Line , 0 , strfind ( Line , "=" , false ) , _MAX_KEY_LENGHT );
180
	        strmid ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , Line , strfind ( Line , "=" , false ) + 1 , strlen ( Line ) , _MAX_VALUE_LENGHT );
181
	        format ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , _MAX_KEY_LENGHT , _StringModificator ( _DECRYPT , _FileCache[ CacheSlot ][ readLine ][ _Key ] ) );
182
	        format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , _StringModificator ( _DECRYPT , _FileCache[ CacheSlot ][ readLine ][ _Value ] ) );
183
	        if ( !strcmp ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , Key ) && strlen ( _FileCache[ CacheSlot ][ readLine ][ _Key ] ) )
184
	            rIdx = readLine;
185
	        readLine++;
186
		}
187
		fclose ( file );
188
		if ( rIdx != -1 )
189
		    return format ( _FileCache[ CacheSlot ][ rIdx ][ _Value ] , _MAX_VALUE_LENGHT , Value );
190
		else {
191
		    format ( _FileCache[ CacheSlot ][ readLine ][ _Key ] , _MAX_KEY_LENGHT , Key );
192
		    return format ( _FileCache[ CacheSlot ][ readLine ][ _Value ] , _MAX_VALUE_LENGHT , Value );
193
		}
194
    }
195
    else {
196
        new CacheSlot = _GetFileCacheIndex ( File ) , Idx;
197
		for ( ; Idx < _MAX_ENTRIES; Idx++ )
198
		    if ( !strcmp ( _FileCache[ CacheSlot ][ Idx ][ _Key ] , Key ) && strlen ( _FileCache[ CacheSlot ][ Idx ][ _Key ] ) )
199
		        return format ( _FileCache[ CacheSlot ][ Idx ][ _Value ] , _MAX_VALUE_LENGHT , Value );
200
	    new AppendEntry = _GetFreeEntry ( CacheSlot );
201
	    if ( AppendEntry == -1 )
202
	        return print ( "HSA ERROR: Entries set too low!" );
203
  		format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Key ] , _MAX_KEY_LENGHT , Key );
204
        return format ( _FileCache[ CacheSlot ][ AppendEntry ][ _Value ] , _MAX_VALUE_LENGHT , Value );
205
    }
206
}
207
208
stock _WriteFile ( File[ ] ) {
209
    new FileName[ _MAX_FILE_NAME_LENGHT ] , File:file , Line[ _MAX_LINE_CACHE ] , CacheSlot = _GetFileCacheIndex ( File );
210
    format ( FileName , _MAX_FILE_NAME_LENGHT , "%s.hsa" , File );
211
    if ( _GetFileCacheIndex ( File ) == -1 )
212
        return -1;
213
	fremove ( FileName );
214
    file = fopen ( FileName , io_append );
215
    for ( new Idx; Idx < _MAX_ENTRIES; Idx++ ) {
216
        if ( strlen ( _FileCache[ CacheSlot ][ Idx ][ _Key ] ) < 1 )
217
            break;
218
        format ( Line , _MAX_LINE_CACHE , "%s=%s\r\n" , _StringModificator ( _ENCRYPT , _FileCache[ CacheSlot ][ Idx ][ _Key ] ) , _StringModificator ( _ENCRYPT , _FileCache[ CacheSlot ][ Idx ][ _Value ] ) );
219
        fwrite ( file , Line );
220
    }
221
    return fclose ( file );
222
}
223
224
stock _WriteAll ( ) {
225
	for ( new Slot; Slot < _MAX_FILES; Slot++ )
226
	{
227
	    if ( strlen ( _CachedFiles[ Slot ] ) < 1 )
228
	        continue;
229
	    new FileName[ _MAX_FILE_NAME_LENGHT ] , File:file , Line[ _MAX_LINE_CACHE ];
230
	    format ( FileName , _MAX_FILE_NAME_LENGHT , "%s.hsa" , _CachedFiles[ Slot ] );
231
		fremove ( FileName );
232
	    file = fopen ( FileName , io_append );
233
	    for ( new Idx; Idx < _MAX_ENTRIES; Idx++ ) {
234
	        if ( strlen ( _FileCache[ Slot ][ Idx ][ _Key ] ) < 1 )
235
	            break;
236
	        format ( Line , _MAX_LINE_CACHE , "%s=%s\r\n" , _StringModificator ( _ENCRYPT , _FileCache[ CacheSlot ][ Idx ][ _Key ] ) , _StringModificator ( _ENCRYPT , _FileCache[ CacheSlot ][ Idx ][ _Value ] ) );
237
	        fwrite ( file , Line );
238
	    }
239
	    fclose ( file );
240
	}
241
}
242
243
stock _FreeCache ( ) {
244
    for ( new Slot; Slot < _MAX_FILES; Slot++ )
245
        _FreeSlot ( Slot );
246
}
247
248
stock _FreeSlot ( Slot ) {
249
    for ( new Idx; Idx < _MAX_ENTRIES; Idx++ ) {
250
		_FileCache[ Slot ][ Idx ][ _Key ][ 0 ] = '\0';
251
		_FileCache[ Slot ][ Idx ][ _Value ][ 0 ] = '\0';
252
	}
253
}
254
255
stock _GetFreeEntry ( Slot )
256
{
257
	for ( new Idx; Idx < _MAX_ENTRIES; Idx++ )
258
	    if ( strlen ( _FileCache[ Slot ][ Idx ][ _Key ] ) < 1 )
259
	        return Idx;
260
	return -1;
261
}
262
263
stock _GetFileCacheIndex ( File[ ] ) {
264
	for ( new Idx; Idx < _MAX_FILES; Idx++ )
265
	    if ( !strcmp ( _CachedFiles[ Idx ] , File ) && strlen ( _CachedFiles[ Idx ] ) )
266
	        return Idx;
267
	return -1;
268
}
269
270
stock _GetFreeCacheSlot ( )
271
{
272
    for ( new index; index < _MAX_FILES; index++ )
273
	    if ( strlen ( _CachedFiles[ index ] ) == 0 )
274
	        return index;
275
	return -1;
276
}
277
278
stock _StringModificator ( Method , String[ ] ) {
279
    new Converted[ _MAX_LINE_CACHE ];
280
    for ( new i = 0; i < strlen ( String ); i++ )
281
    Converted[ i ] = Method == _ENCRYPT ? ( String[ i ] - _STRING_KEY ) + _STRING_KEY_OFFSET : ( String[ i ] + _STRING_KEY ) - _STRING_KEY_OFFSET;
282
    return Converted;
283
}