NCBI C Toolkit Cross Reference

C/connect/ncbi_core_c.c


  1 /* $Id: ncbi_core_c.c,v 6.21 2008/10/17 15:59:05 lavr Exp $
  2  * ===========================================================================
  3  *
  4  *                            PUBLIC DOMAIN NOTICE
  5  *               National Center for Biotechnology Information
  6  *
  7  *  This software/database is a "United States Government Work" under the
  8  *  terms of the United States Copyright Act.  It was written as part of
  9  *  the author's official duties as a United States Government employee and
 10  *  thus cannot be copyrighted.  This software/database is freely available
 11  *  to the public for use. The National Library of Medicine and the U.S.
 12  *  Government have not placed any restriction on its use or reproduction.
 13  *
 14  *  Although all reasonable efforts have been taken to ensure the accuracy
 15  *  and reliability of the software and data, the NLM and the U.S.
 16  *  Government do not and cannot warrant the performance or results that
 17  *  may be obtained by using this software or data. The NLM and the U.S.
 18  *  Government disclaim all warranties, express or implied, including
 19  *  warranties of performance, merchantability or fitness for any particular
 20  *  purpose.
 21  *
 22  *  Please cite the author in any work or product based on this material.
 23  *
 24  * ===========================================================================
 25  *
 26  * Author:  Anton Lavrentiev
 27  *
 28  * File description:
 29  *   C->C conversion functions for basic CORE connect stuff:
 30  *     Registry
 31  *     Logging
 32  *     Locking
 33  *
 34  */
 35 
 36 #include "ncbi_ansi_ext.h"
 37 #include "ncbi_priv.h"
 38 #include <connect/ncbi_core_c.h>
 39 #include <connect/ncbi_util.h>
 40 #include <ncbienv.h>
 41 #include <ncbierr.h>
 42 #include <stdlib.h>
 43 #include <string.h>
 44 #include <time.h>
 45 
 46 
 47 /***********************************************************************
 48  *                              Registry                               *
 49  ***********************************************************************/
 50 
 51 #ifdef __cplusplus
 52 extern "C" {
 53 #endif
 54     static void s_REG_Get    (void*, const char*, const char*, char*, size_t);
 55     static int  s_REG_Set    (void*, const char*, const char*,
 56                               const char*, EREG_Storage);
 57     static void s_REG_Cleanup(void*);
 58 #ifdef __cplusplus
 59 }
 60 #endif
 61 
 62 static void s_REG_Get(void* user_data,
 63                       const char* section, const char* name,
 64                       char* value, size_t val_size)
 65 {
 66     const char* conf_file = (const char*) user_data;
 67     char* dflt = *value ? strdup(value) : 0;
 68     Nlm_GetAppParam(conf_file, section, name, dflt, value, val_size);
 69     if (dflt)
 70         free(dflt);
 71 }
 72 
 73 
 74 static int s_REG_Set(void* user_data,
 75                      const char* section, const char* name,
 76                      const char* value, EREG_Storage storage)
 77 {
 78     const char* conf_file = (const char*) user_data;
 79     Nlm_Boolean result = storage == eREG_Persistent
 80         ? Nlm_SetAppParam(conf_file, section, name, value)
 81         : Nlm_TransientSetAppParam(conf_file, section, name, value);
 82     return result;
 83 }
 84 
 85 
 86 static void s_REG_Cleanup(void* user_data)
 87 {
 88     if (user_data)
 89         free(user_data);
 90 }
 91 
 92 
 93 extern REG REG_c2c(const char* conf_file)
 94 {
 95     char* s = 0;
 96 
 97     if (!conf_file)
 98         conf_file = "ncbi";
 99     if (!(s = strdup(conf_file)))
100         return 0/*failure*/;
101 
102     return REG_Create(s/*user_data*/, s_REG_Get, s_REG_Set, s_REG_Cleanup, 0);
103 }
104 
105 
106 /***********************************************************************
107  *                                Logger                               *
108  ***********************************************************************/
109 
110 #ifdef __cplusplus
111 extern "C" {
112 #endif
113     static void s_LOG_Handler(void*, SLOG_Handler*);
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 static void s_LOG_Handler(void* user_data/*unused*/, SLOG_Handler* call_data)
119 {
120     enum _ErrSev sev;
121     char* msg;
122 
123     switch (call_data->level) {
124     case eLOG_Trace:
125         sev = SEV_INFO; /* FIXME: Should be somewhat different */
126         break;
127     case eLOG_Note:
128         sev = SEV_INFO;
129         break;
130     case eLOG_Warning:
131         sev = SEV_WARNING;
132         break;
133     case eLOG_Error:
134         sev = SEV_ERROR;
135         break;
136     case eLOG_Critical:
137         sev = SEV_REJECT;
138         break;
139     case eLOG_Fatal:
140         /*FALLTHRU*/
141     default:
142         sev = SEV_FATAL;
143         break;
144     }
145 
146     if (Nlm_ErrSetContext(call_data->module, call_data->file, call_data->line,
147                           DBFLAG/*defined in ncbierr.h*/, 0, 0, 0) != 0)
148         return/*busy*/;
149 
150     if (!(msg = LOG_ComposeMessage(call_data, fLOG_None)))
151         return/*failed*/;
152 
153     Nlm_ErrPostStr(sev, call_data->err_code, call_data->err_subcode, msg);
154 
155     if (call_data->level == eLOG_Trace)
156         Nlm_ErrClear();
157 
158     free(msg);
159 }
160 
161 
162 extern LOG LOG_c2c(void)
163 {
164     return LOG_Create(0, s_LOG_Handler, 0, 0);
165 }
166 
167 
168 /***********************************************************************
169  *                               MT-Lock                               *
170  ***********************************************************************/
171 
172 #ifdef __cplusplus
173 extern "C" {
174 #endif
175     static int/*bool*/ s_LOCK_Handler(void*, EMT_Lock);
176     static void        s_LOCK_Cleanup(void*);
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 static int/*bool*/ s_LOCK_Handler(void* user_data, EMT_Lock how)
182 {
183     TNlmRWlock lock = (TNlmRWlock) user_data;
184     int/*bool*/ result;
185     if ( !lock )
186         return -1/*rightful not doing*/;
187     switch ( how ) {
188     case eMT_Lock:
189         result = NlmRWwrlock(lock) == 0;
190         break;
191     case eMT_LockRead:
192         result = NlmRWrdlock(lock) == 0;
193         break;
194     case eMT_Unlock:
195         result = NlmRWunlock(lock) == 0;
196         break;
197     case eMT_TryLock:
198         result = NlmRWtrywrlock(lock) == 0;
199         break;
200     case eMT_TryLockRead:
201         result = NlmRWtryrdlock(lock) == 0;
202         break;
203     default:
204         assert(0);
205         result = 0/*false*/;
206         break;
207     }
208     return result;
209 }
210 
211 
212 static void s_LOCK_Cleanup(void* user_data)
213 {
214     NlmRWdestroy((TNlmRWlock) user_data);
215 }
216 
217 
218 extern MT_LOCK MT_LOCK_c2c(TNlmRWlock lock, int/*bool*/ pass_ownership)
219 {
220     return MT_LOCK_Create(lock ? lock : NlmRWinit(), s_LOCK_Handler,
221                           !lock  ||  pass_ownership ? s_LOCK_Cleanup : 0);
222 }
223 
224 
225 /***********************************************************************
226  *                                 Init                                *
227  ***********************************************************************/
228 
229 extern void CONNECT_Init(const char* conf_file)
230 {
231     g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND;
232     srand(g_NCBI_ConnectRandomSeed);
233 
234     CORE_SetLOCK(MT_LOCK_c2c(0, 1/*true*/));
235     CORE_SetLOG(LOG_c2c());
236     CORE_SetREG(REG_c2c(conf_file));
237 }
238 
239 
240 /*
241  * ---------------------------------------------------------------------------
242  * $Log: ncbi_core_c.c,v $
243  * Revision 6.21  2008/10/17 15:59:05  lavr
244  * REG_Set() made to return a value
245  *
246  * Revision 6.20  2008/10/16 19:12:55  lavr
247  * Id left justified
248  *
249  * Revision 6.19  2007/10/18 15:29:26  ivanovp
250  * Connect and ctools libraries are changed to use new error and log posting system with error codes and subcodes. JIRA: CXX-3
251  *
252  * Revision 6.18  2007/06/25 15:33:04  lavr
253  * +eMT_TryLock[Read]
254  *
255  * Revision 6.17  2006/03/07 17:24:54  lavr
256  * Remove g_NCBI_ConnectSrandAddent for good
257  *
258  * Revision 6.16  2005/12/07 22:18:20  lavr
259  * Use strdup() instead of malloc()+strcpy()
260  *
261  * Revision 6.15  2005/07/26 20:00:33  lavr
262  * Retroactively provide s_NCBI_ConnectSrandAddent() temporarily
263  *
264  * Revision 6.14  2005/07/11 18:51:34  lavr
265  * Spell ADDEND
266  *
267  * Revision 6.13  2005/05/03 11:59:00  ivanov
268  * Removing #include <connect/ncbi_priv.h>
269  *
270  * Revision 6.12  2005/05/03 11:51:13  ivanov
271  * Include ncbi_priv.h instead of ncbi_socket.h
272  *
273  * Revision 6.11  2005/05/02 16:28:02  lavr
274  * CONNECT_Init() to set global random seed
275  *
276  * Revision 6.10  2005/04/20 20:38:42  lavr
277  * +"ncbi_assert.h"
278  *
279  * Revision 6.9  2002/10/31 17:53:33  lavr
280  * Clear error in case of a trace message
281  *
282  * Revision 6.8  2002/09/24 18:07:12  lavr
283  * Declare s_LOCK_Handler static in the first place of declaration
284  *
285  * Revision 6.7  2002/07/05 17:52:46  lavr
286  * LOCK handler to return -1 in case of an empty lock object
287  *
288  * Revision 6.6  2002/07/03 19:53:40  lavr
289  * Do not consult environment in registry searches (use only GetAppParam())
290  *
291  * Revision 6.5  2002/07/02 21:20:53  lavr
292  * Use INFO severity for tracing; patch for registry lookup in environment
293  *
294  * Revision 6.4  2002/06/12 16:41:37  lavr
295  * +CONNECT_Init()
296  *
297  * Revision 6.3  2002/05/08 19:04:37  kans
298  * needed to include connect/ncbi_ansi_ext.h
299  *
300  * Revision 6.2  2002/05/07 18:44:45  lavr
301  * Refined API; Change log moved to the end
302  *
303  * Revision 6.1  2002/05/07 18:17:54  lavr
304  * Initial revision
305  *
306  * ===========================================================================
307  */
308 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.