1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/**
* SAMPUTIL.C -- Utility methods to convert struct pointers to user handles.
*
* @brief Utility methods to convert struct pointers to user handles.
*
* @file sampUtil.c
* @author Mike Fitzpatrick
* @date 7/10/09
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "samp.h"
#define MAX_HANDLES 128
int numHandles = 0;
long sampHandles[MAX_HANDLES];
/* Public procedures
*/
handle_t samp_P2H (void *ptr);
void *samp_H2P (handle_t handle);
handle_t samp_newHandle (void *ptr);
void samp_freeHandle (handle_t handle);
/* Utility routines for keep track of handles.
*/
/**
* SAMP_NEWHANDLE -- Get an unused object handle.
*
* @brief Get an unused object handle
* @fn handle_t samp_newHandle (void *ptr)
*
* @param ptr pointer to object to be stored
* @return new object handle
*/
handle_t
samp_newHandle (void *ptr)
{
/* Initialize the handle-to-ptr converter the first time we're called,
** or whenever we've restarted.
*/
if (numHandles == 0)
memset (sampHandles, 0, sizeof (sampHandles));
sampHandles[++numHandles] = (long) ptr;
return (numHandles);
}
/**
* SAMP_FREEHANDLE -- Free the handle for later re-use.
*
* @brief Free the handle for later re-use.
* @fn samp_freeHandle (handle_t handle)
*
* @param handle object handle
* @return nothing
*/
void
samp_freeHandle (handle_t handle)
{
register int i, j;
void *ptr = samp_H2P (handle);
if (handle <= 0) {
fprintf (stderr, "Error: Attempt to free zero handle!\n");
return;
}
for (i=1; i < MAX_HANDLES; i++) {
if ((void *) ptr == (void *) sampHandles[i]) {
for (j=i+1; j < MAX_HANDLES; j++) {
if (sampHandles[j])
sampHandles[i++] = sampHandles[j];
else
break;
}
numHandles = ((numHandles-1) >= 0 ? (numHandles-1) : 0);
break;
}
}
}
/**
* SAMP_P2H -- Convert a pointer to a handle
*
* @brief Convert a pointer to a handle
* @fn handle_t samp_P2H (void *ptr)
*
* @param ptr pointer to object
* @return handle to object, < 0 on error
*/
handle_t
samp_P2H (void *ptr)
{
register int i;
for (i=1; i < MAX_HANDLES; i++) {
if ((void *) ptr == (void *) sampHandles[i])
return ((int) i);
}
return (-1);
}
/**
* SAMP_H2P -- Convert a handle to a pointer
*
* @brief Convert a handle to a pointer
* @fn void *samp_H2P (int handle)
*
* @param handle object handle
* @return pointer to object or NULL
*/
void *
samp_H2P (handle_t handle)
{
return ((void *) sampHandles[handle]);
}
/**
* SAMP_APP2ID -- Convert an application name to a public-ID.
*
* @brief Convert an application name to a public-ID.
* @fn pubId = samp_app2id (handle_t handle, char *appName)
*
* @param handle samp struct handle
* @param appName name of registered application
* @return public ID of application
*/
char *
samp_app2id (handle_t handle, char *appName)
{
Samp *samp = samp_H2P(handle);
register int i = 0;
for (i=0; i < samp->nclients; i++)
if (strcasecmp (appName, samp->clients[i].name) == 0)
return (samp->clients[i].pubId);
return (appName); /* Not found, return input appName */
}
/**
* SAMP_ID2APP -- Convert a public-ID to the application name.
*
* @brief Convert a public-ID to the application name.
* @fn appName = samp_id2app (handle_t handle, char *pubId)
*
* @param handle samp struct handle
* @param pubId public ID of application
* @return name of registered application
*/
char *
samp_id2app (handle_t handle, char *pubId)
{
Samp *samp = samp_H2P(handle);
register int i = 0;
for (i=0; i < samp->nclients; i++)
if (strcasecmp (pubId, samp->clients[i].pubId) == 0)
return (samp->clients[i].name);
return (pubId); /* Not found, return input pubId */
}
/**
* SAMP_SERVERPORT -- Return a unique port number for the server.
*
* @brief Return a unique port number for the server.
* @fn port = samp_serverPort (void)
*
* @return port number
*/
int
samp_serverPort ()
{
static int port = 0;
if ( port == 0 )
port = (DEF_PORT + (getpid() % 32768));
return ( port );
}
/**
* SAMP_PRINTMETADATA -- Print the metadata for the application.
*
* @brief Print the metadata for the application.
* @fn samp_printMetadata (handle_t handle, String name)
*
* @param handle samp handle
* @return nothing
*/
void
samp_printMetadata (handle_t handle, String name)
{
Samp *sampP = samp_H2P (handle);
register int i;
if (!name || strcmp (name, "samp.name") == 0)
printf ((!name ? "samp.name: %s\n" : "%s"),
sampP->appName);
if (!name || strcmp (name, "samp.description.text") == 0)
printf ((!name ? "samp.description.text: %s\n" : "%s"),
sampP->description);
if (!name || strcmp (name, "samp.icon.url") == 0)
printf ((!name ? "samp.icon.url: %s\n" : "%s"),
sampP->meta.iconURL);
if (!name || strcmp (name, "samp.documentation.url") == 0)
printf ((!name ? "samp.documentation.url: %s\n" : "%s"),
sampP->meta.docURL);
for (i=0; i < sampP->meta.nkeys; i++) {
if (!name) {
printf ("%s: %s\n", sampP->meta.aKey[i], sampP->meta.aVal[i]);
} else if (strcmp (name, sampP->meta.aKey[i]) == 0) {
if (!name)
printf ("%s: %s\n", sampP->meta.aKey[i], sampP->meta.aVal[i]);
else
printf ("%s\n", sampP->meta.aVal[i]);
}
}
}
/**
* SAMP_GETMETADATA -- Get the metadata item for the application.
*
* @brief Get the metadata item for the application.
* @fn str = samp_getMetadata (handle_t handle, String param)
*
* @param handle samp handle
* @return nothing
*/
char *
samp_getMetadata (handle_t handle, String name)
{
Samp *sampP = samp_H2P (handle);
register int i;
if (!name)
return (NULL);
if (strcmp (name, "samp.name") == 0)
return (sampP->appName);
if (strcmp (name, "samp.description.text") == 0)
return (sampP->description);
if (strcmp (name, "samp.icon.url") == 0)
return (sampP->meta.iconURL);
if (strcmp (name, "samp.documentation.url") == 0)
return (sampP->meta.docURL);
for (i=0; i < sampP->meta.nkeys; i++) {
if (strcmp (name, sampP->meta.aKey[i]) == 0)
return (sampP->meta.aVal[i]);
}
return (NULL);
}
|