From 82922bda8db249215f73cf379b4258675c56e34e Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Tue, 18 Aug 2015 12:44:53 +0200 Subject: [PATCH] removed a number of gratuitous linuxisms --- src/utils/Makefile | 12 ++++++++++++ src/utils/appformime.cpp | 4 +--- src/utils/appformime.h | 6 ++++-- src/utils/base64.cpp | 7 ++----- src/utils/debuglog.cpp | 14 ++++++++------ src/utils/idfile.cpp | 4 ---- src/utils/ptmutex.cpp | 1 - src/utils/readfile.cpp | 10 ++-------- src/utils/smallut.cpp | 6 ++---- src/utils/transcode.cpp | 14 ++++++-------- src/utils/workqueue.cpp | 30 +++++++++++++++++++++++++++--- src/utils/workqueue.h | 4 ++-- 12 files changed, 66 insertions(+), 46 deletions(-) diff --git a/src/utils/Makefile b/src/utils/Makefile index a5e943dc..e298dfed 100644 --- a/src/utils/Makefile +++ b/src/utils/Makefile @@ -14,6 +14,18 @@ pxattr: $(PXATTROBJS) trpxattr.o : pxattr.cpp $(CXX) -c $(CXXFLAGS) -DTEST_PXATTR -o $@ pxattr.cpp +PTMUTEXOBJS = ptmutex.o +ptmutex: $(PTMUTEXOBJS) + $(CXX) -o ptmutex $(PTMUTEXOBJS) $(LIBRECOLL) +ptmutex.o : ptmutex.cpp + $(CXX) -c $(CXXFLAGS) -o $@ $< + +WORKQUEUEOBJS = workqueue.o +workqueue: $(WORKQUEUEOBJS) + $(CXX) -o workqueue $(WORKQUEUEOBJS) $(LIBRECOLL) -lpthread +workqueue.o : workqueue.cpp + $(CXX) -c $(CXXFLAGS) -o $@ $< + ECRONTAB_OBJS= trecrontab.o trecrontab : $(ECRONTAB_OBJS) $(CXX) -o trecrontab $(ECRONTAB_OBJS) $(LIBRECOLL) diff --git a/src/utils/appformime.cpp b/src/utils/appformime.cpp index 3d26e08b..60a41a7e 100644 --- a/src/utils/appformime.cpp +++ b/src/utils/appformime.cpp @@ -176,7 +176,6 @@ const string& DesktopDb::getReason() #include #include -#include #include #include @@ -212,8 +211,7 @@ int main(int argc, char **argv) vector appdefs; DesktopDb *ddb = DesktopDb::getDb(); if (ddb == 0) { - cerr << "Could not initialize desktop db: " << DesktopDb::getReason() - << endl; + cerr << "Could not create desktop db\n"; exit(1); } if (!ddb->appForMime(mime, &appdefs, &reason)) { diff --git a/src/utils/appformime.h b/src/utils/appformime.h index 4f9155a6..c979f346 100644 --- a/src/utils/appformime.h +++ b/src/utils/appformime.h @@ -18,6 +18,8 @@ #define _APPFORMIME_H_INCLUDED_ #include +#include +#include /** * Rather strangely, I could not find a reasonably simple piece of @@ -71,12 +73,12 @@ public: */ bool appByName(const string& nm, AppDef& app); - typedef map > AppMap; + typedef std::map > AppMap; private: /** This is used by getDb() and builds a db for the standard location */ DesktopDb(); - void build(const string& dir); + void build(const std::string& dir); DesktopDb(const DesktopDb &); DesktopDb& operator=(const DesktopDb &); diff --git a/src/utils/base64.cpp b/src/utils/base64.cpp index 0e0bcbe7..87cd530d 100644 --- a/src/utils/base64.cpp +++ b/src/utils/base64.cpp @@ -20,9 +20,7 @@ #include #include -#ifndef NO_NAMESPACES using std::string; -#endif /* NO_NAMESPACES */ #undef DEBUG_BASE64 #ifdef DEBUG_BASE64 @@ -270,7 +268,6 @@ void base64_encode(const string &in, string &out) #ifdef TEST_BASE64 #include #include -#include #include "readfile.h" @@ -358,8 +355,8 @@ int main(int argc, char **argv) fprintf(stderr, "Decoding failed\n"); exit(1); } - ::write(1, odata.c_str(), - odata.size() * sizeof(string::value_type)); + fwrite(odata.c_str(), 1, + odata.size() * sizeof(string::value_type), stdout); exit(0); } } diff --git a/src/utils/debuglog.cpp b/src/utils/debuglog.cpp index 3d76c52e..eb9c4ae2 100644 --- a/src/utils/debuglog.cpp +++ b/src/utils/debuglog.cpp @@ -21,7 +21,9 @@ #include #include #include +#ifndef _WIN32 #include +#endif #ifdef INCLUDE_NEW_H #include @@ -41,12 +43,9 @@ using std::string; #define freeZ(X) {if (X) {free(X);X=0;}} #endif -#ifndef NO_NAMESPACES using namespace std; namespace DebugLog { -#endif // NO_NAMESPACES - bool DebugLog::isspecialname(const char *logname) { return !strcmp(logname, "stdout") || !strcmp(logname, "stderr"); @@ -193,7 +192,7 @@ static bool fileInFiles(const string& file) return false; } -#ifdef _WINDOWS +#ifdef _WIN32 #include static void datestring(char *d, int sz) { SYSTEMTIME buf; @@ -390,16 +389,19 @@ DebugLog *getdbl() } #endif -#ifndef NO_NAMESPACES } -#endif // NO_NAMESPACES ////////////////////////////////////////// TEST DRIVER ////////////////// #else /* TEST_DEBUGLOG */ #include #include +#ifdef _WIN32 +#include +static inline unsigned int sleep(unsigned int s) {Sleep(s * 1000); return 0;} +#else #include +#endif #include #include "debuglog.h" diff --git a/src/utils/idfile.cpp b/src/utils/idfile.cpp index 8be93f00..0a148f15 100644 --- a/src/utils/idfile.cpp +++ b/src/utils/idfile.cpp @@ -15,7 +15,6 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef TEST_IDFILE -#include // for access(2) #include #include #include @@ -26,9 +25,7 @@ #include "idfile.h" #include "debuglog.h" -#ifndef NO_NAMESPACES using namespace std; -#endif /* NO_NAMESPACES */ // Bogus code to avoid bogus valgrind mt warnings about the // initialization of treat_mbox_... which I can't even remember the @@ -179,7 +176,6 @@ string idFileMem(const string& data) #include #include -#include #include using namespace std; diff --git a/src/utils/ptmutex.cpp b/src/utils/ptmutex.cpp index 7e6e2e87..bbb63bd0 100644 --- a/src/utils/ptmutex.cpp +++ b/src/utils/ptmutex.cpp @@ -25,7 +25,6 @@ #include #include -#include #include using namespace std; diff --git a/src/utils/readfile.cpp b/src/utils/readfile.cpp index 858a5155..4c6905d6 100644 --- a/src/utils/readfile.cpp +++ b/src/utils/readfile.cpp @@ -22,18 +22,13 @@ #include #include -#ifndef O_STREAMING -#define O_STREAMING 0 -#endif #include #include #include #include -#ifndef NO_NAMESPACES using std::string; -#endif /* NO_NAMESPACES */ #include "readfile.h" #include "smallut.h" @@ -78,7 +73,7 @@ bool file_scan(const string &fn, FileScanDo* doer, string *reason) return file_scan(fn, doer, 0, size_t(-1), reason); } -const int RDBUFSZ = 4096; +const int RDBUFSZ = 8192; // Note: the fstat() + reserve() (in init()) calls divide cpu usage almost by 2 // on both linux i586 and macosx (compared to just append()) // Also tried a version with mmap, but it's actually slower on the mac and not @@ -95,7 +90,7 @@ bool file_scan(const string &fn, FileScanDo* doer, off_t startoffs, // If we have a file name, open it, else use stdin. if (!fn.empty()) { - fd = open(fn.c_str(), O_RDONLY|O_STREAMING); + fd = open(fn.c_str(), O_RDONLY); if (fd < 0 || fstat(fd, &st) < 0) { catstrerror(reason, "open/stat", errno); return false; @@ -170,7 +165,6 @@ bool file_scan(const string &fn, FileScanDo* doer, off_t startoffs, #include #include #include -#include #include #include diff --git a/src/utils/smallut.cpp b/src/utils/smallut.cpp index c148e74c..8087eeef 100644 --- a/src/utils/smallut.cpp +++ b/src/utils/smallut.cpp @@ -22,11 +22,8 @@ #include #include #include -#include #include #include -#include -#include #include #include @@ -1084,7 +1081,8 @@ void catstrerror(string *reason, const char *what, int _errno) // normally default to the posix version. // At worse we get no message at all here. errbuf[0] = 0; - strerror_r(_errno, errbuf, ERRBUFSZ); + // We don't use ret, it's there to silence a cc warning + char *ret = (char *)strerror_r(_errno, errbuf, ERRBUFSZ); reason->append(errbuf); #endif } diff --git a/src/utils/transcode.cpp b/src/utils/transcode.cpp index aec847d7..498e6629 100644 --- a/src/utils/transcode.cpp +++ b/src/utils/transcode.cpp @@ -20,9 +20,8 @@ #include #include -#ifndef NO_NAMESPACES + using std::string; -#endif /* NO_NAMESPACES */ #include #include @@ -165,7 +164,6 @@ error: #include #include #include -#include #include #include @@ -219,16 +217,16 @@ int main(int argc, char **argv) cerr << out << endl; exit(1); } - int fd = open(ofilename.c_str(), O_CREAT|O_TRUNC|O_WRONLY, 0666); - if (fd < 0) { + FILE *fp = fopen(ofilename.c_str(), "wb"); + if (fp == 0) { perror("Open/create output"); exit(1); } - if (write(fd, out.c_str(), out.length()) != (int)out.length()) { - perror("write"); + if (fwrite(out.c_str(), 1, out.length(), fp) != (int)out.length()) { + perror("fwrite"); exit(1); } - close(fd); + fclose(fp); exit(0); } #endif diff --git a/src/utils/workqueue.cpp b/src/utils/workqueue.cpp index b3d6a2f7..16f56b2e 100644 --- a/src/utils/workqueue.cpp +++ b/src/utils/workqueue.cpp @@ -1,9 +1,33 @@ +/* Copyright (C) 2014 J.F.Dockes + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +// Test program for the workqueue module + #include #include -#include #include #include +#ifdef _WIN32 +#include +static inline unsigned int sleep(unsigned int s) {Sleep(s * 1000); return 0;} +#else +#include +#endif + #include "workqueue.h" static char *thisprog; @@ -80,9 +104,9 @@ int main(int argc, char **argv) if (argc != 0) Usage(); - WorkQueue wq(10); + WorkQueue wq("testwq", 10); - if (!wq.start(&worker, &wq)) { + if (!wq.start(2, &worker, &wq)) { fprintf(stderr, "Start failed\n"); exit(1); } diff --git a/src/utils/workqueue.h b/src/utils/workqueue.h index 164bac12..9a245d8a 100644 --- a/src/utils/workqueue.h +++ b/src/utils/workqueue.h @@ -49,7 +49,7 @@ public: * meaning no limit. hi == -1 means that the queue is disabled. * @param lo minimum count of tasks before worker starts. Default 1. */ - WorkQueue(const string& name, size_t hi = 0, size_t lo = 1) + WorkQueue(const std::string& name, size_t hi = 0, size_t lo = 1) : m_name(name), m_high(hi), m_low(lo), m_workers_exited(0), m_clients_waiting(0), m_workers_waiting(0), m_tottasks(0), m_nowake(0), m_workersleeps(0), m_clientsleeps(0) @@ -309,7 +309,7 @@ private: } // Configuration - string m_name; + std::string m_name; size_t m_high; size_t m_low;