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
32 #include "web_server/ejs/miniMpr.h"
34 /************************************ Code ************************************/
36 #if !BLD_GOAHEAD_WEBSERVER
38 static TALLOC_CTX *mpr_ctx;
40 void mprSetCtx(TALLOC_CTX *ctx)
45 void mprFree(void *ptr)
50 void *mprMalloc(uint size)
52 return talloc_size(mpr_ctx, size);
55 /******************************************************************************/
57 void *mprRealloc(void *ptr, uint size)
59 return talloc_realloc_size(mpr_ctx, ptr, size);
62 /******************************************************************************/
64 char *mprStrdup(const char *str)
69 return talloc_strdup(mpr_ctx, str);
72 /*****************************************************************************/
74 int mprAllocSprintf(char **msgbuf, int maxSize, const char *fmt, ...)
81 buf = mprMalloc(maxSize + 1);
82 count = mtVsprintf(buf, maxSize, fmt, args);
88 /*****************************************************************************/
90 int mprAllocVsprintf(char **msgbuf, int maxSize, const char *fmt, va_list args)
95 buf = mprMalloc(maxSize + 1);
96 count = mtVsprintf(buf, maxSize, fmt, args);
102 /*****************************************************************************/
104 * Format a number as a string. FUTURE -- reverse args to be standard.
105 * ie. mprItoa(char *userBuf, int bufsize, int value);
108 char *mprItoa(int value, char *buf, int width)
111 char *cp, *dp, *endp;
114 cp = &numBuf[sizeof(numBuf)];
126 *--cp = '0' + (value % 10);
136 while (dp < endp && *cp) {
143 /*****************************************************************************/
145 void mprLog(int level, const char *fmt, ...)
150 if (DEBUGLVL(level)) {
152 mprAllocVsprintf(&buf, MPR_MAX_STRING, fmt, args);
154 DEBUG(level, ("mprLog: %s", buf));
159 /*****************************************************************************/
161 void mprBreakpoint(const char *file, int line, const char *cond)
164 * Optionally break into the debugger here
166 mprLog(0, "ASSERT at %s:%d, %s\n", file, line, cond);
169 #endif /* !BLD_GOAHEAD_WEBSERVER */
170 /*****************************************************************************/
172 * Create a general growable array structure
175 MprArray *mprCreateArray()
180 array = (MprArray*) mprMalloc(sizeof(MprArray));
184 memset(array, 0, sizeof(MprArray));
186 size = MPR_ARRAY_INCR * sizeof(void*);
187 array->handles = (void**) mprMalloc(size);
188 if (array->handles == 0) {
192 memset(array->handles, 0, size);
193 array->max = MPR_ARRAY_INCR;
198 /*****************************************************************************/
200 * Dispose of the array. Callers responsibility to dispose of handle entries.
203 void mprDestroyArray(MprArray *array)
206 mprAssert(array->max >= 0);
207 mprAssert(array->used >= 0);
209 mprFree(array->handles);
213 /*****************************************************************************/
215 * Add an item to the array
218 int mprAddToArray(MprArray *array, void *item)
220 int memsize, idx, len;
223 mprAssert(array->max >= 0);
224 mprAssert(array->used >= 0);
226 if (array->used < array->max) {
228 mprAssert(idx >= 0 && idx < array->max);
229 mprAssert(array->handles[idx] == 0);
230 array->handles[idx] = item;
234 for (idx = array->used; idx < array->max; idx++) {
235 if (array->handles[idx] == 0) {
237 mprAssert(array->handles[idx] == 0);
238 array->handles[idx] = item;
243 len = array->max + MPR_ARRAY_INCR;
244 memsize = len * sizeof(void*);
245 array->handles = (void**) mprRealloc((void*) array->handles, memsize);
246 if (array->handles == NULL) {
249 memset(&array->handles[array->max], 0, sizeof(void*) * MPR_ARRAY_INCR);
253 mprAssert(idx >= 0 && idx < array->max);
254 mprAssert(array->handles[idx] == 0);
256 array->handles[idx] = item;
260 /*****************************************************************************/
262 * Remove from the array
265 int mprRemoveFromArray(MprArray *array, int idx)
268 mprAssert(array->max > 0);
269 mprAssert(idx >= 0 && idx < array->max);
270 mprAssert(array->handles[idx] != 0);
271 mprAssert(array->used > 0);
273 array->handles[idx] = 0;
274 return --array->used;
277 /*****************************************************************************/
279 * Thread-safe wrapping of strtok. Note "str" is modifed as per strtok()
282 char *mprStrTok(char *str, const char *delim, char **tok)
287 start = str ? str : *tok;
293 i = strspn(start, delim);
295 if (*start == '\0') {
299 end = strpbrk(start, delim);
302 i = strspn(end, delim);
309 /*****************************************************************************/
311 static int mprCoreStrcat(int alloc, char **destp, int destMax, int existingLen,
312 const char *delim, const char *src, va_list args)
317 int sepLen, addBytes, required;
320 mprAssert(destMax > 0);
324 sepLen = (delim) ? strlen(delim) : 0;
334 addBytes += strlen(str) + sepLen;
335 str = va_arg(ap, const char*);
338 if (existingLen > 0) {
341 required = existingLen + addBytes + 1;
342 if (required >= destMax) {
344 return MPR_ERR_WONT_FIT;
349 dest = (char*) mprMalloc(required);
351 dest = (char*) mprRealloc(dest, required);
354 dest = (char*) *destp;
357 dp = &dest[existingLen];
373 str = va_arg(ap, char*);
379 } else if (dest == 0) {
380 dest = (char*) mprMalloc(1);
385 mprAssert(dp < &dest[required]);
389 /*****************************************************************************/
391 int mprReallocStrcat(char **destp, int destMax, int existingLen,
392 const char *delim, const char *src,...)
398 rc = mprCoreStrcat(1, destp, destMax, existingLen, delim, src, ap);
403 /*****************************************************************************/
405 * Return the directory portion of a pathname into the users buffer.
408 int mprGetDirName(char *buf, int bufsize, char *path)
415 mprAssert(bufsize > 0);
417 cp = strrchr(path, '/');
420 cp = strrchr(path, '\\');
429 if (cp == path && cp[1] == '\0') {
435 if (dlen < bufsize) {
439 mprMemcpy(buf, bufsize, path, dlen);
443 return MPR_ERR_WONT_FIT;
446 /*****************************************************************************/
448 int mprStrcpy(char *dest, int destMax, const char *src)
453 mprAssert(destMax > 0);
457 if (len >= destMax && len > 0) {
459 return MPR_ERR_WONT_FIT;
462 memcpy(dest, src, len);
471 /*****************************************************************************/
473 int mprMemcpy(char *dest, int destMax, const char *src, int nbytes)
476 mprAssert(destMax > nbytes);
478 mprAssert(nbytes > 0);
480 if (nbytes > destMax) {
482 return MPR_ERR_WONT_FIT;
485 memcpy(dest, src, nbytes);
492 /*****************************************************************************/
494 void miniMprDummy() {}
495 #endif // !BLD_APPWEB && !BLD_GOAHEAD_WEBSERVER
503 * vim600: sw=4 ts=4 fdm=marker