Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- Makefile | 2 +- clocks.c | 62 +------------------------------------------------------------- utils.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 22 ++++++++++++++++++++++ 4 files changed, 79 insertions(+), 62 deletions(-) create mode 100644 utils.c create mode 100644 utils.h
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