Skip to content

Commit aa4221a

Browse files
committed
Move environment-file helpers to environment.c
1 parent a1a3cfa commit aa4221a

File tree

7 files changed

+144
-115
lines changed

7 files changed

+144
-115
lines changed

environment.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#define _POSIX_C_SOURCE 200809L
3+
#include <assert.h>
4+
#include <ctype.h>
5+
#include <dirent.h>
6+
#include <stdint.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
#include <strings.h>
11+
#include <sys/stat.h>
12+
#include <unistd.h>
13+
#include "theme.h"
14+
15+
static void
16+
rtrim(char **s)
17+
{
18+
size_t len = strlen(*s);
19+
if (!len) {
20+
return;
21+
}
22+
char *end = *s + len - 1;
23+
while (end >= *s && isspace(*end)) {
24+
end--;
25+
}
26+
*(end + 1) = '\0';
27+
}
28+
29+
static char *
30+
string_strip(char *s)
31+
{
32+
rtrim(&s);
33+
while (isspace(*s)) {
34+
s++;
35+
}
36+
return s;
37+
}
38+
39+
static char *
40+
get_value(char *line, const char *key)
41+
{
42+
if (!line || !*line || line[0] == '#') {
43+
return NULL;
44+
}
45+
char *p = strchr(line, '=');
46+
if (!p) {
47+
return NULL;
48+
}
49+
*p = '\0';
50+
if (!!strcmp(key, string_strip(line))) {
51+
return NULL;
52+
}
53+
char *value = string_strip(++p);
54+
return value ? value : NULL;
55+
}
56+
57+
void
58+
environment_get(char *buffer, size_t size, const char *key)
59+
{
60+
char filename[4096];
61+
snprintf(filename, sizeof(filename), "%s/%s", getenv("HOME"), ".config/labwc/environment");
62+
63+
char *value = NULL;
64+
char *line = NULL;
65+
size_t len = 0;
66+
FILE *stream = fopen(filename, "r");
67+
if (!stream) {
68+
return;
69+
}
70+
71+
while (getline(&line, &len, stream) != -1) {
72+
char *p = strrchr(line, '\n');
73+
if (p) {
74+
*p = '\0';
75+
}
76+
value = get_value(line, key);
77+
if (value) {
78+
snprintf(buffer, size, "%s", value);
79+
break;
80+
}
81+
}
82+
free(line);
83+
fclose(stream);
84+
}
85+
86+
void
87+
environment_set(const char *key, const char *value)
88+
{
89+
/* set cursor for labwc - should cover 'replace' or 'append' */
90+
char xcur[4096] = {0};
91+
strcpy(xcur, key);
92+
strcat(xcur, "=");
93+
char filename[4096];
94+
char bufname[4096];
95+
char *home = getenv("HOME");
96+
snprintf(filename, sizeof(filename), "%s/%s", home, ".config/labwc/environment");
97+
snprintf(bufname, sizeof(bufname), "%s/%s", home, ".config/labwc/buf");
98+
FILE *fe = fopen(filename, "r");
99+
FILE *fw = fopen(bufname, "a");
100+
if ((fe == NULL) || (fw == NULL)) {
101+
perror("Unable to open file!");
102+
return;
103+
}
104+
char chunk[128];
105+
while (fgets(chunk, sizeof(chunk), fe) != NULL) {
106+
if (strstr(chunk, xcur) != NULL) {
107+
continue;
108+
} else {
109+
fprintf(fw, "%s", chunk);
110+
}
111+
}
112+
fclose(fe);
113+
if (value) {
114+
fprintf(fw, "%s\n", strcat(xcur, value));
115+
}
116+
fclose(fw);
117+
rename(bufname, filename);
118+
}
119+
120+
void
121+
environment_set_num(const char *key, int value)
122+
{
123+
char buffer[255];
124+
snprintf(buffer, 255, "%d", value);
125+
126+
environment_set(key, buffer);
127+
}
128+

environment.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef ENVIRONMENT_H
3+
#define ENVIRONMENT_H
4+
#include <stdio.h>
5+
6+
void environment_get(char *buffer, size_t size, const char *key);
7+
8+
void environment_set(const char *key, const char *value);
9+
void environment_set_num(const char *key, int value);
10+
11+
#endif /* ENVIRONMENT_H */

keyboard-layouts.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
#define _POSIX_C_SOURCE 200809L
3+
#include <stdio.h>
34
#include <stdbool.h>
45
#include <string.h>
56
#include "keyboard-layouts.h"

