From patchwork Wed Jun 15 13:50:35 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 1923 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 571BC23DE6 for ; Wed, 15 Jun 2011 13:52:40 +0000 (UTC) Received: from mail-vx0-f180.google.com (mail-vx0-f180.google.com [209.85.220.180]) by fiordland.canonical.com (Postfix) with ESMTP id 08221A18976 for ; Wed, 15 Jun 2011 13:52:39 +0000 (UTC) Received: by vxk12 with SMTP id 12so410110vxk.11 for ; Wed, 15 Jun 2011 06:52:39 -0700 (PDT) Received: by 10.52.112.106 with SMTP id ip10mr793942vdb.127.1308145959442; Wed, 15 Jun 2011 06:52:39 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.52.183.130 with SMTP id em2cs126625vdc; Wed, 15 Jun 2011 06:52:37 -0700 (PDT) Received: by 10.227.164.199 with SMTP id f7mr635988wby.70.1308145955973; Wed, 15 Jun 2011 06:52:35 -0700 (PDT) Received: from smtp.smtpout.orange.fr (smtp05.smtpout.orange.fr [80.12.242.127]) by mx.google.com with ESMTP id fe15si1397944wbb.0.2011.06.15.06.52.35; Wed, 15 Jun 2011 06:52:35 -0700 (PDT) Received-SPF: neutral (google.com: 80.12.242.127 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) client-ip=80.12.242.127; Authentication-Results: mx.google.com; spf=neutral (google.com: 80.12.242.127 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) smtp.mail=daniel.lezcano@linaro.org Received: from monster.dhcp.lxc ([92.134.76.78]) by mwinf5d28 with ME id wDsa1g00D1hMfSL03DsaAZ; Wed, 15 Jun 2011 15:52:35 +0200 From: Daniel Lezcano To: daniel.lezcano@linaro.org Cc: linaro-dev@lists.linaro.org, patches@linaro.org Subject: [powerdebug 01/22] Add some helper functions in a specific utils file Date: Wed, 15 Jun 2011 15:50:35 +0200 Message-Id: <1308145856-6112-1-git-send-email-daniel.lezcano@linaro.org> X-Mailer: git-send-email 1.7.1 Signed-off-by: Daniel Lezcano --- 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 (IBM Corporation) + * - initial API and implementation + *******************************************************************************/ + +#define _GNU_SOURCE +#include +#undef _GNU_SOURCE +#include + +/* + * 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 (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