|
ocssw
1.0
|
00001 /* 00002 * $Id: epr_core.c,v 1.1.1.1 2004-10-28 19:22:22 norman Exp $ 00003 * 00004 * Copyright (C) 2002 by Brockmann Consult (info@brockmann-consult.de) 00005 * 00006 * This program is free software; you can redistribute it and/or modify it 00007 * under the terms of the GNU General Public License as published by the 00008 * Free Software Foundation. This program is distributed in the hope it will 00009 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00010 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00011 * See the GNU General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program; if not, write to the Free Software 00015 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00016 */ 00017 00018 #include <assert.h> 00019 #include <errno.h> 00020 #include <stdio.h> 00021 #include <stdlib.h> 00022 #include <string.h> 00023 00024 #include "epr_api.h" 00025 #include "epr_core.h" 00026 #include "epr_string.h" 00027 #include "epr_ptrarray.h" 00028 #include "epr_swap.h" 00029 #include "epr_field.h" 00030 #include "epr_record.h" 00031 #include "epr_param.h" 00032 #include "epr_dsd.h" 00033 #include "epr_msph.h" 00034 #include "epr_band.h" 00035 #include "epr_bitmask.h" 00036 00037 #include "epr_dddb.h" 00038 00039 00043 EPR_SAPI epr_api; 00044 00045 /* 00046 Function: epr_str_to_data_type_id 00047 Access: private API implementation helper 00048 Changelog: 2002/01/05 nf initial version 00049 */ 00058 EPR_EDataTypeId epr_str_to_data_type_id(const char* str) 00059 { 00060 assert(str != NULL); 00061 if (epr_equal_names(str, "UChar") || epr_equal_names(str, "uchar")) 00062 return e_tid_uchar; 00063 else if (epr_equal_names(str, "AChar") || epr_equal_names(str, "SChar") || epr_equal_names(str, "char")) 00064 return e_tid_char; 00065 else if (epr_equal_names(str, "UShort") || epr_equal_names(str, "ushort")) 00066 return e_tid_ushort; 00067 else if (epr_equal_names(str, "SShort") || epr_equal_names(str, "short")) 00068 return e_tid_short; 00069 else if (epr_equal_names(str, "UInt") || epr_equal_names(str, "uint")) 00070 return e_tid_uint; 00071 else if (epr_equal_names(str, "SInt") || epr_equal_names(str, "int")) 00072 return e_tid_int; 00073 else if (epr_equal_names(str, "ULong") || epr_equal_names(str, "ulong")) 00074 return e_tid_uint; 00075 else if (epr_equal_names(str, "SLong") || epr_equal_names(str, "long")) 00076 return e_tid_int; 00077 else if (epr_equal_names(str, "Float") || epr_equal_names(str, "float")) 00078 return e_tid_float; 00079 else if (epr_equal_names(str, "Double") || epr_equal_names(str, "double")) 00080 return e_tid_double; 00081 else if (epr_equal_names(str, "@/types/UTC.dd") || epr_equal_names(str, "time")) 00082 return e_tid_time; 00083 else if (epr_equal_names(str, "String") || epr_equal_names(str, "string")) 00084 return e_tid_string; 00085 else if (epr_equal_names(str, "Spare") || epr_equal_names(str, "spare")) 00086 return e_tid_spare; 00087 else 00088 return e_tid_unknown; 00089 } 00090 00091 /* 00092 Function: epr_data_type_id_to_str 00093 Access: public API implementation helper 00094 Changelog: 2003/07/10 nf initial version 00095 */ 00104 const char* epr_data_type_id_to_str(EPR_EDataTypeId data_type_id) 00105 { 00106 switch (data_type_id) 00107 { 00108 case e_tid_uchar: 00109 return "uchar"; 00110 case e_tid_char: 00111 return "char"; 00112 case e_tid_ushort: 00113 return "ushort"; 00114 case e_tid_short: 00115 return "short"; 00116 case e_tid_uint: 00117 return "uint"; 00118 case e_tid_int: 00119 return "int"; 00120 case e_tid_float: 00121 return "float"; 00122 case e_tid_double: 00123 return "double"; 00124 case e_tid_string: 00125 return "string"; 00126 case e_tid_spare: 00127 return "spare"; 00128 case e_tid_time: 00129 return "time"; 00130 default: 00131 return ""; 00132 } 00133 } 00134 00135 /* 00136 Function: epr_get_data_type_size 00137 Access: private API implementation helper 00138 Changelog: 2002/01/24 nf initial version 00139 */ 00148 epr_uint epr_get_data_type_size(EPR_EDataTypeId data_type_id) 00149 { 00150 switch (data_type_id) 00151 { 00152 case e_tid_uchar: 00153 return sizeof(epr_uchar); 00154 case e_tid_char: 00155 return sizeof(char); 00156 case e_tid_ushort: 00157 return sizeof(epr_ushort); 00158 case e_tid_short: 00159 return sizeof(short); 00160 case e_tid_uint: 00161 return sizeof(epr_uint); 00162 case e_tid_int: 00163 return sizeof(int); 00164 case e_tid_float: 00165 return sizeof(float); 00166 case e_tid_double: 00167 return sizeof(double); 00168 case e_tid_string: 00169 return sizeof(char); 00170 case e_tid_spare: 00171 return sizeof(epr_uchar); 00172 case e_tid_time: 00173 return sizeof (int) /* days */ 00174 + sizeof (epr_uint) /* seconds */ 00175 + sizeof (epr_uint); /* microseconds */ 00176 default: 00177 return 0; 00178 } 00179 } 00180 /* 00181 * ===================== Logging ============================== 00182 */ 00183 00184 /* 00185 Function: epr_log 00186 Access: private API implementation helper 00187 Changelog: 2002/01/05 mp initial version 00188 */ 00199 void epr_log(EPR_ELogLevel log_level, const char* log_message) 00200 { 00201 if (epr_api.log_handler != NULL && log_level >= epr_api.log_level) 00202 epr_api.log_handler(log_level, log_message); 00203 } 00204 00205 /* 00206 * ===================== Error Handling ============================== 00207 */ 00208 00209 /* 00210 Function: epr_set_error 00211 Access: private API implementation helper 00212 Changelog: 2002/01/05 mp initial version 00213 */ 00221 void epr_set_err(EPR_EErrCode err_code, const char* err_message) 00222 { 00223 epr_api.last_err_code = err_code; 00224 epr_assign_string(&epr_api.last_err_message, err_message); 00225 00226 if (epr_api.log_handler != NULL) 00227 { 00228 epr_api.log_handler(e_log_error, err_message); 00229 } 00230 00231 if (epr_api.err_handler != NULL) 00232 { 00233 epr_api.err_handler(err_code, err_message); 00234 } 00235 } 00236 00237 /* 00238 Function: epr_set_error 00239 Access: private API implementation helper 00240 Changelog: 2002/01/05 mp initial version 00241 */ 00247 void epr_clear_err() 00248 { 00249 epr_api.last_err_code = e_err_none; 00250 epr_free_string(epr_api.last_err_message); 00251 epr_api.last_err_message = NULL; 00252 } 00253 00254 /* 00255 Function: epr_get_last_err_code 00256 Access: private API implementation helper 00257 Changelog: 2002/01/05 nf initial version 00258 */ 00265 EPR_EErrCode epr_get_last_err_code() 00266 { 00267 return epr_api.last_err_code; 00268 } 00269 00270 /* 00271 Function: epr_get_last_err_message 00272 Access: private API implementation helper 00273 Changelog: 2002/01/05 nf initial version 00274 */ 00281 const char* epr_get_last_err_message() 00282 { 00283 return epr_api.last_err_message; 00284 } 00285 00286 00295 FILE* epr_open_file(char* file_path) 00296 { 00297 FILE* db_file; 00298 epr_log(e_log_debug, "about to open file: "); 00299 epr_log(e_log_debug, file_path); 00300 00301 db_file = fopen(epr_trim_string(file_path), "rb"); 00302 if (db_file == NULL) 00303 { 00304 epr_log(e_log_debug, "open failed"); 00305 if (errno == ENOENT) 00306 { 00307 epr_set_err(e_err_file_not_found, 00308 "epr_open_file: file not found"); 00309 } 00310 else { 00311 epr_set_err(e_err_file_access_denied, 00312 "epr_open_file: file open failed"); 00313 } 00314 } 00315 /*epr_log(e_log_debug, "open successful");*/ 00316 return db_file; 00317 } 00318 00319 00328 int epr_str_to_number(const char* str) 00329 { 00330 char *stopstring; 00331 int l; 00332 assert(str != NULL); 00333 00334 if (strcmp(str, "*") == 0) return 1; 00335 if (strcmp(str, "") == 0) return 1; 00336 00337 errno = 0; 00338 l = strtol( str, &stopstring, 10 ); 00339 00340 if (errno == EDOM) 00341 { 00342 epr_set_err(e_err_illegal_conversion, 00343 "failed to convert string to integer: errno = EDOM"); 00344 return -1; 00345 } 00346 if (errno == ERANGE) 00347 { 00348 epr_set_err(e_err_illegal_conversion, 00349 "failed to convert string to integer: errno = ERANGE"); 00350 return -1; 00351 } 00352 00353 return l; 00354 } 00355 00356 00373 epr_uint epr_parse_value_count(EPR_SProductId* product_id, const char* count) 00374 { 00375 char seps[] = EPR_COUNT_SEPARATOR_ARRAY; 00376 char * token; 00377 char * str; 00378 epr_uint c; 00379 int pos = 0; 00380 epr_uint comes_from_convert; 00381 00382 c = 1; 00383 00384 str = epr_clone_string(count); 00385 if (str == NULL) 00386 { 00387 epr_set_err(e_err_out_of_memory, 00388 "epr_parse_value_count: cannot allocate memory"); 00389 return 16111;/*8999*/ /* @todo check: why in the hell 16111 ? */ 00390 } 00391 00392 while ((token = epr_str_tok(str, seps, &pos)) != NULL) 00393 { 00394 comes_from_convert = epr_str_to_number(token); 00395 if (comes_from_convert == 0 && (strcmp(token, "0") != 0)) 00396 { 00397 /* check if "token" belongs to param_table*/ 00398 comes_from_convert = epr_param_to_value(token, product_id->param_table); 00399 if (comes_from_convert == -1) 00400 { 00401 epr_set_err(e_err_illegal_conversion, 00402 "epr_parse_value_count: parameter not found"); 00403 return 16111; /* @todo check: why in the hell 16111 ? */ 00404 } 00405 } 00406 c *= comes_from_convert; 00407 epr_free_string(token); 00408 token = NULL; 00409 } 00410 epr_free_string(str); 00411 return c; 00412 } 00413 00414 00424 epr_uint epr_param_to_value(const char* str, EPR_SPtrArray* param_table) 00425 { 00426 EPR_SParamElem* param_elem = NULL; 00427 int elem_index; 00428 int param_table_length; 00429 00430 param_table_length = (int)epr_get_ptr_array_length(param_table); 00431 for (elem_index = 0; elem_index < param_table_length; elem_index++) 00432 { 00433 param_elem = (EPR_SParamElem*)epr_get_ptr_array_elem_at(param_table, elem_index); 00434 if (epr_equal_names(param_elem->param_name, str)) 00435 { 00436 return param_elem->param_value; 00437 } 00438 } 00439 return (epr_uint) -1; /*error*/ 00440 } 00441 00442 00449 void epr_make_os_compatible_path(char* path) 00450 { 00451 if (path != NULL) 00452 { 00453 char* pc = path; 00454 while (*pc != '\0') 00455 { 00456 #ifdef WIN32 00457 if (*pc == '/') 00458 *pc = '\\'; 00459 00460 #elif _M_MPPC 00461 if (*pc == '/') 00462 *pc = ':'; /* UK note: the colon is an old-style path separator of the Mac OS */ 00463 /* possibly not used any more but supported for Classic compatibility */ 00464 /* @to do: check whether the forward slash / should be used in Mac OS X */ 00465 #else 00466 if (*pc == '\\') 00467 *pc = '/'; 00468 #endif 00469 pc++; 00470 } 00471 } 00472 } 00473 00474 00475 epr_boolean epr_check_api_init_flag() { 00476 if (!epr_api.init_flag) { 00477 epr_set_err(e_err_api_not_initialized, 00478 "epr_open_product: API not initialized (forgot to call epr_init_api?)"); 00479 return EPR_FALSE; 00480 } 00481 return EPR_TRUE; 00482 }
1.7.6.1