1 /* libquvi
2  * Copyright (C) 2012,2013  Toni Gundogdu <legatvs@gmail.com>
3  *
4  * This file is part of libquvi <http://quvi.sourceforge.net/>.
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Affero General Public
8  * License as published by the Free Software Foundation, either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General
17  * Public License along with this library.  If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 /** @file version.c */
22 
23 #include "config.h"
24 
25 #include <string.h>
26 #include <glib.h>
27 
28 #include "quvi.h"
29 /* -- */
30 #include "_quvi_s.h"
31 
kval(GKeyFile * f,const gchar * k,gchar * dst,const gsize dst_size)32 static void kval(GKeyFile *f, const gchar *k, gchar *dst,
33                  const gsize dst_size)
34 {
35   gchar *r = g_key_file_get_string(f, "libquvi-scripts", k, NULL);
36   if (r != NULL)
37     {
38       gchar *s = g_strescape(g_strstrip(r), NULL);
39       g_snprintf(dst, dst_size, "%s", s);
40       g_free(s);
41       g_free(r);
42     }
43 }
44 
45 /** @cond NODOC */
46 struct scripts_version_s
47 {
48   gchar configuration[128];
49   gchar version[32];
50 };
51 
52 typedef struct scripts_version_s *scripts_version_t;
53 /** @endcond */
54 
scripts_version_read(scripts_version_t v)55 static void scripts_version_read(scripts_version_t v)
56 {
57   GKeyFile *f = g_key_file_new();
58   v->configuration[0]= '\0';
59   v->version[0]= '\0';
60   if (g_key_file_load_from_file(f, VERSIONFILE, G_KEY_FILE_NONE, NULL)==TRUE)
61     {
62       kval(f, "configuration", v->configuration, sizeof(v->configuration));
63       kval(f, "version", v->version, sizeof(v->version));
64     }
65   g_key_file_free(f);
66 }
67 
68 static struct scripts_version_s sv;
69 
read_scripts_version(const QuviVersion qv)70 static const gchar *read_scripts_version(const QuviVersion qv)
71 {
72   scripts_version_read(&sv);
73   if (qv == QUVI_VERSION_SCRIPTS_CONFIGURATION)
74     return (sv.configuration);
75   else
76     return (sv.version);
77 }
78 
79 static const gchar *_version[] =
80 {
81 #ifdef VN
82   VN
83 #else
84   PACKAGE_VERSION
85 #endif
86   ,
87   BUILD_OPTS,
88   CC ", " CFLAGS,
89   CANONICAL_TARGET,
90   BUILD_TIME
91 };
92 
93 /** @return NULL-terminated version string
94 @note Do not attempt to free the returned string
95 @ingroup convenience
96 */
quvi_version(QuviVersion version)97 const char *quvi_version(QuviVersion version)
98 {
99   switch (version)
100     {
101     case QUVI_VERSION_SCRIPTS_CONFIGURATION:
102     case QUVI_VERSION_SCRIPTS:
103       return (read_scripts_version(version));
104 
105     case QUVI_VERSION_CONFIGURATION:
106     case QUVI_VERSION_BUILD_CC_CFLAGS:
107     case QUVI_VERSION_BUILD_TARGET:
108     case QUVI_VERSION_BUILD_TIME:
109       return (_version[version]);
110 
111     default:
112       break;
113     }
114   return (_version[QUVI_VERSION]);
115 }
116 
117 /* vim: set ts=2 sw=2 tw=72 expandtab: */
118