[go: up one dir, main page]

Menu

[e669e2]: / src / flom_rsrc.c  Maximize  Restore  History

Download this file

217 lines (180 with data), 6.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (c) 2013-2014, Christian Ferrari <tiian@users.sourceforge.net>
* All rights reserved.
*
* This file is part of FLOM.
*
* FLOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* FLOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FLOM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#ifdef HAVE_GLIB_H
# include <glib.h>
#endif
#include "flom_config.h"
#include "flom_errors.h"
#include "flom_rsrc.h"
#include "flom_resource_simple.h"
#include "flom_trace.h"
/* set module trace flag */
#ifdef FLOM_TRACE_MODULE
# undef FLOM_TRACE_MODULE
#endif /* FLOM_TRACE_MODULE */
#define FLOM_TRACE_MODULE FLOM_TRACE_MOD_RSRC
/* global static objects */
regex_t global_res_name_preg[FLOM_RSRC_TYPE_N];
int global_res_name_preg_init()
{
enum Exception { SNPRINTF_ERROR
, REGCOMP_ERROR
, NONE } excp;
int ret_cod = FLOM_RC_INTERNAL_ERROR;
FLOM_TRACE(("global_res_name_preg_init\n"));
TRY {
int reg_error;
char reg_errbuf[200];
char reg_expr[1000];
flom_rsrc_type_t i;
const char *reg_str[FLOM_RSRC_TYPE_N] = {
"^_$" /* this is a dummy value */ ,
"^%s$|^[[:alpha:]]([[:alpha:][:digit:]])+$" };
memset(global_res_name_preg, 0, sizeof(global_res_name_preg));
for (i=FLOM_RSRC_TYPE_NULL; i<FLOM_RSRC_TYPE_N; ++i) {
/* preparing regular expression */
if (sizeof(reg_expr) <= snprintf(
reg_expr, sizeof(reg_expr), reg_str[i],
DEFAULT_RESOURCE_NAME))
THROW(SNPRINTF_ERROR);
FLOM_TRACE(("global_res_name_preg_init: regular expression for "
"type %d is '%s'\n", i, reg_expr));
reg_error = regcomp(global_res_name_preg+i, reg_expr,
REG_EXTENDED|REG_NOSUB|REG_NEWLINE);
if (0 != reg_error) {
regerror(reg_error, global_res_name_preg+i,
reg_errbuf, sizeof(reg_errbuf));
FLOM_TRACE(("global_res_name_preg_init: regcomp returned %d "
"('%s') instead of 0\n", reg_error, reg_errbuf));
THROW(REGCOMP_ERROR);
}
}
THROW(NONE);
} CATCH {
switch (excp) {
case SNPRINTF_ERROR:
ret_cod = FLOM_RC_SNPRINTF_ERROR;
break;
case REGCOMP_ERROR:
ret_cod = FLOM_RC_REGCOMP_ERROR;
break;
case NONE:
ret_cod = FLOM_RC_OK;
break;
default:
ret_cod = FLOM_RC_INTERNAL_ERROR;
} /* switch (excp) */
} /* TRY-CATCH */
FLOM_TRACE(("global_res_name_preg_init/excp=%d/"
"ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
return ret_cod;
}
void global_res_name_preg_free()
{
flom_rsrc_type_t i;
FLOM_TRACE(("global_res_name_preg_free\n"));
for (i=FLOM_RSRC_TYPE_NULL; i<FLOM_RSRC_TYPE_N; ++i)
regfree(global_res_name_preg+i);
memset(global_res_name_preg, 0, sizeof(global_res_name_preg));
}
flom_rsrc_type_t flom_rsrc_get_type(const gchar *resource_name)
{
int reg_error;
char reg_errbuf[200];
flom_rsrc_type_t i;
flom_rsrc_type_t ret_cod = FLOM_RSRC_TYPE_NULL;
FLOM_TRACE(("flom_rsrc_get_type\n"));
for (i=FLOM_RSRC_TYPE_NULL+1; i<FLOM_RSRC_TYPE_N; ++i) {
reg_error = regexec(global_res_name_preg+i,
resource_name, 0, NULL, 0);
regerror(reg_error, global_res_name_preg+i, reg_errbuf,
sizeof(reg_errbuf));
FLOM_TRACE(("flom_rsrc_get_type: regexec returned "
"%d ('%s') for string '%s'\n",
reg_error, reg_errbuf, resource_name));
if (0 == reg_error) {
ret_cod = i;
break;
} /* if (0 == reg_error) */
} /* for (i=FLOM_RSRC_RES_TYPE_NULL+1; ... */
FLOM_TRACE(("flom_rsrc_get_type/ret_cod=%d\n", ret_cod));
return ret_cod;
}
int flom_resource_init(flom_resource_t *resource,
flom_rsrc_type_t type, const gchar *name)
{
enum Exception { OUT_OF_RANGE
, G_QUEUE_NEW_ERROR
, UNKNOW_RESOURCE
, NONE } excp;
int ret_cod = FLOM_RC_INTERNAL_ERROR;
FLOM_TRACE(("flom_resource_init\n"));
TRY {
if (FLOM_RSRC_TYPE_NULL >= type || FLOM_RSRC_TYPE_N <= type)
THROW(OUT_OF_RANGE);
resource->type = type;
resource->name = g_strdup(name);
FLOM_TRACE(("flom_resource_init: initialized resource ('%s',%d)\n",
resource->name, resource->type));
switch (resource->type) {
case FLOM_RSRC_TYPE_SIMPLE:
resource->data.simple.holders = NULL;
if (NULL == (resource->data.simple.waitings = g_queue_new()))
THROW(G_QUEUE_NEW_ERROR);
resource->inmsg = flom_resource_simple_inmsg;
resource->clean = flom_resource_simple_clean;
resource->free = flom_resource_simple_free;
break;
default:
THROW(UNKNOW_RESOURCE);
}
THROW(NONE);
} CATCH {
switch (excp) {
case OUT_OF_RANGE:
ret_cod = FLOM_RC_OUT_OF_RANGE;
break;
case G_QUEUE_NEW_ERROR:
ret_cod = FLOM_RC_G_QUEUE_NEW_ERROR;
break;
case UNKNOW_RESOURCE:
ret_cod = FLOM_RC_INTERNAL_ERROR;
break;
case NONE:
ret_cod = FLOM_RC_OK;
break;
default:
ret_cod = FLOM_RC_INTERNAL_ERROR;
} /* switch (excp) */
} /* TRY-CATCH */
FLOM_TRACE(("flom_resource_init/excp=%d/"
"ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
return ret_cod;
}
void flom_resource_free(flom_resource_t *resource)
{
FLOM_TRACE(("flom_resource_free\n"));
/* calling the destructor */
resource->free(resource);
/* releasing resource name */
if (NULL != resource->name)
g_free(resource->name);
resource->name = NULL;
}