3 * @brief Mini Mbedthis Portable Runtime (MPR)
6 * Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
8 * This software is distributed under commercial and open source licenses.
9 * You may use the GPL open source license described below or you may acquire
10 * a commercial license from Mbedthis Software. You agree to be fully bound
11 * by the terms of either license. Consult the LICENSE.TXT distributed with
12 * this software for full details.
14 * This software is open source; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See the GNU General Public License for more
18 * details at: http://www.mbedthis.com/downloads/gplLicense.html
20 * This program is distributed WITHOUT ANY WARRANTY; without even the
21 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * This GPL license does NOT permit incorporating this software into
24 * proprietary programs. If you are unable to comply with the GPL, you must
25 * acquire a commercial license to use this software. Commercial licenses
26 * for this software and support services are available from Mbedthis
27 * Software at http://www.mbedthis.com
33 #include "param/param.h"
35 /************************************ Code ************************************/
37 #if !BLD_GOAHEAD_WEBSERVER
41 /* set the memory context to be used for all ejs variables */
42 void mprSetCtx(TALLOC_CTX *ctx)
47 /* return the memory context being used for all ejs variables */
53 /* return the loadparm context being used for all ejs variables */
54 struct loadparm_context *mprLpCtx(void)
56 return global_loadparm;
59 void mprFree(void *ptr)
64 void *mprMalloc(uint size)
66 return talloc_size(mpr_ctx, size);
69 /******************************************************************************/
71 void *mprRealloc(void *ptr, uint size)
73 return talloc_realloc_size(mpr_ctx, ptr, size);
76 /******************************************************************************/
78 char *mprStrdup(const char *str)
83 return talloc_strdup(mpr_ctx, str);
86 /*****************************************************************************/
88 int mprAllocSprintf(char **msgbuf, int maxSize, const char *fmt, ...)
95 buf = mprMalloc(maxSize + 1);
96 count = mtVsprintf(buf, maxSize, fmt, args);
102 /*****************************************************************************/
104 int mprAllocVsprintf(char **msgbuf, int maxSize, const char *fmt, va_list args)
109 buf = mprMalloc(maxSize + 1);
110 count = mtVsprintf(buf, maxSize, fmt, args);
116 /*****************************************************************************/
118 * Format a number as a string. FUTURE -- reverse args to be standard.
119 * ie. mprItoa(char *userBuf, int bufsize, int value);
122 char *mprItoa(int value, char *buf, int width)
125 char *cp, *dp, *endp;
128 cp = &numBuf[sizeof(numBuf)];
140 *--cp = '0' + (value % 10);
150 while (dp < endp && *cp) {
157 /*****************************************************************************/
159 void mprLog(int level, const char *fmt, ...)
164 if (DEBUGLVL(level)) {
166 mprAllocVsprintf(&buf, MPR_MAX_STRING, fmt, args);
168 DEBUG(level, ("mprLog: %s", buf));
173 /*****************************************************************************/
175 void mprBreakpoint(const char *file, int line, const char *cond)
178 mprAllocSprintf(&buf, MPR_MAX_STRING, "esp exception - ASSERT at %s:%d, %s\n",
183 #endif /* !BLD_GOAHEAD_WEBSERVER */
184 /*****************************************************************************/
186 * Create a general growable array structure
189 MprArray *mprCreateArray()
194 array = (MprArray*) mprMalloc(sizeof(MprArray));
198 memset(array, 0, sizeof(MprArray));
200 size = MPR_ARRAY_INCR * sizeof(void*);
201 array->handles = (void**) mprMalloc(size);
202 if (array->handles == 0) {
206 memset(array->handles, 0, size);
207 array->max = MPR_ARRAY_INCR;
212 /*****************************************************************************/
214 * Dispose of the array. Callers responsibility to dispose of handle entries.
217 void mprDestroyArray(MprArray *array)
220 mprAssert(array->max >= 0);
221 mprAssert(array->used >= 0);
223 mprFree(array->handles);
227 /*****************************************************************************/
229 * Add an item to the array
232 int mprAddToArray(MprArray *array, void *item)
234 int memsize, idx, len;
237 mprAssert(array->max >= 0);
238 mprAssert(array->used >= 0);
240 if (array->used < array->max) {
242 mprAssert(idx >= 0 && idx < array->max);
243 mprAssert(array->handles[idx] == 0);
244 array->handles[idx] = item;
248 for (idx = array->used; idx < array->max; idx++) {
249 if (array->handles[idx] == 0) {
251 mprAssert(array->handles[idx] == 0);
252 array->handles[idx] = item;
257 len = array->max + MPR_ARRAY_INCR;
258 memsize = len * sizeof(void*);
259 array->handles = (void**) mprRealloc((void*) array->handles, memsize);
260 if (array->handles == NULL) {
263 memset(&array->handles[array->max], 0, sizeof(void*) * MPR_ARRAY_INCR);
267 mprAssert(idx >= 0 && idx < array->max);
268 mprAssert(array->handles[idx] == 0);
270 array->handles[idx] = item;
274 /*****************************************************************************/
276 * Remove from the array
279 int mprRemoveFromArray(MprArray *array, int idx)
282 mprAssert(array->max > 0);
283 mprAssert(idx >= 0 && idx < array->max);
284 mprAssert(array->handles[idx] != 0);
285 mprAssert(array->used > 0);
287 array->handles[idx] = 0;
288 return --array->used;
291 /*****************************************************************************/
293 * Thread-safe wrapping of strtok. Note "str" is modifed as per strtok()
296 char *mprStrTok(char *str, const char *delim, char **tok)
301 start = str ? str : *tok;
307 i = strspn(start, delim);
309 if (*start == '\0') {
313 end = strpbrk(start, delim);
316 i = strspn(end, delim);
323 /*****************************************************************************/
325 static int mprCoreStrcat(int alloc, char **destp, int destMax, int existingLen,
326 const char *delim, const char *src, va_list args)
331 int sepLen, addBytes, required;
334 mprAssert(destMax > 0);
338 sepLen = (delim) ? strlen(delim) : 0;
344 addBytes += strlen(str) + sepLen;
345 str = va_arg(ap, const char*);
349 if (existingLen > 0) {
352 required = existingLen + addBytes + 1;
353 if (required >= destMax) {
355 return MPR_ERR_WONT_FIT;
360 dest = (char*) mprMalloc(required);
362 dest = (char*) mprRealloc(dest, required);
365 dest = (char*) *destp;
368 dp = &dest[existingLen];
380 str = va_arg(ap, char*);
387 } else if (dest == 0) {
388 dest = (char*) mprMalloc(1);
393 mprAssert(dp < &dest[required]);
397 /*****************************************************************************
398 Note that this VARARGS function must be NULL (not 0, this must be a
402 int mprReallocStrcat(char **destp, int destMax, int existingLen,
403 const char *delim, const char *src,...)
409 rc = mprCoreStrcat(1, destp, destMax, existingLen, delim, src, ap);
414 /*****************************************************************************/
416 * Return the directory portion of a pathname into the users buffer.
419 int mprGetDirName(char *buf, int bufsize, char *path)
426 mprAssert(bufsize > 0);
428 cp = strrchr(path, '/');
431 cp = strrchr(path, '\\');
440 if (cp == path && cp[1] == '\0') {
446 if (dlen < bufsize) {
450 mprMemcpy(buf, bufsize, path, dlen);
454 return MPR_ERR_WONT_FIT;
457 /*****************************************************************************/
459 int mprStrcpy(char *dest, int destMax, const char *src)
464 mprAssert(destMax > 0);
468 if (len >= destMax && len > 0) {
470 return MPR_ERR_WONT_FIT;
473 memcpy(dest, src, len);
482 /*****************************************************************************/
484 int mprMemcpy(char *dest, int destMax, const char *src, int nbytes)
487 mprAssert(destMax > nbytes);
489 mprAssert(nbytes > 0);
491 if (nbytes > destMax) {
493 return MPR_ERR_WONT_FIT;
496 memcpy(dest, src, nbytes);
503 /*****************************************************************************/
505 void miniMprDummy() {}
506 #endif // !BLD_APPWEB && !BLD_GOAHEAD_WEBSERVER
514 * vim600: sw=4 ts=4 fdm=marker