bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

check_version


From: Simon Josefsson
Subject: check_version
Date: Sat, 25 Jun 2005 11:42:26 +0200
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/22.0.50 (gnu/linux)

I'm using this module in all of my GNU packages.  One complication
might be that it depends on VERSION being defined.  Feedback
appreciated.

2005-06-25  Simon Josefsson  <address@hidden>

        * modules/check_version: New file.

2005-06-25  Simon Josefsson  <address@hidden>

        * check_version.h, check_version.c: New file.


Index: lib/check_version.c
===================================================================
RCS file: lib/check_version.c
diff -N lib/check_version.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/check_version.c 25 Jun 2005 09:39:48 -0000
@@ -0,0 +1,109 @@
+/* check_version.h --- Check version string compatibility.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free
+   Software Foundation, Inc.
+
+   This program 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.
+
+   This program 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* This file is based on src/global.c from Werner Koch's Libgcrypt.
+   Modularized for gnulib by Simon Josefsson.  */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <ctype.h>
+#include <string.h>
+
+static const char *
+parse_version_number (const char *s, int *number)
+{
+  int val = 0;
+
+  if (*s == '0' && isdigit (s[1]))
+    return NULL;               /* leading zeros are not allowed */
+
+  for (; isdigit (*s); s++)
+    {
+      val *= 10;
+      val += *s - '0';
+    }
+
+  *number = val;
+
+  return val < 0 ? NULL : s;
+}
+
+
+static const char *
+parse_version_string (const char *s, int *major, int *minor, int *micro)
+{
+  s = parse_version_number (s, major);
+  if (!s || *s != '.')
+    return NULL;
+
+  s++;
+
+  s = parse_version_number (s, minor);
+  if (!s || *s != '.')
+    return NULL;
+
+  s++;
+
+  s = parse_version_number (s, micro);
+  if (!s)
+    return NULL;
+
+  return s;                    /* patchlevel */
+}
+
+/* Check that the the version of the library (i.e., the CPP symbol
+ * VERSION) is at minimum the requested one in REQ_VERSION (typically
+ * found in a header file) and return the version string.  Return NULL
+ * if the condition is not satisfied.  If a NULL is passed to this
+ * function, no check is done, but the version string is simply
+ * returned.
+ */
+extern const char *
+check_version (const char *req_version)
+{
+  const char *ver = VERSION;
+  int my_major, my_minor, my_micro;
+  int rq_major, rq_minor, rq_micro;
+  const char *my_plvl, *rq_plvl;
+
+  if (!req_version)
+    return ver;
+
+  my_plvl = parse_version_string (ver, &my_major, &my_minor, &my_micro);
+  if (!my_plvl)
+    return NULL;               /* very strange our own version is bogus */
+
+  rq_plvl = parse_version_string (req_version, &rq_major, &rq_minor,
+                                 &rq_micro);
+  if (!rq_plvl)
+    return NULL;               /* req version string is invalid */
+
+  if (my_major > rq_major
+      || (my_major == rq_major && my_minor > rq_minor)
+      || (my_major == rq_major && my_minor == rq_minor
+         && my_micro > rq_micro)
+      || (my_major == rq_major && my_minor == rq_minor
+         && my_micro == rq_micro && strcmp (my_plvl, rq_plvl) >= 0))
+    {
+      return ver;
+    }
+
+  return NULL;
+}
Index: lib/check_version.h
===================================================================
RCS file: lib/check_version.h
diff -N lib/check_version.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/check_version.h 25 Jun 2005 09:39:48 -0000
@@ -0,0 +1,26 @@
+/* check_version.h --- Check version string compatibility.
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+   This program 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.
+
+   This program 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Simon Josefsson. */
+
+#ifndef CHECK_VERSION_H
+# define CHECK_VERSION_H
+
+extern const char *
+check_version (const char *req_version);
+
+#endif /* CHECK_VERSION_H */
Index: modules/check_version
===================================================================
RCS file: modules/check_version
diff -N modules/check_version
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/check_version       25 Jun 2005 09:39:48 -0000
@@ -0,0 +1,22 @@
+Description:
+Check version string compatibility.
+
+Files:
+lib/check_version.h
+lib/check_version.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += check_version.h check_version.c
+
+Include:
+"check_version.h"
+
+License:
+LGPL
+
+Maintainer:
+Simon Josefsson




reply via email to

[Prev in Thread] Current Thread [Next in Thread]