shared code: windows simplifications

This commit is contained in:
Jean-Francois Dockes 2020-05-11 07:45:22 +02:00
parent d58fec0b81
commit 69df9196d9
8 changed files with 166 additions and 66 deletions

View File

@ -16,7 +16,6 @@
*/
#ifdef BUILDING_RECOLL
#include "autoconfig.h"
#include "transcode.h"
#else
#include "config.h"
#endif
@ -24,15 +23,10 @@
#include "conftree.h"
#include <ctype.h>
#if defined(BUILDING_RECOLL) || !defined(_WIN32)
#include <fnmatch.h>
#ifdef _WIN32
#include "safesysstat.h"
#else
#endif /* BUILDING_RECOLL */
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#endif
#include <algorithm>
#include <cstring>
@ -243,7 +237,7 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp,
fstream input = path_open(fname, mode);
if (!input.is_open()) {
LOGDEB0("ConfSimple::ConfSimple: fstream(w)(" << fname << ", " << mode <<
") errno " << errno << "\n");
") errno " << errno << "\n");
}
if (!readonly && !input.is_open()) {
@ -255,13 +249,13 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp,
}
if (!input.is_open()) {
// Don't log ENOENT, this is common with some recoll config files
string reason;
catstrerror(&reason, nullptr, errno);
if (errno != 2) {
LOGERR("ConfSimple::ConfSimple: fstream(" << fname << ", " <<
ios::in << ") " << reason << "\n");
}
// Don't log ENOENT, this is common with some recoll config files
string reason;
catstrerror(&reason, nullptr, errno);
if (errno != 2) {
LOGERR("ConfSimple::ConfSimple: fstream(" << fname << ", " <<
ios::in << ") " << reason << "\n");
}
status = STATUS_ERROR;
return;
}
@ -285,9 +279,9 @@ ConfSimple::StatusCode ConfSimple::getStatus() const
bool ConfSimple::sourceChanged() const
{
if (!m_filename.empty()) {
struct stat st;
if (stat(m_filename.c_str(), &st) == 0) {
if (m_fmtime != st.st_mtime) {
PathStat st;
if (path_fileprops(m_filename, &st) == 0) {
if (m_fmtime != st.pst_mtime) {
return true;
}
}
@ -298,11 +292,11 @@ bool ConfSimple::sourceChanged() const
bool ConfSimple::i_changed(bool upd)
{
if (!m_filename.empty()) {
struct stat st;
if (stat(m_filename.c_str(), &st) == 0) {
if (m_fmtime != st.st_mtime) {
PathStat st;
if (path_fileprops(m_filename, &st) == 0) {
if (m_fmtime != st.pst_mtime) {
if (upd) {
m_fmtime = st.st_mtime;
m_fmtime = st.pst_mtime;
}
return true;
}
@ -670,7 +664,14 @@ vector<string> ConfSimple::getNames(const string& sk, const char *pattern) const
}
mylist.reserve(ss->second.size());
for (const auto& item : ss->second) {
if (pattern && 0 != fnmatch(pattern, item.first.c_str(), 0)) {
if (pattern &&
#if defined(BUILDING_RECOLL) || !defined(_WIN32)
0 != fnmatch(pattern, item.first.c_str(), 0)
#else
/* Default to no match: yields easier to spot errors */
1
#endif
) {
continue;
}
mylist.push_back(item.first);

View File

@ -49,7 +49,6 @@
* (useful to have central/personal config files).
*/
#include <time.h>
#include <algorithm>
#include <map>
#include <string>
@ -219,7 +218,8 @@ public:
/** Print all values to stdout */
virtual void showall() const override;
/** Return all names in given submap. */
/** Return all names in given submap. On win32, the pattern thing
only works in recoll builds */
virtual std::vector<std::string> getNames(
const std::string& sk, const char *pattern = 0) const override;
@ -299,7 +299,7 @@ protected:
private:
// Set if we're working with a file
std::string m_filename;
time_t m_fmtime;
int64_t m_fmtime;
// Configuration data submaps (one per subkey, the main data has a
// null subkey)
std::map<std::string, std::map<std::string, std::string> > m_submaps;
@ -492,6 +492,8 @@ public:
return m_confs.front()->holdWrites(on);
}
/** Return all names in given submap. On win32, the pattern thing
only works in recoll builds */
virtual std::vector<std::string> getNames(
const std::string& sk, const char *pattern = 0) const override {
return getNames1(sk, pattern, false);

View File

@ -1063,7 +1063,7 @@ void ReExec::init(int argc, char *args[])
void ReExec::insertArgs(const vector<string>& args, int idx)
{
vector<string>::iterator it, cit;
vector<string>::iterator it;
unsigned int cmpoffset = (unsigned int) - 1;
if (idx == -1 || string::size_type(idx) >= m_argv.size()) {

View File

@ -50,6 +50,7 @@
#endif
#include <map>
#include <algorithm>
#ifdef MDU_INCLUDE_LOG
#include MDU_INCLUDE_LOG
@ -59,6 +60,10 @@
using namespace std;
#ifndef PRETEND_USE
#define PRETEND_USE(expr) ((void)(expr))
#endif
#ifndef SOCKLEN_T
#define SOCKLEN_T socklen_t
#endif
@ -81,12 +86,7 @@ static const int zero = 0;
LOGERR(who << ": " << call << "(" << spar << ") errno " << \
errno << " (" << strerror(errno) << ")\n")
#endif
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif
#ifndef freeZ
#define freeZ(X) if (X) {free(X);X=0;}
#endif
@ -276,11 +276,11 @@ int SelectLoop::doLoop()
pll->m_wantedEvents << "\n");
if (pll->m_wantedEvents & Netcon::NETCONPOLL_READ) {
FD_SET(fd, &rd);
nfds = MAX(nfds, fd + 1);
nfds = std::max(nfds, fd + 1);
}
if (pll->m_wantedEvents & Netcon::NETCONPOLL_WRITE) {
FD_SET(fd, &wd);
nfds = MAX(nfds, fd + 1);
nfds = std::max(nfds, fd + 1);
}
}
@ -708,7 +708,7 @@ void NetconData::cancelReceive()
// We can't do a thing about the ::write return value, the
// following nonsense is for cancelling warnings
int ret = ::write(m_wkfds[1], "!", 1);
ret = ret;
PRETEND_USE(ret);
}
}
@ -727,7 +727,7 @@ int NetconData::receive(char *buf, int cnt, int timeo)
// Get whatever might have been left in the buffer by a previous
// getline, except if we're called to fill the buffer of course
if (m_buf && m_bufbytes > 0 && (buf < m_buf || buf > m_buf + m_bufsize)) {
fromibuf = MIN(m_bufbytes, cnt);
fromibuf = std::min(m_bufbytes, cnt);
memcpy(buf, m_bufbase, fromibuf);
m_bufbytes -= fromibuf;
m_bufbase += fromibuf;
@ -750,7 +750,7 @@ int NetconData::receive(char *buf, int cnt, int timeo)
LOGDEB2("NetconData::receive: cancel fd " << m_wkfds[0] << endl);
FD_SET(m_wkfds[0], &rd);
}
int nfds = MAX(m_fd, m_wkfds[0]) + 1;
int nfds = std::max(m_fd, m_wkfds[0]) + 1;
int ret = select(nfds, &rd, 0, 0, &tv);
LOGDEB2("NetconData::receive: select returned " << ret << endl);
@ -760,7 +760,7 @@ int NetconData::receive(char *buf, int cnt, int timeo)
// We can't do a thing about the return value, the
// following nonsense is for cancelling warnings
int ret = ::read(m_wkfds[0], b, 100);
ret = ret;
PRETEND_USE(ret);
return Cancelled;
}
@ -834,7 +834,7 @@ int NetconData::getline(char *buf, int cnt, int timeo)
for (;;) {
// Transfer from buffer. Have to take a lot of care to keep counts and
// pointers consistant in all end cases
int maxtransf = MIN(m_bufbytes, cnt - 1);
int maxtransf = std::min(m_bufbytes, cnt - 1);
int nn = maxtransf;
LOGDEB2("Before loop, bufbytes " << m_bufbytes << ", maxtransf " <<
maxtransf << ", nn: " << nn << "\n");

View File

@ -45,19 +45,63 @@
#include "config.h"
#endif
#include <stdio.h>
#include <math.h>
#include <errno.h>
#include "pathut.h"
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <math.h>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
#include <fcntl.h>
#ifndef PRETEND_USE
#define PRETEND_USE(expr) ((void)(expr))
#endif
#ifdef _WIN32
#include "safefcntl.h"
#include "safeunistd.h"
#include "safewindows.h"
#include "safesysstat.h"
#include "transcode.h"
#if !defined(S_IFLNK)
#define S_IFLNK 0
#endif
#ifndef _MSC_VER
#undef WINVER
#define WINVER 0x0601
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0601
#define LOGFONTW void
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#define WIN32_LEAN_AND_MEAN
#define NOGDI
#define MAXPATHLEN PATH_MAX
#include <windows.h>
#include <io.h>
#include <sys/stat.h>
#ifndef S_ISDIR
# define S_ISDIR(ST_MODE) (((ST_MODE) & _S_IFMT) == _S_IFDIR)
#endif
#ifndef S_ISREG
# define S_ISREG(ST_MODE) (((ST_MODE) & _S_IFMT) == _S_IFREG)
#endif
#include <direct.h>
#include <Shlobj.h>
#include <Stringapiset.h>
#define STAT _wstati64
#define LSTAT _wstati64
@ -72,9 +116,10 @@
#define OPEN ::_wopen
#define UNLINK _wunlink
#define ftruncate _chsize_s
#else /* !_WIN32 -> */
#include <fcntl.h>
#include <unistd.h>
#include <sys/param.h>
#include <pwd.h>
@ -99,16 +144,6 @@
#endif /* !_WIN32 */
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <stack>
#include <set>
#include <vector>
#include <regex>
#include "pathut.h"
#include "smallut.h"
#ifdef MDU_INCLUDE_LOG
#include MDU_INCLUDE_LOG
@ -119,8 +154,69 @@
using namespace std;
#ifdef _WIN32
//#include <shlobj_core.h>
#include <Shlobj.h>
bool wchartoutf8(const wchar_t *in, std::string& out)
{
out.clear();
if (nullptr == in) {
return true;
}
size_t wlen = wcslen(in);
const UINT codePageUtf8 = CP_UTF8;
const DWORD flags = MB_ERR_INVALID_CHARS;
int bytes = ::WideCharToMultiByte(
codePageUtf8, flags, in, wlen, nullptr, 0, nullptr, nullptr);
if (bytes <= 0) {
std::cerr << "wchartoutf8: CONVERSION ERROR\n";
return false;
}
char *cp = (char *)malloc(bytes+1);
if (nullptr == cp) {
std::cerr << "wchartoutf8: MALLOC FAILED\n";
return false;
}
bytes = ::WideCharToMultiByte(
codePageUtf8, flags, in, wlen, cp, bytes, nullptr, nullptr);
if (bytes <= 0) {
std::cerr << "wchartoutf8: CONVERSION ERROR\n";
free(cp);
return false;
}
cp[bytes] = 0;
out = cp;
free(cp);
return true;
}
bool utf8towchar(const std::string& in, wchar_t *out, size_t obytescap)
{
size_t wcharsavail = obytescap / sizeof(wchar_t);
if (nullptr == out || wcharsavail < 1) {
return false;
}
out[0] = 0;
const UINT codePageUtf8 = CP_UTF8;
const DWORD flags = MB_ERR_INVALID_CHARS;
int wcharcnt = MultiByteToWideChar(
codePageUtf8, flags, in.c_str(), in.size(), nullptr, 0);
if (wcharcnt <= 0) {
std::cerr << "utf8towchar: CONVERSION ERROR\n";
return false;
}
if (wcharcnt + 1 > int(wcharsavail)) {
std::cerr << "utf8towchar: NOT ENOUGH SPACE\n";
return false;
}
wcharcnt = MultiByteToWideChar(
codePageUtf8, flags, in.c_str(), in.size(), out, wcharsavail);
if (wcharcnt <= 0) {
std::cerr << "utf8towchar: CONVERSION ERROR\n";
return false;
}
out[wcharcnt] = 0;
return true;
}
/// Convert \ separators to /
void path_slashize(string& s)

View File

@ -109,6 +109,8 @@ extern bool path_readable(const std::string& path);
extern std::string path_PATHsep();
#ifdef _WIN32
extern bool wchartoutf8(const wchar_t *in, std::string& out);
extern bool utf8towchar(const std::string& in, wchar_t *out, size_t obytescap);
#define SYSPATH(PATH, SPATH) wchar_t PATH ## _buf[2048]; \
utf8towchar(PATH, PATH ## _buf, 2048); \
wchar_t *SPATH = PATH ## _buf;

View File

@ -563,7 +563,7 @@ bool file_scan(const string& fn, FileScanDo* doer, int64_t startoffs,
FileScanSourceFile source(doer, fn, startoffs, cnttoread, reason);
FileScanUpstream *up = &source;
up = up;
PRETEND_USE(up);
#if defined(READFILE_ENABLE_ZLIB)
GzFilter gzfilter;
@ -637,7 +637,7 @@ bool string_scan(const char *data, size_t cnt, FileScanDo* doer,
{
FileScanSourceBuffer source(doer, data, cnt, reason);
FileScanUpstream *up = &source;
up = up;
PRETEND_USE(up);
#ifdef READFILE_ENABLE_MD5
string digest;

View File

@ -18,9 +18,8 @@
#ifndef _SMALLUT_H_INCLUDED_
#define _SMALLUT_H_INCLUDED_
#include <sys/types.h>
#include <stdint.h>
#include <time.h>
#include <string>
#include <vector>
#include <map>