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;
46 void mprSetCtx(TALLOC_CTX *ctx)
49 mpr_ctx = talloc_new(ctx);
52 void mprFree(void *ptr)
57 void *mprMalloc(uint size)
59 return talloc_size(mpr_ctx, size);
62 /******************************************************************************/
64 void *mprRealloc(void *ptr, uint size)
66 return talloc_realloc_size(mpr_ctx, ptr, size);
69 /******************************************************************************/
71 char *mprStrdup(const char *str)
76 return talloc_strdup(mpr_ctx, str);
79 /*****************************************************************************/
81 int mprAllocSprintf(char **msgbuf, int maxSize, const char *fmt, ...)
88 buf = mprMalloc(maxSize + 1);
89 count = mtVsprintf(buf, maxSize, fmt, args);
95 /*****************************************************************************/
97 int mprAllocVsprintf(char **msgbuf, int maxSize, const char *fmt, va_list args)
102 buf = mprMalloc(maxSize + 1);
103 count = mtVsprintf(buf, maxSize, fmt, args);
109 /*****************************************************************************/
111 * Format a number as a string. FUTURE -- reverse args to be standard.
112 * ie. mprItoa(char *userBuf, int bufsize, int value);
115 char *mprItoa(int value, char *buf, int width)
118 char *cp, *dp, *endp;
121 cp = &numBuf[sizeof(numBuf)];
133 *--cp = '0' + (value % 10);
143 while (dp < endp && *cp) {
150 /*****************************************************************************/
152 void mprLog(int level, const char *fmt, ...)
157 if (DEBUGLVL(level)) {
159 mprAllocVsprintf(&buf, MPR_MAX_STRING, fmt, args);
161 DEBUG(level, ("mprLog: %s", buf));
166 /*****************************************************************************/
168 void mprBreakpoint(const char *file, int line, const char *cond)
171 * Optionally break into the debugger here
173 mprLog(0, "ASSERT at %s:%d, %s\n", file, line, cond);
176 #endif /* !BLD_GOAHEAD_WEBSERVER */
177 /*****************************************************************************/
179 * Create a general growable array structure
182 MprArray *mprCreateArray()
187 array = (MprArray*) mprMalloc(sizeof(MprArray));
191 memset(array, 0, sizeof(MprArray));
193 size = MPR_ARRAY_INCR * sizeof(void*);
194 array->handles = (void**) mprMalloc(size);
195 if (array->handles == 0) {
199 memset(array->handles, 0, size);
200 array->max = MPR_ARRAY_INCR;
205 /*****************************************************************************/
207 * Dispose of the array. Callers responsibility to dispose of handle entries.
210 void mprDestroyArray(MprArray *array)
213 mprAssert(array->max >= 0);
214 mprAssert(array->used >= 0);
216 mprFree(array->handles);
220 /*****************************************************************************/
222 * Add an item to the array
225 int mprAddToArray(MprArray *array, void *item)
227 int memsize, idx, len;
230 mprAssert(array->max >= 0);
231 mprAssert(array->used >= 0);
233 if (array->used < array->max) {
235 mprAssert(idx >= 0 && idx < array->max);
236 mprAssert(array->handles[idx] == 0);
237 array->handles[idx] = item;
241 for (idx = array->used; idx < array->max; idx++) {
242 if (array->handles[idx] == 0) {
244 mprAssert(array->handles[idx] == 0);
245 array->handles[idx] = item;
250 len = array->max + MPR_ARRAY_INCR;
251 memsize = len * sizeof(void*);
252 array->handles = (void**) mprRealloc((void*) array->handles, memsize);
253 if (array->handles == NULL) {
256 memset(&array->handles[array->max], 0, sizeof(void*) * MPR_ARRAY_INCR);
260 mprAssert(idx >= 0 && idx < array->max);
261 mprAssert(array->handles[idx] == 0);
263 array->handles[idx] = item;
267 /*****************************************************************************/
269 * Remove from the array
272 int mprRemoveFromArray(MprArray *array, int idx)
275 mprAssert(array->max > 0);
276 mprAssert(idx >= 0 && idx < array->max);
277 mprAssert(array->handles[idx] != 0);
278 mprAssert(array->used > 0);
280 array->handles[idx] = 0;
281 return --array->used;
284 /*****************************************************************************/
286 * Thread-safe wrapping of strtok. Note "str" is modifed as per strtok()
289 char *mprStrTok(char *str, const char *delim, char **tok)
294 start = str ? str : *tok;
300 i = strspn(start, delim);
302 if (*start == '\0') {
306 end = strpbrk(start, delim);
309 i = strspn(end, delim);
316 /*****************************************************************************/
318 static int mprCoreStrcat(int alloc, char **destp, int destMax, int existingLen,
319 const char *delim, const char *src, va_list args)
324 int sepLen, addBytes, required;
327 mprAssert(destMax > 0);
331 sepLen = (delim) ? strlen(delim) : 0;
341 addBytes += strlen(str) + sepLen;
342 str = va_arg(ap, const char*);
345 if (existingLen > 0) {
348 required = existingLen + addBytes + 1;
349 if (required >= destMax) {
351 return MPR_ERR_WONT_FIT;
356 dest = (char*) mprMalloc(required);
358 dest = (char*) mprRealloc(dest, required);
361 dest = (char*) *destp;
364 dp = &dest[existingLen];
380 str = va_arg(ap, char*);
386 } else if (dest == 0) {
387 dest = (char*) mprMalloc(1);
392 mprAssert(dp < &dest[required]);
396 /*****************************************************************************/
398 int mprReallocStrcat(char **destp, int destMax, int existingLen,
399 const char *delim, const char *src,...)
405 rc = mprCoreStrcat(1, destp, destMax, existingLen, delim, src, ap);
410 /*****************************************************************************/
412 * Return the directory portion of a pathname into the users buffer.
415 int mprGetDirName(char *buf, int bufsize, char *path)
422 mprAssert(bufsize > 0);
424 cp = strrchr(path, '/');
427 cp = strrchr(path, '\\');
436 if (cp == path && cp[1] == '\0') {
442 if (dlen < bufsize) {
446 mprMemcpy(buf, bufsize, path, dlen);
450 return MPR_ERR_WONT_FIT;
453 /*****************************************************************************/
455 int mprStrcpy(char *dest, int destMax, const char *src)
460 mprAssert(destMax > 0);
464 if (len >= destMax && len > 0) {
466 return MPR_ERR_WONT_FIT;
469 memcpy(dest, src, len);
478 /*****************************************************************************/
480 int mprMemcpy(char *dest, int destMax, const char *src, int nbytes)
483 mprAssert(destMax > nbytes);
485 mprAssert(nbytes > 0);
487 if (nbytes > destMax) {
489 return MPR_ERR_WONT_FIT;
492 memcpy(dest, src, nbytes);
499 /*****************************************************************************/
501 void miniMprDummy() {}
502 #endif // !BLD_APPWEB && !BLD_GOAHEAD_WEBSERVER
510 * vim600: sw=4 ts=4 fdm=marker