Update compat functions

This commit is contained in:
Christian W. Zuckschwerdt 2018-12-16 11:11:03 +00:00
parent d17e9a5818
commit 01a21b2c36
11 changed files with 52 additions and 113 deletions

View file

@ -3,9 +3,10 @@
// issue: Linux and Windows use different common paths for config files
// solution: provide specific default paths for each system
#ifndef COMPAT_PATHS_INCLUDED
#define COMPAT_PATHS_INCLUDED
#ifndef COMPAT_PATHS_H
#define COMPAT_PATHS_H
char **compat_getDefaultConfPaths(); // get default search paths for rtl_433 config file
/// get default search paths for rtl_433 config file
char **compat_get_default_conf_paths();
#endif // COMPAT_PATHS_INCLUDED
#endif // COMPAT_PATHS_H

View file

@ -3,8 +3,8 @@
// issue: <sys/time.h> is not available on Windows systems
// solution: provide a compatible version for Windows systems
#ifndef COMPAT_TIME_INCLUDED
#define COMPAT_TIME_INCLUDED
#ifndef COMPAT_TIME_H
#define COMPAT_TIME_H
// ensure struct timeval is known
#ifdef _WIN32
@ -14,6 +14,9 @@
#endif
// platform-specific functions
void compat_get_time_now(struct timeval *tv); // get high precision time
#endif // COMPAT_TIME_INCLUDED
#ifdef _WIN32
int gettimeofday(struct timeval *tv, void *tz);
#endif
#endif // COMPAT_TIME_H

View file

@ -27,7 +27,6 @@ add_executable(rtl_433
decoder_util.c
fileformat.c
optparse.c
platform_win.c
pulse_demod.c
pulse_detect.c
rtl_433.c

View file

@ -13,7 +13,6 @@ rtl_433_SOURCES = am_analyze.c \
decoder_util.c \
fileformat.c \
optparse.c \
platform_win.c \
pulse_demod.c \
pulse_detect.c \
rtl_433.c \

View file

@ -11,19 +11,21 @@
#include <stdio.h>
#include <stdlib.h>
char **compat_getDefaultConfPaths()
#include "compat_paths.h"
char **compat_get_default_conf_paths()
{
static char *pointers[5] = { NULL };
static char *paths[5] = { NULL };
static char buf[256] = "";
if (!pointers[0]) {
pointers[0] = "rtl_433.conf";
if (!paths[0]) {
paths[0] = "rtl_433.conf";
snprintf(buf, sizeof(buf), "%s%s", getenv("HOME"), "/.rtl_433.conf");
pointers[1] = buf;
pointers[2] = "/usr/local/etc/rtl_433.conf";
pointers[3] = "/etc/rtl_433.conf";
pointers[4] = NULL;
paths[1] = buf;
paths[2] = "/usr/local/etc/rtl_433/rtl_433.conf";
paths[3] = "/etc/rtl_433/rtl_433.conf";
paths[4] = NULL;
};
return pointers;
return paths;
}
#else
@ -33,38 +35,40 @@ char **compat_getDefaultConfPaths()
#include <stddef.h>
#include <Shlobj.h>
char **compat_getDefaultConfPaths()
#include "compat_paths.h"
char **compat_get_default_conf_paths()
{
static char bufs[3][256];
static char *pointers[4] = { NULL };
if (pointers[0]) return pointers;
static char *paths[4] = { NULL };
if (paths[0]) return paths;
// Working directory, i.e. where the binary is located
if (GetModuleFileName(NULL, bufs[0], sizeof(bufs[0]))) {
char *last_slash = max(strrchr(bufs[0], '\\'), strrchr(bufs[0], '/'));
if (last_slash) *last_slash = 0;
strcat_s(bufs[0], sizeof(bufs[0]), "\\rtl_433.conf");
pointers[0] = bufs[0];
paths[0] = bufs[0];
}
else {
pointers[0] = NULL;
paths[0] = NULL;
}
// Local per user configuration files (e.g. Win7: C:\Users\myusername\AppData\Local\rtl_433\rtl_433.conf)
if (SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, bufs[1]) == S_OK) {
strcat_s(bufs[1], sizeof(bufs[1]), "\\rtl_433\\rtl_433.conf");
pointers[1] = bufs[1];
paths[1] = bufs[1];
}
else {
pointers[1] = NULL;
paths[1] = NULL;
}
// Per machine configuration data (e.g. Win7: C:\ProgramData\rtl_433\rtl_433.conf)
if (SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, bufs[2]) == S_OK) {
strcat_s(bufs[2], sizeof(bufs[2]), "\\rtl_433\\rtl_433.conf");
pointers[2] = bufs[2];
paths[2] = bufs[2];
}
else {
pointers[2] = NULL;
paths[2] = NULL;
}
pointers[3] = NULL;
return pointers;
paths[3] = NULL;
return paths;
}
#endif // _WIN32 / !_WIN32

