diff options
author | reinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f> | 2004-02-27 07:06:26 +0000 |
---|---|---|
committer | reinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f> | 2004-02-27 07:06:26 +0000 |
commit | 485a26f6e3e2e84d7f9b8279fa6d7f440ea50a8b (patch) | |
tree | 01b27139b7878ca0b0256e2ff149e28b464715cf | |
parent | af9193af58b39bf31ee8beabfac620b274da8b1c (diff) | |
download | lcd4linux-485a26f6e3e2e84d7f9b8279fa6d7f440ea50a8b.tar.gz |
[lcd4linux @ 2004-02-27 07:06:24 by reinelt]
new function 'qprintf()' (simple but quick snprintf() replacement)
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@379 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | Makefile.in | 20 | ||||
-rw-r--r-- | lcd4linux.c | 8 | ||||
-rw-r--r-- | qprintf.c | 143 | ||||
-rw-r--r-- | qprintf.h | 39 |
5 files changed, 200 insertions, 11 deletions
diff --git a/Makefile.am b/Makefile.am index 9432318..528eeff 100644 --- a/Makefile.am +++ b/Makefile.am @@ -27,6 +27,7 @@ lock.c lock.h \ pid.c pid.h \ timer.c timer.h \ udelay.c udelay.h \ +qprintf.c qprintf.h \ \ widget.c widget.h \ widget_text.c widget_text.h \ diff --git a/Makefile.in b/Makefile.in index 342b375..f596ea5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -106,7 +106,7 @@ lcd4linux_LDADD = @DRIVERS@ @DRVLIBS@ #remove next line for liblcd4linux lcd4linux_DEPENDENCIES = @DRIVERS@ -lcd4linux_SOURCES = lcd4linux.c cfg.c cfg.h debug.c debug.h drv.c drv.h evaluator.c evaluator.h hash.c hash.h layout.c layout.h lock.c lock.h pid.c pid.h timer.c timer.h udelay.c udelay.h widget.c widget.h widget_text.c widget_text.h widget_bar.c widget_bar.h widget_icon.c widget_icon.h plugin.c plugin.h plugin_math.c plugin_string.c plugin_cfg.c plugin_uname.c plugin_loadavg.c plugin_proc_stat.c plugin_cpuinfo.c plugin_meminfo.c plugin_netdev.c plugin_ppp.c plugin_dvb.c plugin_i2c_sensors.c plugin_imon.c plugin_xmms.c +lcd4linux_SOURCES = lcd4linux.c cfg.c cfg.h debug.c debug.h drv.c drv.h evaluator.c evaluator.h hash.c hash.h layout.c layout.h lock.c lock.h pid.c pid.h timer.c timer.h udelay.c udelay.h qprintf.c qprintf.h widget.c widget.h widget_text.c widget_text.h widget_bar.c widget_bar.h widget_icon.c widget_icon.h plugin.c plugin.h plugin_math.c plugin_string.c plugin_cfg.c plugin_uname.c plugin_loadavg.c plugin_proc_stat.c plugin_cpuinfo.c plugin_meminfo.c plugin_netdev.c plugin_ppp.c plugin_dvb.c plugin_i2c_sensors.c plugin_imon.c plugin_xmms.c #liblcd4linux_la_DEPENDENCIES = @DRIVERS@ @@ -139,13 +139,13 @@ X_PRE_LIBS = @X_PRE_LIBS@ lcd4linux_OBJECTS = lcd4linux.$(OBJEXT) cfg.$(OBJEXT) debug.$(OBJEXT) \ drv.$(OBJEXT) evaluator.$(OBJEXT) hash.$(OBJEXT) layout.$(OBJEXT) \ lock.$(OBJEXT) pid.$(OBJEXT) timer.$(OBJEXT) udelay.$(OBJEXT) \ -widget.$(OBJEXT) widget_text.$(OBJEXT) widget_bar.$(OBJEXT) \ -widget_icon.$(OBJEXT) plugin.$(OBJEXT) plugin_math.$(OBJEXT) \ -plugin_string.$(OBJEXT) plugin_cfg.$(OBJEXT) plugin_uname.$(OBJEXT) \ -plugin_loadavg.$(OBJEXT) plugin_proc_stat.$(OBJEXT) \ -plugin_cpuinfo.$(OBJEXT) plugin_meminfo.$(OBJEXT) \ -plugin_netdev.$(OBJEXT) plugin_ppp.$(OBJEXT) plugin_dvb.$(OBJEXT) \ -plugin_i2c_sensors.$(OBJEXT) plugin_imon.$(OBJEXT) \ +qprintf.$(OBJEXT) widget.$(OBJEXT) widget_text.$(OBJEXT) \ +widget_bar.$(OBJEXT) widget_icon.$(OBJEXT) plugin.$(OBJEXT) \ +plugin_math.$(OBJEXT) plugin_string.$(OBJEXT) plugin_cfg.$(OBJEXT) \ +plugin_uname.$(OBJEXT) plugin_loadavg.$(OBJEXT) \ +plugin_proc_stat.$(OBJEXT) plugin_cpuinfo.$(OBJEXT) \ +plugin_meminfo.$(OBJEXT) plugin_netdev.$(OBJEXT) plugin_ppp.$(OBJEXT) \ +plugin_dvb.$(OBJEXT) plugin_i2c_sensors.$(OBJEXT) plugin_imon.$(OBJEXT) \ plugin_xmms.$(OBJEXT) CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) @@ -175,8 +175,8 @@ DEP_FILES = .deps/BeckmannEgle.P .deps/MilfordInstruments.P \ .deps/plugin_loadavg.P .deps/plugin_math.P .deps/plugin_meminfo.P \ .deps/plugin_netdev.P .deps/plugin_ppp.P .deps/plugin_proc_stat.P \ .deps/plugin_string.P .deps/plugin_uname.P .deps/plugin_xmms.P \ -.deps/timer.P .deps/udelay.P .deps/widget.P .deps/widget_bar.P \ -.deps/widget_icon.P .deps/widget_text.P +.deps/qprintf.P .deps/timer.P .deps/udelay.P .deps/widget.P \ +.deps/widget_bar.P .deps/widget_icon.P .deps/widget_text.P SOURCES = $(lcd4linux_SOURCES) $(EXTRA_lcd4linux_SOURCES) OBJECTS = $(lcd4linux_OBJECTS) diff --git a/lcd4linux.c b/lcd4linux.c index 9c192fa..764f67e 100644 --- a/lcd4linux.c +++ b/lcd4linux.c @@ -1,4 +1,4 @@ -/* $Id: lcd4linux.c,v 1.63 2004/02/10 07:42:35 reinelt Exp $ +/* $Id: lcd4linux.c,v 1.64 2004/02/27 07:06:25 reinelt Exp $ * * LCD4Linux * @@ -22,6 +22,9 @@ * * * $Log: lcd4linux.c,v $ + * Revision 1.64 2004/02/27 07:06:25 reinelt + * new function 'qprintf()' (simple but quick snprintf() replacement) + * * Revision 1.63 2004/02/10 07:42:35 reinelt * cut off all old-style files which are no longer used with NextGeneration * @@ -318,6 +321,8 @@ #include "layout.h" #include "plugin.h" +#include "qprintf.h" + #ifdef WITH_DMALLOC #include <dmalloc.h> #endif @@ -396,6 +401,7 @@ int main (int argc, char *argv[]) int quiet=0; int interactive=0; + // save arguments for restart my_argv=malloc(sizeof(char*)*(argc+1)); for (c=0; c<argc; c++) { diff --git a/qprintf.c b/qprintf.c new file mode 100644 index 0000000..2a51782 --- /dev/null +++ b/qprintf.c @@ -0,0 +1,143 @@ +/* $Id: qprintf.c,v 1.1 2004/02/27 07:06:26 reinelt Exp $ + * + * simple but quick snprintf() replacement + * + * Copyright 2004 Michael Reinelt <reinelt@eunet.at> + * Copyright 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> + * + * derived from a patch from Martin Hejl which is + * Copyright 2003 Martin Hejl (martin@hejl.de) + * + * This file is part of LCD4Linux. + * + * LCD4Linux is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * LCD4Linux is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * $Log: qprintf.c,v $ + * Revision 1.1 2004/02/27 07:06:26 reinelt + * new function 'qprintf()' (simple but quick snprintf() replacement) + * + */ + +/* + * exported functions: + * + * int qprintf(char *str, size_t size, const char *format, ...) + * works like snprintf(), but format only knows about %d and %s + */ + + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <string.h> + +static char *itoa(char* buffer, size_t size, int value) +{ + char *p; + int sign; + + // sanity checks + if (buffer==NULL || size<2) return (NULL); + + // remember sign of value + sign = 0; + if (value < 0) { + sign = 1; + value = -value; + } + + // p points to last char + p = buffer+size-1; + + // set terminating zero + *p='\0'; + + do { + *--p = value%10 + '0'; + value = value/10; + } while (value!=0 && p>buffer); + + if (sign && p>buffer) + *--p = '-'; + + return p; +} + + +int qprintf(char *str, size_t size, const char *format, ...) { + + va_list ap; + const char *src; + char *dst; + int len; + + src = format; + dst = str; + len = 0; + + // leave room for terminating zero + size--; + + va_start(ap, format); + + while (len < size) { + + if (*src=='%') { + char buf[12], *s; + int d; + switch (*++src) { + case 's': + src++; + s = va_arg(ap, char *); + while (len < size && *s != '\0') { + len++; + *dst++ = *s++; + } + break; + case 'd': + src++; + d = va_arg(ap, int); + s = itoa (buf, sizeof(buf), d); + while (len < size && *s != '\0') { + len++; + *dst++ = *s++; + } + break; + default: + len++; + *dst++ = '%'; + } + } else { + len++; + *dst++ = *src; + if (*src++ == '\0') break; + } + } + + va_end(ap); + + // enforce terminating zero + if (len>=size && *(dst-1)!='\0') { + len++; + *dst='\0'; + } + + // do not count terminating zero + return len-1; +} + diff --git a/qprintf.h b/qprintf.h new file mode 100644 index 0000000..72c0c1b --- /dev/null +++ b/qprintf.h @@ -0,0 +1,39 @@ +/* $Id: qprintf.h,v 1.1 2004/02/27 07:06:26 reinelt Exp $ + * + * simple but quick snprintf() replacement + * + * Copyright 2004 Michael Reinelt <reinelt@eunet.at> + * Copyright 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> + * + * derived from a patch from Martin Hejl which is + * Copyright 2003 Martin Hejl (martin@hejl.de) + * + * This file is part of LCD4Linux. + * + * LCD4Linux is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * LCD4Linux is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * $Log: qprintf.h,v $ + * Revision 1.1 2004/02/27 07:06:26 reinelt + * new function 'qprintf()' (simple but quick snprintf() replacement) + * + */ + +#ifndef _QPRINTF_H_ +#define _QPRINTF_H_ + +int qprintf(char *str, size_t size, const char *format, ...); + +#endif |