1 /* themerc.c - functions that deals with theme rc file
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  */
8 /* please do NOT translate error messages... =/ */
9 #include <glib.h>
10 
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 
20 /* read contents of fname into gchar * */
trc_open(gchar * fname)21 gchar **trc_open(gchar *fname) {
22 	gint fd;
23         struct stat fds;
24         ssize_t rb;
25         gchar *rc,**rcs;
26 
27         if((fd=open(fname, O_RDONLY))==-1) {
28                 fprintf(stderr, "%s: open() %s failed: %s\n", __FUNCTION__, fname, strerror(errno));
29                 return NULL;
30         }
31         if((fstat(fd, &fds))==-1) {
32                 close(fd);
33                 fprintf(stderr, "%s: fstat() failed: %s\n", __FUNCTION__, strerror(errno));
34                 return NULL;
35         }
36         if(!(fds.st_size)) {
37                 close(fd);
38                 fprintf(stderr, "%s: zero length file.\n", __FUNCTION__);
39                 return NULL;
40         }
41         if(!(rc=malloc(fds.st_size+1))) {
42                 close(fd);
43                 fprintf(stderr, "%s: malloc() failed: cannot alloc %d bytes\n", __FUNCTION__, (int)fds.st_size);
44                 return NULL;
45         }
46         if((rb=read(fd, rc, fds.st_size))!=fds.st_size) {
47                 free(rc);
48                 close(fd);
49                 if(rb==-1) {
50                 	fprintf(stderr, "%s: read() failed: %s\n", __FUNCTION__, strerror(errno));
51                 } else {
52                 	fprintf(stderr, "%s: read() reads less bytes than expected =/\n", __FUNCTION__);
53                 }
54                 return NULL;
55         }
56         rc[fds.st_size]=0;
57         close(fd);
58         rcs=g_strsplit(rc, "\n", 0);
59         free(rc);
60         return rcs;
61 }
62 
63 /* free rc... */
trc_close(gchar ** rcs)64 void trc_close(gchar **rcs) {
65         if(rcs) {
66                 g_strfreev(rcs);
67         }
68 }
69 
70 /* return string value for given parameter. if not found retun NULL */
trc_get_str(gchar ** rcs,gchar * param)71 gchar *trc_get_str(gchar **rcs, gchar *param) {
72         gint i;
73         gchar **strval,*val;
74 
75         if(!rcs) {
76         	fprintf(stderr, "%s called with uninitialised rcs. strange. \n", __FUNCTION__);
77 		return NULL;
78         }
79         if(!param) {
80         	fprintf(stderr, "%s called with NULL param. strange. \n", __FUNCTION__);
81 		return NULL;
82         }
83         for(i=0;rcs[i];i++) {
84                 if(rcs[i][0] && rcs[i][0]!='#' && (strval=g_strsplit(rcs[i],"=",2)) && strval[0] && strval[1]) {
85                         if(!(strcmp(g_strstrip(strval[0]),param))) {
86                                 val=g_strdup(g_strstrip(strval[1]));
87                                 g_strfreev(strval);
88                                 return val;
89                         }
90                         g_strfreev(strval);
91                 }
92         }
93         return NULL;
94 }
95 
96 /* return unsigned integer value for given parameter. if not found retun -1 */
trc_get_uint(gchar ** rcs,gchar * param)97 gint trc_get_uint(gchar **rcs, gchar *param) {
98         gchar *val;
99         gint ret;
100 
101         if(!(val=trc_get_str(rcs, param))) return -1;
102         ret=atoi(val);
103         g_free(val);
104         return ret;
105 }
106