-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini.c
150 lines (134 loc) · 4.2 KB
/
ini.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* PROJECT: ReactOS FreeLoader installer for Linux
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: ini.c file
* COPYRIGHT: Copyright 2021 Arnav Bhatt ([email protected])
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bool.h"
#include "ini.h"
bool CreateINI(char *pacPath, char *pacTopic, char *pacItem, char *pacValue)
{
int iFoundTopic;
int iFoundItem;
int iError;
int iItemLength;
int iValueLength;
char acTopicHeading[80];
char acItemHeading[80];
char acIniLine[160];
char acIniPath[160];
char acTempPath[160];
FILE *pFIniFile;
FILE *pFTempIni;
iError = true;
strcpy(acIniPath, pacPath);
strcpy(acTempPath, pacPath);
strcat(acTempPath, "temp");
// add brackets to topic
strcpy(acTopicHeading, "[");
strcat(acTopicHeading, pacTopic);
strcat(acTopicHeading, "]\n");
strcpy(acItemHeading, pacItem);
strcat(acItemHeading, "=");
iItemLength = strlen(acItemHeading);
iValueLength = strlen(pacValue);
iFoundTopic = 0;
iFoundItem = 0;
if ((pFTempIni = fopen (acTempPath, "w")) == NULL)
{
printf ("Could not open temp ini file to write settings\n");
iError = false;
return(iError);
}
// try to open current config file for reading
if ((pFIniFile = fopen (acIniPath, "r")) != NULL)
{
// read a line from the config file until EOF
while (fgets(acIniLine, 159, pFIniFile) != NULL)
{
// the item has been found so continue reading file to end
if (iFoundItem == 1)
{
fputs (acIniLine, pFTempIni);
continue;
}
// topic has not been found yet
if (iFoundTopic == 0)
{
if (strcmp(acTopicHeading, acIniLine) == 0)
{
// found the topic
iFoundTopic = 1;
}
fputs (acIniLine, pFTempIni);
continue;
}
// the item has not been found yet
if ((iFoundItem == 0) && (iFoundTopic == 1))
{
if ( strncmp (acItemHeading, acIniLine, iItemLength) == 0)
{
// found the item
iFoundItem = 1;
if (iValueLength > 0)
{
fputs (acItemHeading, pFTempIni);
fputs (pacValue, pFTempIni);
fputs ("\n", pFTempIni);
}
continue;
}
// if newline or [, the end of the topic has been reached
// so add the item to the topic
if ((acIniLine[0] == '\n') || (acIniLine[0] == '['))
{
iFoundItem = 1;
if ( iValueLength > 0)
{
fputs(acItemHeading, pFTempIni);
fputs(pacValue, pFTempIni);
fputs("\n", pFTempIni);
}
fputs("\n", pFTempIni);
if(acIniLine[0] == '[')
{
fputs(acIniLine, pFTempIni);
}
continue;
}
// if the item has not been found, write line to temp file
if(iFoundItem == 0)
{
fputs (acIniLine, pFTempIni);
continue;
}
}
}
fclose(pFIniFile);
}
// still did not find the item after reading the config file
if (iFoundItem == 0)
{
// config file does not exist
// or topic does not exist so write to temp file
if (iValueLength > 0)
{
if (iFoundTopic == 0)
{
fputs(acTopicHeading, pFTempIni);
}
fputs(acItemHeading, pFTempIni);
fputs(pacValue, pFTempIni);
fputs("\n\n", pFTempIni);
}
}
fclose(pFTempIni);
//delete the ini file
remove(acIniPath);
// rename the temp file to ini file
rename(acTempPath, acIniPath);
return(iError);
}