comparison src/c/urweb.c @ 1061:e8a35d710ab9

Context globals; ctype functions
author Adam Chlipala <adamc@hcoop.net>
date Tue, 08 Dec 2009 10:46:50 -0500
parents 03a81e26e5fe
children e3f6620afd51
comparison
equal deleted inserted replaced
1060:6f4f8b9c5023 1061:e8a35d710ab9
339 typedef struct { 339 typedef struct {
340 void *data; 340 void *data;
341 uw_callback commit, rollback, free; 341 uw_callback commit, rollback, free;
342 } transactional; 342 } transactional;
343 343
344 typedef struct {
345 char *name;
346 void *data;
347 void (*free)(void*);
348 } global;
349
344 struct uw_context { 350 struct uw_context {
345 char *(*get_header)(void *, const char *); 351 char *(*get_header)(void *, const char *);
346 void *get_header_data; 352 void *get_header_data;
347 353
348 buf outHeaders, page, heap, script; 354 buf outHeaders, page, heap, script;
371 377
372 client *client; 378 client *client;
373 379
374 transactional *transactionals; 380 transactional *transactionals;
375 size_t n_transactionals, used_transactionals; 381 size_t n_transactionals, used_transactionals;
382
383 global *globals;
384 size_t n_globals;
376 385
377 char error_message[ERROR_BUF_LEN]; 386 char error_message[ERROR_BUF_LEN];
378 }; 387 };
379 388
380 extern int uw_inputs_len, uw_timeout; 389 extern int uw_inputs_len, uw_timeout;
421 430
422 ctx->error_message[0] = 0; 431 ctx->error_message[0] = 0;
423 432
424 ctx->transactionals = malloc(0); 433 ctx->transactionals = malloc(0);
425 ctx->n_transactionals = ctx->used_transactionals = 0; 434 ctx->n_transactionals = ctx->used_transactionals = 0;
435
436 ctx->globals = malloc(0);
437 ctx->n_globals = 0;
426 438
427 return ctx; 439 return ctx;
428 } 440 }
429 441
430 void uw_set_db(uw_context ctx, void *db) { 442 void uw_set_db(uw_context ctx, void *db) {
447 free(ctx->cleanup); 459 free(ctx->cleanup);
448 free(ctx->transactionals); 460 free(ctx->transactionals);
449 461
450 for (i = 0; i < ctx->n_deltas; ++i) 462 for (i = 0; i < ctx->n_deltas; ++i)
451 buf_free(&ctx->deltas[i].msgs); 463 buf_free(&ctx->deltas[i].msgs);
464
465 for (i = 0; i < ctx->n_globals; ++i)
466 ctx->globals[i].free(ctx->globals[i].data);
452 467
453 free(ctx); 468 free(ctx);
454 } 469 }
455 470
456 void uw_reset_keep_error_message(uw_context ctx) { 471 void uw_reset_keep_error_message(uw_context ctx) {
3090 const uw_Basis_time minTime = 0; 3105 const uw_Basis_time minTime = 0;
3091 3106
3092 uw_Basis_time uw_Basis_now(uw_context ctx) { 3107 uw_Basis_time uw_Basis_now(uw_context ctx) {
3093 return time(NULL); 3108 return time(NULL);
3094 } 3109 }
3110
3111 void *uw_get_global(uw_context ctx, char *name) {
3112 int i;
3113
3114 for (i = 0; i < ctx->n_globals; ++i)
3115 if (!strcmp(name, ctx->globals[i].name))
3116 return ctx->globals[i].data;
3117
3118 return NULL;
3119 }
3120
3121 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
3122 int i;
3123
3124 if (data == NULL) uw_error(ctx, FATAL, "NULL data value for global '%s'", name);
3125
3126 for (i = 0; i < ctx->n_globals; ++i)
3127 if (!strcmp(name, ctx->globals[i].name)) {
3128 if (ctx->globals[i].data)
3129 ctx->globals[i].free(ctx->globals[i].data);
3130 ctx->globals[i].data = data;
3131 ctx->globals[i].free = free;
3132 return;
3133 }
3134
3135 ++ctx->n_globals;
3136 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
3137 ctx->globals[ctx->n_globals-1].name = name;
3138 ctx->globals[ctx->n_globals-1].data = data;
3139 ctx->globals[ctx->n_globals-1].free = free;
3140 }
3141
3142 uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) {
3143 return isalnum(c);
3144 }
3145
3146 uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) {
3147 return isalpha(c);
3148 }
3149
3150 uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) {
3151 return isblank(c);
3152 }
3153
3154 uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) {
3155 return iscntrl(c);
3156 }
3157
3158 uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) {
3159 return isdigit(c);
3160 }
3161
3162 uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) {
3163 return isgraph(c);
3164 }
3165
3166 uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) {
3167 return islower(c);
3168 }
3169
3170 uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) {
3171 return isprint(c);
3172 }
3173
3174 uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) {
3175 return ispunct(c);
3176 }
3177
3178 uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) {
3179 return isspace(c);
3180 }
3181 uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) {
3182 return isupper(c);
3183 }
3184
3185 uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) {
3186 return isxdigit(c);
3187 }
3188
3189 uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) {
3190 return tolower(c);
3191 }
3192
3193 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
3194 return toupper(c);
3195 }