aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Lezcano <daniel.lezcano@linaro.org>2011-06-15 15:45:12 +0200
committerDaniel Lezcano <daniel.lezcano@linaro.org>2011-06-15 15:45:12 +0200
commit88b38e388fc20499eff99bb7282fccdb481f8828 (patch)
tree932f3b9100be2bf3413b229d9a8a8a5902f374a1
parent05916f88daf6dae6185f4ec5792b7ceb552cf8fc (diff)
downloadpowerdebug-88b38e388fc20499eff99bb7282fccdb481f8828.tar.gz
Add some helper functions in a specific utils file
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
-rw-r--r--Makefile2
-rw-r--r--clocks.c62
-rw-r--r--utils.c55
-rw-r--r--utils.h22
4 files changed, 79 insertions, 62 deletions
diff --git a/Makefile b/Makefile
index d88b8ff..1a53121 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ MANDIR=/usr/share/man/man8
CFLAGS?=-O1 -g -Wall -Wshadow
CC?=gcc
-OBJS = powerdebug.o sensor.o clocks.o regulator.o display.o tree.o
+OBJS = powerdebug.o sensor.o clocks.o regulator.o display.o tree.o utils.o
default: powerdebug
diff --git a/clocks.c b/clocks.c
index 603ebe4..4d78910 100644
--- a/clocks.c
+++ b/clocks.c
@@ -28,6 +28,7 @@
#include "powerdebug.h"
#include "clocks.h"
#include "tree.h"
+#include "utils.h"
struct clock_info {
int flags;
@@ -75,67 +76,6 @@ static struct clock_info *clock_alloc(void)
return ci;
}
-/*
- * This functions is a helper to read a specific file content and store
- * the content inside a variable pointer passed as parameter, the format
- * parameter gives the variable type to be read from the file.
- *
- * @path : directory path containing the file
- * @name : name of the file to be read
- * @format : the format of the format
- * @value : a pointer to a variable to store the content of the file
- * Returns 0 on success, -1 otherwise
- */
-int file_read_value(const char *path, const char *name,
- const char *format, void *value)
-{
- FILE *file;
- char *rpath;
- int ret;
-
- ret = asprintf(&rpath, "%s/%s", path, name);
- if (ret < 0)
- return ret;
-
- file = fopen(rpath, "r");
- if (!file) {
- ret = -1;
- goto out_free;
- }
-
- ret = fscanf(file, format, value) == EOF ? -1 : 0;
-
- fclose(file);
-out_free:
- free(rpath);
- return ret;
-}
-
-static int file_read_from_format(const char *file, int *value,
- const char *format)
-{
- FILE *f;
- int ret;
-
- f = fopen(file, "r");
- if (!f)
- return -1;
- ret = fscanf(f, format, value);
- fclose(f);
-
- return !ret ? -1 : 0;
-}
-
-static inline int file_read_int(const char *file, int *value)
-{
- return file_read_from_format(file, value, "%d");
-}
-
-static inline int file_read_hex(const char *file, int *value)
-{
- return file_read_from_format(file, value, "%x");
-}
-
static inline const char *clock_rate(int *rate)
{
int r;
diff --git a/utils.c b/utils.c
new file mode 100644
index 0000000..e47c58e
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (C) 2011, Linaro Limited.
+ *
+ * This file is part of PowerDebug.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation)
+ * - initial API and implementation
+ *******************************************************************************/
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#undef _GNU_SOURCE
+#include <stdlib.h>
+
+/*
+ * This functions is a helper to read a specific file content and store
+ * the content inside a variable pointer passed as parameter, the format
+ * parameter gives the variable type to be read from the file.
+ *
+ * @path : directory path containing the file
+ * @name : name of the file to be read
+ * @format : the format of the format
+ * @value : a pointer to a variable to store the content of the file
+ * Returns 0 on success, -1 otherwise
+ */
+int file_read_value(const char *path, const char *name,
+ const char *format, void *value)
+{
+ FILE *file;
+ char *rpath;
+ int ret;
+
+ ret = asprintf(&rpath, "%s/%s", path, name);
+ if (ret < 0)
+ return ret;
+
+ file = fopen(rpath, "r");
+ if (!file) {
+ ret = -1;
+ goto out_free;
+ }
+
+ ret = fscanf(file, format, value) == EOF ? -1 : 0;
+
+ fclose(file);
+out_free:
+ free(rpath);
+ return ret;
+}
diff --git a/utils.h b/utils.h
new file mode 100644
index 0000000..d4ac65a
--- /dev/null
+++ b/utils.h
@@ -0,0 +1,22 @@
+/*******************************************************************************
+ * Copyright (C) 2011, Linaro Limited.
+ *
+ * This file is part of PowerDebug.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation)
+ * - initial API and implementation
+ *******************************************************************************/
+#ifndef __UTILS_H
+#define __UTILS_H
+
+extern int file_read_value(const char *path, const char *name,
+ const char *format, void *value);
+
+
+#endif