NCBI C++ ToolKit
util_misc.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: util_misc.cpp 98121 2022-09-30 15:38:11Z ucko $
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: Sergey Satskiy,
27  * Anton Lavrentiev (providing line by line advices of how it must be
28  * implemented)
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <util/util_misc.hpp>
34 #include <corelib/ncbi_param.hpp>
35 #include <corelib/ncbifile.hpp>
36 
37 #if defined(NCBI_OS_UNIX)
38 # include <unistd.h>
39 #if defined(HAVE_READPASSPHRASE)
40 # include <readpassphrase.h>
41 #endif
42 #elif defined(NCBI_OS_MSWIN)
43 # include <conio.h>
44 #else
45 # error "Unsuported platform"
46 #endif
47 
48 
50 
51 
53 {
54  switch (GetErrCode()) {
55  case eGetPassError: return "eGetPassError";
56  case eKeyboardInterrupt: return "eKeyboardInterrupt";
57  default: return CException::GetErrCodeString();
58  }
59 }
60 
61 
62 string g_GetPasswordFromConsole(const string& prompt)
63 {
64  string password;
65  CMutex lock;
66  CMutexGuard guard(lock);
67 
68 #if defined(NCBI_OS_UNIX)
69  // UNIX implementation
70 
71 #if defined(HAVE_READPASSPHRASE)
72 
73  char password_buffer[1024];
74  char* raw_password = readpassphrase(prompt.c_str(), password_buffer,
75  sizeof(password_buffer),
77 
78 #elif defined(HAVE_GETPASSPHRASE)
79 
80  char* raw_password = getpassphrase(prompt.c_str());
81 
82 #elif defined(HAVE_GETPASS)
83 
84  char* raw_password = getpass(prompt.c_str());
85 
86 #else
87 # error "Unsupported Unix platform; the getpass, getpassphrase, and readpassphrase functions are all absent"
88 #endif
89 
90  if (!raw_password)
92  (CGetPasswordFromConsoleException, eGetPassError,
93  "g_GetPasswordFromConsole(): error getting password");
94  password = string(raw_password);
95 
96 #elif defined(NCBI_OS_MSWIN)
97  // Windows implementation
98 
99  for (size_t index = 0; index < prompt.size(); ++index) {
100  _putch(prompt[index]);
101  }
102 
103  for (;;) {
104  char ch;
105  ch = _getch();
106  if (ch == '\r' || ch == '\n')
107  break;
108  if (ch == '\003')
109  NCBI_THROW(CGetPasswordFromConsoleException, eKeyboardInterrupt,
110  "g_GetPasswordFromConsole(): keyboard interrupt");
111  if (ch == '\b') {
112  if ( !password.empty() ) {
113  password.resize(password.size() - 1);
114  }
115  }
116  else
117  password.append(1, ch);
118  }
119 
120  _putch('\r');
121  _putch('\n');
122 #endif
123 
124  return password;
125 }
126 
127 
128 NCBI_PARAM_DECL (string, NCBI, DataPath);
129 NCBI_PARAM_DEF_EX(string, NCBI, DataPath, "", 0, NCBI_DATA_PATH);
130 typedef NCBI_PARAM_TYPE(NCBI, DataPath) TNCBIDataPath;
131 
132 NCBI_PARAM_DECL(string, NCBI, Data);
133 NCBI_PARAM_DEF (string, NCBI, Data, "");
134 typedef NCBI_PARAM_TYPE(NCBI, Data) TNCBIDataDir;
135 
136 typedef vector<string> TIgnoreDataFiles;
138 
140 {
141 #ifdef NCBI_OS_MSWIN
142  static const char* kDelim = ";";
143 #else
144  static const char* kDelim = ":";
145 #endif
146 
147  if ( !s_IgnoredDataFiles->empty()
149  return kEmptyStr;
150  }
151 
152  list<string> dirs;
153 
154  if (CDirEntry::IsAbsolutePath(name)) {
155  dirs.push_back(kEmptyStr);
156  } else {
157  TNCBIDataPath path;
158  TNCBIDataDir dir;
159 
160  if ( !path.Get().empty() ) {
161  NStr::Split(path.Get(), kDelim, dirs,
163  }
164  if ( !dir.Get().empty() ) {
165  dirs.push_back(dir.Get());
166  }
167  }
168 
169  CDirEntry candidate;
171  ITERATE (list<string>, dir, dirs) {
172  candidate.Reset(CDirEntry::MakePath(*dir, name));
173  if (candidate.Exists() && candidate.GetType(fl) == type) {
174  return candidate.GetPath();
175  }
176  }
177 
178  return kEmptyStr; // not found
179 }
180 
181 
182 void g_IgnoreDataFile(const string& pattern, bool do_ignore)
183 {
184  vector<string>& idf = *s_IgnoredDataFiles;
185  if (do_ignore) {
186  idf.push_back(pattern);
187  } else {
188  idf.erase(remove(idf.begin(), idf.end(), pattern), idf.end());
189  }
190 }
191 
192 
193 bool g_IsDataFileOld(const CTempString& path, const CTempString& id_line)
194 {
195  // $Id: FILENAME REVISION DATE TIME ...
196  SIZE_TYPE pos = id_line.find("$Id: ");
197  if (pos == NPOS) {
198  return false;
199  }
200  pos = id_line.find(' ', pos + 5); // skip filename
201  if (pos == NPOS) {
202  return false;
203  }
204  pos = id_line.find(' ', pos + 1); // skip revision
205  if (pos == NPOS) {
206  return false;
207  }
208  SIZE_TYPE end = id_line.find(' ', ++pos);
209  if (end == NPOS) {
210  return false;
211  }
212  end = id_line.find(' ', end + 1); // got date, now want time too
213  if (end == NPOS) {
214  return false;
215  }
216  CTempString builtin_timestamp_str = id_line.substr(pos, end - pos);
217  CTime builtin_timestamp(builtin_timestamp_str, "Y-M-D h:m:sZ");
218  return g_IsDataFileOld(path, builtin_timestamp);
219 }
220 
221 bool g_IsDataFileOld(const CTempString& path, const CTime& builtin_timestamp)
222 {
223  CTime file_timestamp;
224  CFile(path).GetTime(&file_timestamp);
225  return file_timestamp < builtin_timestamp;
226 }
227 
229 
CDirEntry –.
Definition: ncbifile.hpp:262
CFile –.
Definition: ncbifile.hpp:1604
Exception class for g_GetPasswordFromConsole.
Definition: util_misc.hpp:61
@ eGetPassError
UNIX specific: error getting password.
Definition: util_misc.hpp:64
@ eKeyboardInterrupt
WIN specific: Ctrl+C has been sent.
Definition: util_misc.hpp:65
virtual const char * GetErrCodeString(void) const override
Translate from the error code value to its string representation.
Definition: util_misc.cpp:52
CMutex –.
Definition: ncbimtx.hpp:749
CSafeStatic<>::
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
CTime –.
Definition: ncbitime.hpp:296
static void DLIST_NAME() remove(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:90
#define readpassphrase
#define RPP_ECHO_OFF
#define RPP_REQUIRE_TTY
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
EFollowLinks
Whether to follow symbolic links (also known as shortcuts or aliases)
Definition: ncbimisc.hpp:143
@ eIgnoreLinks
Do not follow symbolic links.
Definition: ncbimisc.hpp:144
@ eFollowLinks
Follow symbolic links.
Definition: ncbimisc.hpp:145
string
Definition: cgiapp.hpp:687
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
EType
Directory entry type.
Definition: ncbifile.hpp:782
static bool IsAbsolutePath(const string &path)
Check if a "path" is absolute for the current OS.
Definition: ncbifile.cpp:508
virtual bool Exists(void) const
Check the entry existence.
Definition: ncbifile.cpp:2325
static string MakePath(const string &dir=kEmptyStr, const string &base=kEmptyStr, const string &ext=kEmptyStr)
Assemble a path from basic components.
Definition: ncbifile.cpp:413
void Reset(const string &path)
Reset path string.
Definition: ncbifile.cpp:298
static bool MatchesMask(const string &name, const string &mask, NStr::ECase use_case=NStr::eCase)
Match a "name" against a simple filename "mask".
Definition: ncbifile.hpp:3967
EType GetType(EFollowLinks follow=eIgnoreLinks) const
Get a type of a directory entry.
Definition: ncbifile.cpp:2203
bool GetTime(CTime *modification, CTime *last_access=0, CTime *creation=0) const
Get time stamp(s) of a directory entry.
Definition: ncbifile.cpp:1840
const string & GetPath(void) const
Get entry path.
Definition: ncbifile.hpp:3910
@ eLink
Symbolic link (UNIX only)
Definition: ncbifile.hpp:786
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
#define kEmptyStr
Definition: ncbistr.hpp:123
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
#define NPOS
Definition: ncbistr.hpp:133
CTempString substr(size_type pos) const
Obtain a substring from this string, beginning at a given offset.
Definition: tempstr.hpp:776
size_type find(const CTempString match, size_type pos=0) const
Find the first instance of the entire matching string within the current string, beginning at an opti...
Definition: tempstr.hpp:655
@ fSplit_Truncate
Definition: ncbistr.hpp:2501
@ fSplit_MergeDelimiters
Merge adjacent delimiters.
Definition: ncbistr.hpp:2498
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
NCBI
Definition: static_set.hpp:72
Definition: type.c:6
NCBI_PARAM_DECL(string, NCBI, DataPath)
NCBI_PARAM_DEF_EX(string, NCBI, DataPath, "", 0, NCBI_DATA_PATH)
typedef NCBI_PARAM_TYPE(NCBI, DataPath) TNCBIDataPath
NCBI_PARAM_DEF(string, NCBI, Data, "")
string g_GetPasswordFromConsole(const string &prompt)
Get a password without echoing the user input.
Definition: util_misc.cpp:62
string g_FindDataFile(const CTempString &name, CDirEntry::EType type)
Look for an NCBI application data file or directory of the given name and type; in general,...
Definition: util_misc.cpp:139
bool g_IsDataFileOld(const CTempString &path, const CTempString &id_line)
Check whether the given file (a full path, as returned by g_FindDataFile) is older than a built-in ve...
Definition: util_misc.cpp:193
void g_IgnoreDataFile(const string &pattern, bool do_ignore)
Ignore (or stop ignoring, depending on do_ignore) NCBI application data files matching the given patt...
Definition: util_misc.cpp:182
static CSafeStatic< TIgnoreDataFiles > s_IgnoredDataFiles
Definition: util_misc.cpp:137
vector< string > TIgnoreDataFiles
Definition: util_misc.cpp:136
Modified on Wed Jun 19 17:07:48 2024 by modify_doxy.py rev. 669887