View file

@ -6,23 +6,19 @@
#ifndef _WIN32
// Linux variant
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/time.h>
void compat_get_time_now(struct timeval *tv)
// just so the compilation unit isn't empty
int _compat_time(void)
{
int ret = gettimeofday(tv, NULL);
if (ret)
perror("gettimeofday");
return 0;
}
#else
// Windows variant
#include <stdbool.h>
#include <stddef.h>
#include <winsock2.h>
#include "compat_time.h"
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
@ -30,8 +26,11 @@ void compat_get_time_now(struct timeval *tv)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif
void compat_get_time_now(struct timeval *tv)
int gettimeofday(struct timeval *tv, void *tz)
{
if (tz)
return -1; // we don't support TZ
FILETIME ft;
unsigned __int64 t64;
GetSystemTimeAsFileTime(&ft);
@ -40,6 +39,8 @@ void compat_get_time_now(struct timeval *tv)
t64 -= DELTA_EPOCH_IN_MICROSECS; // convert file time to unix epoch
tv->tv_sec = (long)(t64 / 1000000UL);
tv->tv_usec = (long)(t64 % 1000000UL);
return 0;
}
#endif // _WIN32 / !_WIN32

View file

@ -1,65 +0,0 @@
// platform-specific stuff
// only used for single functions needing different treatment
#ifdef _WIN32
#include <stdbool.h>
#include <stddef.h>
#include <Shlobj.h>
#include "platform.h"
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif
char **pf_getDefaultConfPaths()
{
static char bufs[3][256];
static char *pointers[4] = { NULL };
if (pointers[0]) return pointers;
// Working directory, i.e. where the binary is located
if (GetModuleFileName(NULL, bufs[0], sizeof(bufs[0]))) {
char *last_slash = max(strrchr(bufs[0], '\\'), strrchr(bufs[0], '/'));
if (last_slash) *last_slash = 0;
strcat_s(bufs[0], sizeof(bufs[0]), "\\rtl_433.conf");
pointers[0] = bufs[0];
}
else {
pointers[0] = NULL;
}
// Local per user configuration files (e.g. Win7: C:\Users\myusername\AppData\Local\rtl_433\rtl_433.conf)
if (SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, bufs[1]) == S_OK) {
strcat_s(bufs[1], sizeof(bufs[1]), "\\rtl_433\\rtl_433.conf");
pointers[1] = bufs[1];
}
else {
pointers[1] = NULL;
}
// Per machine configuration data (e.g. Win7: C:\ProgramData\rtl_433\rtl_433.conf)
if (SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, bufs[2]) == S_OK) {
strcat_s(bufs[2], sizeof(bufs[2]), "\\rtl_433\\rtl_433.conf");
pointers[2] = bufs[2];
}
else {
pointers[2] = NULL;
}
pointers[3] = NULL;
return pointers;
}
void pf_get_time_now(pf_timeval *tv)
{
FILETIME ft;
unsigned __int64 t64;
GetSystemTimeAsFileTime(&ft);
t64 = (((unsigned __int64) ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
t64 /= 10; // convert to microseconds
t64 -= DELTA_EPOCH_IN_MICROSECS; // convert file time to unix epoch
tv->tv_sec = (long)(t64 / 1000000UL);
tv->tv_usec = (long)(t64 % 1000000UL);
}
#endif // _WIN32

View file

@ -1110,7 +1110,7 @@ static void parse_conf_file(struct app_cfg *cfg, char const *path)
static void parse_conf_try_default_files(struct app_cfg *cfg)
{
char **paths = compat_getDefaultConfPaths();
char **paths = compat_get_default_conf_paths();
for (int a = 0; paths[a]; a++) {
fprintf(stderr, "Trying conf file at \"%s\"...\n", paths[a]);
if (hasconf(paths[a])) {

View file

@ -12,7 +12,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "compat_time.h"
uint8_t reverse8(uint8_t x)
{
@ -250,7 +249,9 @@ int add_bytes(uint8_t const message[], unsigned num_bytes)
void get_time_now(struct timeval *tv)
{
compat_get_time_now(tv);
int ret = gettimeofday(tv, NULL);
if (ret)
perror("gettimeofday");
}
char *local_time_str(time_t time_secs, char *buf)

View file

@ -117,7 +117,6 @@
<ClCompile Include="..\src\decoder_util.c" />
<ClCompile Include="..\src\fileformat.c" />
<ClCompile Include="..\src\optparse.c" />
<ClCompile Include="..\src\platform_win.c" />
<ClCompile Include="..\src\pulse_demod.c" />
<ClCompile Include="..\src\pulse_detect.c" />
<ClCompile Include="..\src\rtl_433.c" />

View file

@ -109,9 +109,6 @@
<ClCompile Include="..\src\optparse.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\platform_win.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\pulse_demod.c">
<Filter>Source Files</Filter>
</ClCompile>