NCBI C++ ToolKit
ncbi_priv.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ncbi_priv.c 101765 2024-02-08 01:53:10Z lavr $
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: Denis Vakatov
27  *
28  * File Description:
29  * Private aux. code for the "ncbi_*.[ch]"
30  *
31  */
32 
33 #include "ncbi_priv.h"
34 #if defined(NCBI_OS_UNIX)
35 # include <unistd.h>
36 #elif defined(NCBI_OS_MSWIN)
37 # include <windows.h>
38 #else
39 # include <connect/ncbi_socket.h>
40 #endif /*NCBI_OS*/
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 
46 /* GLOBALS */
51 unsigned int g_NCBI_ConnectRandomSeed = 0;
56 
57 #ifdef NCBI_MONKEY
58 FMonkeySend g_MONKEY_Send = 0;
59 FMonkeyRecv g_MONKEY_Recv = 0;
60 FMonkeyConnect g_MONKEY_Connect = 0;
61 FMonkeyPoll g_MONKEY_Poll = 0;
62 FMonkeyClose g_MONKEY_Close = 0;
63 FSockHasSocket g_MONKEY_SockHasSocket = 0;
64 #endif /*NCBI_MONKEY*/
65 
66 
67 extern unsigned int g_NCBI_ConnectSrandAddend(void)
68 {
69 #if defined(NCBI_OS_UNIX)
70  return (unsigned int) getpid();
71 #elif defined(NCBI_OS_MSWIN)
72  return (unsigned int) GetCurrentProcessId();
73 #else
75 #endif /*NCBI_OS*/
76 }
77 
78 
79 #ifdef _DEBUG
80 
81 /* NB: Can only track the most recent lock / unlock; cannot handle nested! */
82 
83 static MT_LOCK s_CoreLock = 0;
84 
85 extern int g_NCBI_CoreCheckLock(void)
86 {
87  /* save last lock accessed */
89  return 1/*success*/;
90 }
91 
92 extern int g_NCBI_CoreCheckUnlock(void)
93 {
94  /* check that unlock operates on the same lock (excl. process run-down) */
97  CORE_LOG(eLOG_Critical, "Inconsistent use of CORE MT-Lock");
98  assert(0);
99  return 0/*failure*/;
100  }
101  return 1/*success*/;
102 }
103 
104 #endif /*_DEBUG*/
105 
106 
107 extern const char* g_CORE_Sprintf(const char* fmt, ...)
108 {
109  static const size_t buf_size = 4096;
110  va_list args;
111  char* buf;
112  int len;
113 
114  if (!(buf = (char*) malloc(buf_size)))
115  return 0;
116  *buf = '\0';
117 
118  va_start(args, fmt);
119 #ifndef HAVE_VSNPRINTF
120  len = vsprintf (buf, fmt, args);
121 #else
122  len = vsnprintf(buf, buf_size, fmt, args);
123  if (len < 0 || buf_size <= len) {
124  memcpy(&buf[buf_size - 4], "...", 4);
125  len = (int)(buf_size - 1);
126  } else
127 #endif /*HAVE_VSNPRINTF*/
128  if (len <= 0)
129  *buf = '\0', len = 0;
130  assert(0 <= len && len < buf_size);
131  va_end(args);
132  return buf;
133 }
134 
135 
136 extern const char* g_CORE_RegistryGET
137 (const char* section,
138  const char* name,
139  char* value,
140  size_t value_size,
141  const char* def_value)
142 {
143  const char* retval;
145  retval = REG_Get(g_CORE_Registry,
146  section, name, value, value_size, def_value);
147  CORE_UNLOCK;
148  return retval;
149 }
150 
151 
152 extern int/*bool*/ g_CORE_RegistrySET
153 (const char* section,
154  const char* name,
155  const char* value,
156  EREG_Storage storage)
157 {
158  int/*bool*/ retval;
160  retval = REG_Set(g_CORE_Registry,
161  section, name, value, storage);
162  CORE_UNLOCK;
163  return retval;
164 }
@ eDefault
Definition: ncbi_types.h:112
unsigned int SOCK_GetLocalHostAddress(ESwitch reget)
Get (and cache for faster follow-up retrievals) IPv4 address of local host.
Definition: ncbi_socket.c:8827
const char * REG_Get(REG rg, const char *section, const char *name, char *value, size_t value_size, const char *def_value)
Copy the registry value stored in "section" under name "name" to buffer "value"; if the entry is foun...
Definition: ncbi_core.c:567
EREG_Storage
Transient/Persistent storage.
Definition: ncbi_core.h:528
int REG_Set(REG rg, const char *section, const char *name, const char *value, EREG_Storage storage)
Store the "value" into the registry section "section" under the key "name", and according to "storage...
Definition: ncbi_core.c:605
@ eLOG_Critical
Definition: ncbi_core.h:298
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
char * buf
int len
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
struct MT_LOCK_tag g_CORE_MT_Lock_default
Definition: ncbi_core.c:185
int g_CORE_RegistrySET(const char *section, const char *name, const char *value, EREG_Storage storage)
Definition: ncbi_priv.c:153
unsigned int g_NCBI_ConnectSrandAddend(void)
Definition: ncbi_priv.c:67
unsigned int g_NCBI_ConnectRandomSeed
Definition: ncbi_priv.c:51
int g_NCBI_CoreCheckUnlock(void)
Definition: ncbi_priv.c:92
LOG g_CORE_Log
Definition: ncbi_priv.c:49
const char * g_CORE_Sprintf(const char *fmt,...)
Definition: ncbi_priv.c:107
FNcbiGetRequestDtab g_CORE_GetRequestDtab
Definition: ncbi_priv.c:55
TCORE_Set g_CORE_Set
Definition: ncbi_priv.c:47
REG g_CORE_Registry
Definition: ncbi_priv.c:50
FNcbiGetRequestID g_CORE_GetRequestID
Definition: ncbi_priv.c:54
FNcbiGetAppName g_CORE_GetAppName
Definition: ncbi_priv.c:52
const char * g_CORE_RegistryGET(const char *section, const char *name, char *value, size_t value_size, const char *def_value)
Definition: ncbi_priv.c:137
MT_LOCK g_CORE_MT_Lock
Definition: ncbi_priv.c:48
static MT_LOCK s_CoreLock
Definition: ncbi_priv.c:83
FNcbiGetReferer g_CORE_GetReferer
Definition: ncbi_priv.c:53
int g_NCBI_CoreCheckLock(void)
Definition: ncbi_priv.c:85
const char *(* FNcbiGetRequestDtab)(void)
Definition: ncbi_priv.h:376
const char *(* FNcbiGetReferer)(void)
Definition: ncbi_priv.h:359
const char *(* FNcbiGetAppName)(void)
Definition: ncbi_priv.h:351
char *(* FNcbiGetRequestID)(ENcbiRequestID)
Definition: ncbi_priv.h:369
#define CORE_UNLOCK
Definition: ncbi_priv.h:273
unsigned int TCORE_Set
Definition: ncbi_priv.h:331
#define CORE_LOCK_READ
Definition: ncbi_priv.h:271
#define CORE_LOG(level, message)
Definition: ncbi_priv.h:154
#define vsnprintf
Definition: ncbiconf_msvc.h:66
#define assert(x)
Definition: srv_diag.hpp:58
voidp malloc(uInt size)
Modified on Sun Apr 14 05:28:29 2024 by modify_doxy.py rev. 669887