Mercurial > hg > mlmmj
comparison src/ctrlvalue.c @ 754:ecb991e41a4c
Add $controlN$ substitution
The interfaces to substitute() and substitute_one() have changed, as they
now need to know the listdir to be able to find control files
author | Ben Schmidt |
---|---|
date | Wed, 06 Oct 2010 23:30:26 +1100 |
parents | 7da5c55b9330 |
children | bb803487199c |
comparison
equal
deleted
inserted
replaced
753:b58fd7980358 | 754:ecb991e41a4c |
---|---|
31 #include "ctrlvalue.h" | 31 #include "ctrlvalue.h" |
32 #include "mygetline.h" | 32 #include "mygetline.h" |
33 #include "chomp.h" | 33 #include "chomp.h" |
34 #include "memory.h" | 34 #include "memory.h" |
35 | 35 |
36 char *ctrlvalue(const char *listdir, const char *ctrlstr) | 36 static char *ctrlval(const char *listdir, const char *ctrlstr, int oneline) |
37 { | 37 { |
38 char *filename, *value = NULL; | 38 char *filename, *value = NULL; |
39 int ctrlfd; | 39 int ctrlfd, i; |
40 | 40 |
41 if(listdir == NULL) | 41 if(listdir == NULL) |
42 return NULL; | 42 return NULL; |
43 | 43 |
44 filename = concatstr(3, listdir, "/control/", ctrlstr); | 44 filename = concatstr(3, listdir, "/control/", ctrlstr); |
45 ctrlfd = open(filename, O_RDONLY); | 45 ctrlfd = open(filename, O_RDONLY); |
46 myfree(filename); | 46 myfree(filename); |
47 | 47 |
48 if(ctrlfd < 0) | 48 if(ctrlfd < 0) |
49 return NULL; | 49 return NULL; |
50 | 50 |
51 value = mygetline(ctrlfd); | 51 if (oneline) { |
52 value = mygetline(ctrlfd); | |
53 chomp(value); | |
54 } else { | |
55 value = mygetcontent(ctrlfd); | |
56 i = strlen(value) - 1; | |
57 if (i >= 0 && value[i] == '\n') { | |
58 value[i] = '\0'; | |
59 i--; | |
60 } | |
61 if (i >= 0 && value[i] == '\r') { | |
62 value[i] = '\0'; | |
63 i--; | |
64 } | |
65 } | |
52 close(ctrlfd); | 66 close(ctrlfd); |
53 chomp(value); | |
54 | 67 |
55 return value; | 68 return value; |
56 } | 69 } |
57 | 70 |
58 | 71 char *ctrlvalue(const char *listdir, const char *ctrlstr) |
59 | 72 { |
73 return ctrlval(listdir, ctrlstr, 1); | |
74 } | |
75 | |
76 char *ctrlcontent(const char *listdir, const char *ctrlstr) | |
77 { | |
78 return ctrlval(listdir, ctrlstr, 0); | |
79 } |