From 5fa64f43b85d5af0e949901d58f23f5f74ef4931 Mon Sep 17 00:00:00 2001 From: Stefan Seyfried Date: Sun, 24 Jun 2012 12:26:03 +0200 Subject: [PATCH] create common proc_tools proc_put, proc_get and proc_get_hex are used in many files. Avoid duplication by moving them to common/proc_tools.c. Convert azbox's cVideo to use proc_tools.h Origin commit data ------------------ Branch: master Commit: https://github.com/neutrino-images/ni-libstb-hal/commit/47941bd84986d655c58ee105a6c77c5f961b8f09 Author: Stefan Seyfried Date: 2012-06-24 (Sun, 24 Jun 2012) ------------------ This commit was generated by Migit --- Makefile.am | 3 ++- azbox/video.cpp | 47 +++--------------------------------- common/Makefile.am | 3 ++- common/proc_tools.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ common/proc_tools.h | 20 +++++++++++++++ 5 files changed, 86 insertions(+), 46 deletions(-) create mode 100644 common/proc_tools.c create mode 100644 common/proc_tools.h diff --git a/Makefile.am b/Makefile.am index 3fd7047..0a9c765 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,7 +5,8 @@ bin_PROGRAMS = libstb-hal-test libstb_hal_a_LIBADD = \ common/ca.o \ - common/lt_debug.o + common/lt_debug.o \ + common/proc_tools.o libstb_hal_test_SOURCES = libtest.cpp libstb_hal_test_LDADD = \ diff --git a/azbox/video.cpp b/azbox/video.cpp index a4aa2b3..67aef48 100644 --- a/azbox/video.cpp +++ b/azbox/video.cpp @@ -34,6 +34,9 @@ #include #include + +#include + #include "video_lib.h" #define VIDEO_DEVICE "/dev/dvb/adapter0/video0" #include "lt_debug.h" @@ -67,50 +70,6 @@ static bool stillpicture = false; #define VIDEO_STREAMTYPE_VC1_SM 5 #define VIDEO_STREAMTYPE_MPEG1 6 - -static int proc_put(const char *path, const char *value, const int len) -{ - int ret, ret2; - int pfd = open(path, O_WRONLY); - if (pfd < 0) - return pfd; - ret = write(pfd, value, len); - ret2 = close(pfd); - if (ret2 < 0) - return ret2; - return ret; -} - -static int proc_get(const char *path, char *value, const int len) -{ - int ret, ret2; - int pfd = open(path, O_RDONLY); - if (pfd < 0) - return pfd; - ret = read(pfd, value, len); - value[len-1] = '\0'; /* make sure string is terminated */ - if (ret >= 0) - { - while (ret > 0 && isspace(value[ret-1])) - ret--; /* remove trailing whitespace */ - value[ret] = '\0'; /* terminate, even if ret = 0 */ - } - ret2 = close(pfd); - if (ret2 < 0) - return ret2; - return ret; -} - -static unsigned int proc_get_hex(const char *path) -{ - unsigned int n, ret = 0; - char buf[16]; - n = proc_get(path, buf, 16); - if (n > 0) - sscanf(buf, "%x", &ret); - return ret; -} - cVideo::cVideo(int, void *, void *) { lt_debug("%s\n", __FUNCTION__); diff --git a/common/Makefile.am b/common/Makefile.am index 3a89fe0..200f85d 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -4,4 +4,5 @@ AM_CXXFLAGS = -fno-rtti -fno-exceptions -fno-strict-aliasing libcommon_a_SOURCES = \ ca.cpp \ - lt_debug.cpp + lt_debug.cpp \ + proc_tools.c diff --git a/common/proc_tools.c b/common/proc_tools.c new file mode 100644 index 0000000..0983de9 --- /dev/null +++ b/common/proc_tools.c @@ -0,0 +1,59 @@ +/* + * helper functions to interact with files, usually in the proc filesystem + * + * (C) 2012 Stefan Seyfried + * + * License: GPLv2 or later + * + */ +#include +#include +#include +#include +#include /* isspace */ +#include /* sscanf */ + +#include "proc_tools.h" + +int proc_put(const char *path, const char *value, const int len) +{ + int ret, ret2; + int pfd = open(path, O_WRONLY); + if (pfd < 0) + return pfd; + ret = write(pfd, value, len); + ret2 = close(pfd); + if (ret2 < 0) + return ret2; + return ret; +} + +int proc_get(const char *path, char *value, const int len) +{ + int ret, ret2; + int pfd = open(path, O_RDONLY); + if (pfd < 0) + return pfd; + ret = read(pfd, value, len); + value[len-1] = '\0'; /* make sure string is terminated */ + if (ret >= 0) + { + while (ret > 0 && isspace(value[ret-1])) + ret--; /* remove trailing whitespace */ + value[ret] = '\0'; /* terminate, even if ret = 0 */ + } + ret2 = close(pfd); + if (ret2 < 0) + return ret2; + return ret; +} + +unsigned int proc_get_hex(const char *path) +{ + unsigned int n, ret = 0; + char buf[16]; + n = proc_get(path, buf, 16); + if (n > 0) + sscanf(buf, "%x", &ret); + return ret; +} diff --git a/common/proc_tools.h b/common/proc_tools.h new file mode 100644 index 0000000..2a3a8cd --- /dev/null +++ b/common/proc_tools.h @@ -0,0 +1,20 @@ +/* + * helper functions to interact with files, usually in the proc filesystem + * + * (C) 2012 Stefan Seyfried + * + * License: GPLv2 or later + * + */ +#ifndef __PROC_TOOLS_H__ +#define __PROC_TOOLS_H__ +#ifdef __cplusplus +extern "C" { +#endif +int proc_put(const char *path, const char *value, const int len); +int proc_get(const char *path, char *value, const int len); +unsigned int proc_get_hex(const char *path); +#ifdef __cplusplus +} +#endif +#endif