keyboard-layouts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* SPDX-License-Identifier: GPL-2.0-only */
22
#ifndef KEYBOARD_LAYOUTS_H
33
#define KEYBOARD_LAYOUTS_H
4-
#include <gtk/gtk.h>
4+
#include <glib.h>
55

66
struct layout {
77
char *lang;

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ executable(
1515
files(
1616
'main.c',
1717
'xml.c',
18+
'environment.c',
1819
'theme.c',
1920
'keyboard-layouts.c',
2021
'stack-appearance.c',

stack-lang.c

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,13 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
#define _POSIX_C_SOURCE 200809L
33
#include <ctype.h>
4+
#include "environment.h"
45
#include "keyboard-layouts.h"
56
#include "state.h"
67
#include "stack-lang.h"
78
#include "theme.h"
89
#include "xml.h"
910

10-
static void
11-
rtrim(char **s)
12-
{
13-
size_t len = strlen(*s);
14-
if (!len) {
15-
return;
16-
}
17-
char *end = *s + len - 1;
18-
while (end >= *s && isspace(*end)) {
19-
end--;
20-
}
21-
*(end + 1) = '\0';
22-
}
23-
24-
static char *
25-
string_strip(char *s)
26-
{
27-
rtrim(&s);
28-
while (isspace(*s)) {
29-
s++;
30-
}
31-
return s;
32-
}
33-
34-
static char *
35-
get_value(char *line, const char *key)
36-
{
37-
if (!line || !*line || line[0] == '#') {
38-
return NULL;
39-
}
40-
char *p = strchr(line, '=');
41-
if (!p) {
42-
return NULL;
43-
}
44-
*p = '\0';
45-
if (!!strcmp(key, string_strip(line))) {
46-
return NULL;
47-
}
48-
char *value = string_strip(++p);
49-
return value ? value : NULL;
50-
}
51-
52-
static void
53-
environment_get(char *buffer, size_t size, const char *key)
54-
{
55-
char filename[4096];
56-
snprintf(filename, sizeof(filename), "%s/%s", getenv("HOME"), ".config/labwc/environment");
57-
58-
char *value = NULL;
59-
char *line = NULL;
60-
size_t len = 0;
61-
FILE *stream = fopen(filename, "r");
62-
if (!stream) {
63-
return;
64-
}
65-
66-
while (getline(&line, &len, stream) != -1) {
67-
char *p = strrchr(line, '\n');
68-
if (p) {
69-
*p = '\0';
70-
}
71-
value = get_value(line, key);
72-
if (value) {
73-
snprintf(buffer, size, "%s", value);
74-
break;
75-
}
76-
}
77-
free(line);
78-
fclose(stream);
79-
}
80-
8111
void
8212
stack_lang_init(struct state *state, GtkWidget *stack)
8313
{

update.c

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
#include <assert.h>
3+
#include "environment.h"
34
#include "state.h"
45
#include "update.h"
56
#include "xml.h"
@@ -16,49 +17,6 @@ spawn_sync(char const *command)
1617
}
1718
}
1819

19-
static void
20-
environment_set(const char *key, const char *value)
21-
{
22-
/* set cursor for labwc - should cover 'replace' or 'append' */
23-
char xcur[4096] = {0};
24-
strcpy(xcur, key);
25-
strcat(xcur, "=");
26-
char filename[4096];
27-
char bufname[4096];
28-
char *home = getenv("HOME");
29-
snprintf(filename, sizeof(filename), "%s/%s", home, ".config/labwc/environment");
30-
snprintf(bufname, sizeof(bufname), "%s/%s", home, ".config/labwc/buf");
31-
FILE *fe = fopen(filename, "r");
32-
FILE *fw = fopen(bufname, "a");
33-
if ((fe == NULL) || (fw == NULL)) {
34-
perror("Unable to open file!");
35-
return;
36-
}
37-
char chunk[128];
38-
while (fgets(chunk, sizeof(chunk), fe) != NULL) {
39-
if (strstr(chunk, xcur) != NULL) {
40-
continue;
41-
} else {
42-
fprintf(fw, "%s", chunk);
43-
}
44-
}
45-
fclose(fe);
46-
if (value) {
47-
fprintf(fw, "%s\n", strcat(xcur, value));
48-
}
49-
fclose(fw);
50-
rename(bufname, filename);
51-
}
52-
53-
static void
54-
environment_set_num(const char *key, int value)
55-
{
56-
char buffer[255];
57-
snprintf(buffer, 255, "%d", value);
58-
59-
environment_set(key, buffer);
60-
}
61-
6220
static const char
6321
*first_field(const char *s, char delim)
6422
{

0 commit comments

Comments
 (0)