1 /*  $Id: IdLogUtl.cpp 584459 2019-04-11 18:46:04Z dmitrie1 $
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  * Authors: Dmitri Dmitrienko
27  *
28  * File Description:
29  *
30  *  useful utilities
31  *
32  */
33 
34 #include <ncbi_pch.hpp>
35 
36 #include <sys/time.h>
37 
38 #include <iostream>
39 #include <objtools/pubseq_gateway/impl/diag/IdLogUtl.hpp>
40 
41 namespace IdLogUtil {
42 
43 USING_NCBI_SCOPE;
44 
DumpBinBuffer(const unsigned char * buf,size_t len)45 void DumpBinBuffer(const unsigned char* buf, size_t len) {
46 	for (size_t i = 0; i < len; i++)
47 		NcbiCout << setfill('0') << setw(2) << hex << (int)buf[i];
48 	NcbiCout << dec << NcbiEndl;
49 }
50 
DumpBinBuffer(const unsigned char * buf,size_t len,const string & filename)51 void DumpBinBuffer(const unsigned char* buf, size_t len, const string& filename) {
52 	filebuf fb;
53 	if (fb.open( filename.c_str(), ios::out | ios::binary)) {
54 		fb.sputn(reinterpret_cast<const char*>(buf), len);
55 		fb.close();
56 	}
57 }
58 
StringFromFile(const string & filename,string & str)59 void StringFromFile(const string& filename, string& str) {
60     filebuf fb;
61     if (fb.open( filename.c_str(), ios::in | ios::binary)) {
62 	    streampos sz = fb.pubseekoff(0, ios_base::end);
63 	    fb.pubseekoff(0, ios_base::beg);
64 	    str.resize((size_t)sz);
65 	    if (sz > 0) {
66 		    fb.sgetn(&(str.front()), sz);
67 		}
68 	    fb.close();
69 	}
70 }
71 
StringToFile(const string & filename,const string & str)72 void StringToFile(const string& filename, const string& str) {
73     filebuf fb;
74     if (fb.open( filename.c_str(), ios::out | ios::binary)) {
75 	    fb.sputn(&(str.front()), str.size());
76 	    fb.close();
77 	}
78 }
79 
StringReplace(string & where,const string & what,const string & replace)80 void StringReplace(string& where, const string& what, const string& replace) {
81 	size_t pos = 0;
82 	while (pos < where.size()) {
83 		size_t nx = where.find(what, pos);
84 		if (nx != string::npos)
85 			where.replace(nx, what.size(), replace);
86 		else
87 			break;
88 		pos = nx + what.size();
89 	}
90 }
91 
StringFetch(string & src,char delim)92 string StringFetch(string& src, char delim) {
93 	auto pos = src.find(delim);
94     string rv;
95     if (pos == string::npos) {
96         rv = src;
97         src.clear();
98     }
99     else {
100         rv = src.substr(0, pos);
101         src = src.substr(pos + 1);
102     }
103     return rv;
104 }
105 
gettime(void)106 int64_t gettime(void) {
107 	int cnt = 100;
108 
109 	struct timeval tv = {0};
110 	while (gettimeofday(&tv, NULL) != 0 && cnt-- > 0);
111 	return (int64_t)tv.tv_usec + ((int64_t)tv.tv_sec) * 1000000L;
112 }
113 
gettime_ns(void)114 int64_t gettime_ns(void) {
115 	int cnt = 100;
116 	struct timeval tv = {0};
117 	while (gettimeofday(&tv, NULL) != 0 && cnt-- > 0);
118 	return (int64_t)tv.tv_usec * 1000L + ((int64_t)tv.tv_sec) * 1000000000L;
119 }
120 
Int64ToDt(int64_t dt,bool inUTC)121 string Int64ToDt(int64_t dt, bool inUTC) {
122 	char buf[64], bufms[64];
123 	struct tm tm;
124 	time_t t = (time_t)(dt / 1000L);
125 	if (inUTC)
126 		localtime_r(&t, &tm);
127 	else {
128 		gmtime_r(&t, &tm);
129 	}
130 	strftime(buf, sizeof(buf), "%m/%d/%Y %H:%M:%S", &tm);
131 	snprintf(bufms, sizeof(bufms), "%s.%03u", buf, (unsigned int)(dt % 1000L));
132 	return string(bufms);
133 }
134 
TrimRight(const string & s,const string & delimiters)135 string TrimRight(const string& s, const string& delimiters) {
136 	return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
137 }
138 
TrimLeft(const string & s,const string & delimiters)139 string TrimLeft(const string& s, const string& delimiters) {
140 	return s.substr( s.find_first_not_of( delimiters ) );
141 }
142 
Trim(const string & s,const string & delimiters)143 string Trim(const string& s, const string& delimiters) {
144 	return TrimLeft(TrimRight(s, delimiters), delimiters);
145 }
146 
147 
148 };
149 
150