forked from kerrickstaley/lp_solve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini.c
76 lines (60 loc) · 1.18 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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "lp_lib.h"
#include "ini.h"
FILE *ini_create(char *filename)
{
FILE *fp;
fp = fopen(filename, "w");
return(fp);
}
FILE *ini_open(char *filename)
{
FILE *fp;
fp = fopen(filename, "r");
return(fp);
}
void ini_writecomment(FILE *fp, char *comment)
{
fprintf(fp, "; %s\n", comment);
}
void ini_writeheader(FILE *fp, char *header, int addnewline)
{
if((addnewline) && (ftell(fp) > 0))
fputs("\n", fp);
fprintf(fp, "[%s]\n", header);
}
void ini_writedata(FILE *fp, char *name, char *data)
{
if(name != NULL)
fprintf(fp, "%s=%s\n", name, data);
else
fprintf(fp, "%s\n", data);
}
int ini_readdata(FILE *fp, char *data, int szdata, int withcomment)
{
int l;
char *ptr;
if(fgets(data, szdata, fp) == NULL)
return(0);
if(!withcomment) {
ptr = strchr(data, ';');
if(ptr != NULL)
*ptr = 0;
}
l = (int) strlen(data);
while((l > 0) && (isspace(data[l - 1])))
l--;
data[l] = 0;
if((l >= 2) && (data[0] == '[') && (data[l - 1] == ']')) {
memcpy(data, data + 1, l - 2);
data[l - 2] = 0;
return(1);
}
return(2);
}
void ini_close(FILE *fp)
{
fclose(fp);
}