src/hcode/glue.hcode.c

Go to the documentation of this file.
00001 
00002 /*
00003  * $Id: glue.hcode.c,v 1.1 2005/06/13 20:50:49 murrayma Exp $
00004  *
00005  * Author: Markus Stenberg <fingon@iki.fi>
00006  *
00007  *  Copyright (c) 1997 Markus Stenberg
00008  *  Copyright (c) 1998-2002 Thomas Wouters 
00009  *  Copyright (c) 2000-2002 Cord Awtry 
00010  *       All rights reserved
00011  *
00012  * Created: Mon Oct 20 18:53:30 1997 fingon
00013  * Last modified: Sat Jun  6 18:43:51 1998 fingon
00014  *
00015  */
00016 
00017 #include "copyright.h"
00018 #include "config.h"
00019 
00020 #include "db.h"
00021 #include "externs.h"
00022 #include "mech.h"
00023 #include "macros.h"
00024 #include "p.glue.h"
00025 
00026 void set_attr_internal(dbref player, dbref thing, int attrnum,
00027                                            char *attrtext, int key);
00028 
00029 int bt_get_attr(char *tbuf, int obj, char *name)
00030 {
00031         ATTR *a;
00032         int ao, af;
00033 
00034         if(!name)
00035                 return 0;
00036         if(!(a = atr_str(name)))
00037                 return 0;
00038         atr_get_str(tbuf, obj, a->number, &ao, &af);
00039         if(*tbuf)
00040                 return 1;
00041         return 0;
00042 }
00043 
00044 char *silly_atr_get(int id, int flag)
00045 {
00046         int i, j;
00047         static char buf[LBUF_SIZE];
00048 
00049         atr_get_str(buf, id, flag, &i, &j);
00050         return buf;
00051 #if 0                                                   /* This would waste memory, so.. :P */
00052         return atr_pget(id, flag, &i, &j);
00053 #endif
00054 }
00055 
00056 void silly_atr_set(int id, int flag, char *dat)
00057 {
00058         atr_add_raw(id, flag, dat);
00059 }
00060 
00061 void bt_set_attr(dbref obj, char *attri, char *value)
00062 {
00063         int attr;
00064 
00065         ATTR *atr;
00066 
00067         atr = atr_str(attri);
00068         attr = atr ? atr->number : mkattr(attri);
00069         set_attr_internal(GOD, obj, attr, value, SET_QUIET);
00070 }
00071 
00072 void KillText(char **mapt)
00073 {
00074         int i;
00075 
00076         for(i = 0; mapt[i]; i++)
00077                 free(mapt[i]);
00078         free(mapt);
00079 }
00080 
00081 void ShowText(char **mapt, dbref player)
00082 {
00083         int i;
00084 
00085         for(i = 0; mapt[i]; i++)
00086                 notify(player, mapt[i]);
00087 }
00088 
00089 int BOUNDED(int min, int val, int max)
00090 {
00091         if(val < min)
00092                 return min;
00093         if(val > max)
00094                 return max;
00095         return val;
00096 }
00097 
00098 float FBOUNDED(float min, float val, float max)
00099 {
00100         if(val < min)
00101                 return min;
00102         if(val > max)
00103                 return max;
00104         return val;
00105 }
00106 
00107 int MAX(int v1, int v2)
00108 {
00109         if(v1 > v2)
00110                 return v1;
00111         return v2;
00112 }
00113 
00114 int MIN(int v1, int v2)
00115 {
00116         if(v1 < v2)
00117                 return v1;
00118         return v2;
00119 }
00120 
00121 /*
00122  * Gets the first parameter from a string
00123  * and returns it.
00124  */
00125 char *first_parseattribute(char *buffer)
00126 {
00127 
00128         int length;
00129         char *start, *first;
00130 
00131         /* Look for the first parameter */
00132         start = buffer;
00133         length = strcspn(start, " \t=");
00134 
00135         /* If the first parameter is to big set the size */
00136         if(length > SBUF_SIZE)
00137                 length = SBUF_SIZE;
00138 
00139         /* Make it and return it */
00140         first = (char *) strndup(start, length);
00141 
00142         return first;
00143 
00144 }
00145 
00146 /*
00147  * proper_parseattributes
00148  *
00149  * Split user input on whitespace delimeters, including '='.
00150  *
00151  *
00152  * This function is designed to be used for commands of the nature
00153  *
00154  * COMMAND KEY=VALUE
00155  *
00156  * It expects the null terminated source character buffer as the firs
00157  * argument, buffer.
00158  *
00159  * It will duplicate each, space, tab, or equal-sign delimeted field
00160  * and assign a pointer to it in the string pointer array, args, in
00161  * ascending order for up to max-1 fields.
00162  *
00163  * All remaining input, if any, will be duplicated and the address of
00164  * duplicate string will be assigned as the last entry in args.
00165  *
00166  * For the above example, the result would be a set of strings with
00167  * the contents:
00168  *
00169  * "COMMAND", "=", "KEY", "VALUE"
00170  *
00171  * NOTE: it is the caller's responsibility to free the duplicated
00172  * strings.
00173  */
00174 
00175 int proper_parseattributes(char *buffer, char **args, int max)
00176 {
00177         int count = 0, iterator = 0, length;
00178         char *start, *finish;
00179 
00180         memset(args, 0, sizeof(char *) * max);
00181 
00182         start = buffer;
00183         while (count < (max - 1) && *start) {
00184                 if(*start == '=') {
00185                         args[count++] = strndup(start, 1);
00186                         start++;
00187                         continue;
00188                 }
00189                 length = strcspn(start, " \t=");
00190                 args[count++] = strndup(start, length);
00191                 start += length;
00192                 if(*start != '=' && *start != '\x0')
00193                         start++;
00194         }
00195         if(*start) {
00196                 args[max - 1] = strdup(start);
00197                 count++;
00198         }
00199         return count;
00200 }
00201 
00202 int silly_parseattributes(char *buffer, char **args, int max)
00203 {
00204         char bufferi[LBUF_SIZE], foobuff[LBUF_SIZE];
00205         char *a, *b;
00206         int count = 0;
00207         char *parsed = buffer;
00208         int num_args = 0;
00209 
00210         memset(args, 0, sizeof(char *) * max);
00211 
00212         b = bufferi;
00213         for(a = buffer; *a && a; a++)
00214                 if(*a == '=') {
00215                         *(b++) = ' ';
00216                         *(b++) = '=';
00217                         *(b++) = ' ';
00218                 } else
00219                         *(b++) = *a;
00220         *b = 0;
00221         /* Got da silly string in bufferi variable */
00222 
00223         while ((count < max) && parsed) {
00224                 if(!count) {
00225                         /* first time through */
00226                         parsed = strtok(bufferi, " \t");
00227                 } else {
00228                         parsed = strtok(NULL, " \t");
00229                 }
00230                 args[count] = parsed;   /* Set the args pointer */
00231                 if(parsed)
00232                         num_args++;                     /* Actual count of arguments */
00233                 count++;                                /* Loop to make sure we don't overrun our */
00234                 /* buffer */
00235         }
00236         /* Hrm. Now all we gotta do is append -rest- of data to end of _last_ arg */
00237         if(args[max - 1] && args[max - 1][0]) {
00238                 strcpy(foobuff, args[max - 1]);
00239                 while ((parsed = strtok(NULL, " \t")))
00240                         sprintf(foobuff + strlen(foobuff), " %s", parsed);
00241                 args[max - 1] = foobuff;
00242         }
00243         return num_args;
00244 }
00245 
00246 /*
00247  * proper_explodearguments
00248  *
00249  * Split user input on whitespace delimeters.
00250  *
00251  *
00252  * This function is designed to be used for commands of the nature
00253  *
00254  * COMMAND VALUE1 VALUE2 VALUE3
00255  *
00256  * It expects the null terminated source character buffer as the firs
00257  * argument, buffer.
00258  *
00259  * It will duplicate each space or tab delimeted field and assign a 
00260  * pointer to it in the string pointer array, args, in ascending order 
00261  * for up to max-1 fields.
00262  *
00263  * All remaining input, if any, will be duplicated and the address of
00264  * duplicate string will be assigned as the last entry in args.
00265  *
00266  * For the above example, the result would be a set of strings with
00267  * the contents:
00268  *
00269  * "COMMAND", "VALUE1", "VALUE2", "VALUE3"
00270  *
00271  * NOTE: it is the caller's responsibility to free the duplicated
00272  * strings.
00273  */
00274 
00275 
00276 int proper_explodearguments(char *buffer, char **args, int max)
00277 {
00278         int count = 0, iterator = 0, length;
00279         char *start, *finish;
00280 
00281         memset(args, 0, sizeof(char *) * max);
00282 
00283         start = buffer;
00284         while (count < max - 1 && *start) {
00285                 length = strcspn(start, " \t");
00286                 args[count++] = strndup(start, length);
00287                 start += length;
00288                 if(*start != '\x0')
00289                         start++;
00290         }
00291         if(*start) {
00292                 args[max - 1] = strdup(start);
00293                 count++;
00294         }
00295         return count;
00296 }
00297 
00298 int mech_parseattributes(char *buffer, char **args, int maxargs)
00299 {
00300         int count = 0;
00301         char *parsed = buffer;
00302         int num_args = 0;
00303 
00304         memset(args, 0, sizeof(char *) * maxargs);
00305 
00306         while ((count < maxargs) && parsed) {
00307                 parsed = strtok(!count ? buffer : NULL, " \t");
00308                 args[count] = parsed;   /* Set the args pointer */
00309                 if(parsed)
00310                         num_args++;                     /* Actual count of arguments */
00311                 count++;                                /* Loop to make sure we don't overrun our */
00312                 /* buffer */
00313         }
00314         return num_args;
00315 }

Generated on Mon May 28 04:25:24 2007 for BattletechMUX by  doxygen 1.4.7