11// SPDX-License-Identifier: GPL-2.0-only
22#define _POSIX_C_SOURCE 200809L
3+ #include < iostream>
4+ #include < fstream>
35#include < QDebug>
46#include < QFile>
57#include < QString>
68#include < QTextStream>
79#include < cstring>
10+ #include < vector>
11+ #include < memory>
12+ #include " log.h"
813
9- QString environment_get ( const char *key)
14+ class Line
1015{
11- QString filename = qgetenv (" HOME" ) + " /.config/labwc/environment" ;
12- QFile inputFile (filename);
13- inputFile.open (QIODevice::ReadOnly);
14- if (!inputFile.isOpen ()) {
15- return nullptr ;
16- }
16+ public:
17+ Line ();
18+ ~Line ();
1719
18- QTextStream stream (&inputFile);
19- QString line;
20- while (stream.readLineInto (&line)) {
21- if (line.isEmpty () || line.startsWith (" #" )) {
22- continue ;
23- }
20+ QString data;
21+ bool isKeyValuePair;
22+ QString key;
23+ QString value;
24+ };
25+
26+ static std::vector<std::unique_ptr<Line>> lines;
27+
28+ Line::Line (void )
29+ {
30+ isKeyValuePair = false ;
31+ }
32+
33+ Line::~Line (void ) { };
2434
25- QStringList elements = line.split (' =' );
26- if (elements.count () != 2 ) {
35+ // TODO: should be envGet()
36+ QString environment_get (QString key)
37+ {
38+ for (auto &line : lines) {
39+ if (!line->isKeyValuePair )
2740 continue ;
28- }
29- if (elements[0 ].trimmed () == QString (key)) {
30- return elements[1 ].trimmed ();
31- }
41+ if (line->key == key)
42+ return line->value ;
3243 }
3344 return nullptr ;
3445}
3546
47+ int environment_get_int (QString key)
48+ {
49+ qDebug () << " key=" << key;
50+ for (auto &line : lines) {
51+ qDebug () << " line->key=" << line->key << " key=" << key;
52+ if (!line->isKeyValuePair )
53+ continue ;
54+ if (line->key == key)
55+ return atoi (line->value .toStdString ().c_str ());
56+ }
57+ return -1 ;
58+ }
59+
60+ // TODO: use std::string instead of const char *
3661void environment_set (const char *key, const char *value)
3762{
38- if (!key || !*key) {
39- return ;
40- }
41- if (!value || !*value) {
42- return ;
43- }
44- /* set cursor for labwc - should cover 'replace' or 'append' */
45- char xcur[4096 ] = { 0 };
46- strcpy (xcur, key);
47- strcat (xcur, " =" );
48- char filename[4096 ];
49- char bufname[4096 ];
50- char *home = getenv (" HOME" );
51- snprintf (filename, sizeof (filename), " %s/%s" , home, " .config/labwc/environment" );
52- snprintf (bufname, sizeof (bufname), " %s/%s" , home, " .config/labwc/buf" );
53- FILE *fe = fopen (filename, " r" );
54- FILE *fw = fopen (bufname, " a" );
55- if ((fe == NULL ) || (fw == NULL )) {
56- perror (" Unable to open file!" );
63+ if (!key || !*key) {
64+ return ;
65+ }
66+ if (!value || !*value) {
5767 return ;
5868 }
59- char chunk[128 ];
60- while (fgets (chunk, sizeof (chunk), fe) != NULL ) {
61- if (strstr (chunk, xcur) != NULL ) {
69+ for (auto &line : lines) {
70+ if (!line->isKeyValuePair ) {
6271 continue ;
63- } else {
64- fprintf (fw, " %s" , chunk);
72+ }
73+ if (line->key == key) {
74+ // Modify existing key=value pair
75+ line->value = QString (value);
76+ return ;
6577 }
6678 }
67- fclose (fe);
68- if (value) {
69- fprintf (fw, " %s \n " , strcat (xcur, value ));
70- }
71- fclose (fw );
72- rename (bufname, filename );
79+
80+ // Append
81+ lines. push_back (std::make_unique<Line>( ));
82+ lines. back ()-> isKeyValuePair = true ;
83+ lines. back ()-> key = QString (key );
84+ lines. back ()-> value = QString (value );
7385}
7486
7587void environment_set_num (const char *key, int value)
@@ -79,3 +91,44 @@ void environment_set_num(const char *key, int value)
7991
8092 environment_set (key, buffer);
8193}
94+
95+ static void processLine (QString line)
96+ {
97+ lines.push_back (std::make_unique<Line>());
98+ lines.back ()->data = line;
99+ if (line.isEmpty () || line.startsWith (" #" ) || !line.contains (" =" )) {
100+ return ;
101+ }
102+ lines.back ()->isKeyValuePair = true ;
103+ QStringList elements = line.split (' =' );
104+ lines.back ()->key = elements[0 ].trimmed ();
105+ lines.back ()->value = elements[1 ].trimmed ();
106+ }
107+
108+ void environmentInit (std::string filename)
109+ {
110+ if (access (filename.c_str (), F_OK)) {
111+ info (" environment file not found '{}'" , filename);
112+ return ;
113+ }
114+
115+ std::string line;
116+ std::ifstream stream (filename);
117+ while (getline (stream, line)) {
118+ processLine (QString (line.c_str ()));
119+ }
120+ stream.close ();
121+ }
122+
123+ void environmentSave (std::string filename)
124+ {
125+ std::ofstream ofs (filename);
126+ for (auto &line : lines) {
127+ if (!line->isKeyValuePair ) {
128+ ofs << line->data .toStdString () << std::endl;
129+ } else {
130+ ofs << line->key .toStdString () << " =" << line->value .toStdString () << std::endl;
131+ }
132+ }
133+ ofs.close ();
134+ }
0 commit comments