summaryrefslogtreecommitdiff
path: root/linux/profile.cpp
blob: 1f25f5e6cde9a72d1b1d1170344ab374a20f8784 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// Functions to read/write user preferences
// Everything is saved in the file ~/.leocad
//

#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "defines.h"
#include "file.h"

// =============================================================================
// Static functions

static bool read_var (const char *section, const char *key, char *value)
{
  char line[1024], *ptr, filename[LC_MAXPATH];
  FILE *rc;

  sprintf (filename, "%s/.leocad", g_get_home_dir ());
  rc = fopen (filename, "rt");

  if (rc == NULL)
    return false;

  while (fgets (line, 1024, rc) != 0)
  {
    // First we find the section
    if (line[0] != '[')
      continue;

    ptr = strchr (line, ']');
    *ptr = '\0';

    if (strcmp (&line[1], section) == 0)
    {
      while (fgets (line, 1024, rc) != 0)
      {
        ptr = strchr (line, '=');

        if (ptr == NULL)
	{
	  // reached the end of the section
	  fclose (rc);
	  return false;
	}
        *ptr = '\0';

        if (strcmp (line, key) == 0)
        {
          strcpy (value, ptr+1);
	  fclose (rc);

	  while (value[strlen (value)-1] == 10 || 
		 value[strlen (value)-1] == 13 ||
		 value[strlen (value)-1] == 32)
	    value[strlen (value)-1] = 0; 
	  return true;
        }
      }
    }
  }

  fclose (rc);
  return false;
}

static bool save_var (const char *section, const char *key, const char *value)
{
  char line[1024], *ptr, filename[LC_MAXPATH];
  FileMem old_rc;
  bool found;
  FILE *rc;

  sprintf (filename, "%s/.leocad", g_get_home_dir ());
  rc = fopen (filename, "rb");

  if (rc != NULL)
  {
    guint32 len;
    void *buf;

    fseek (rc, 0, SEEK_END);
    len = ftell (rc);
    rewind (rc);
    buf = g_malloc (len);
    fread (buf, len, 1, rc);
    old_rc.Write (buf, len);
    g_free (buf);
    fclose (rc);
    old_rc.Seek (0, SEEK_SET);
  }

  rc = fopen (filename, "wt");
  if (rc == NULL)
    return false;

  // First we need to find the section
  found = false;
  while (old_rc.ReadLine(line, 1024) != NULL)
  {
    fputs (line, rc);

    if (line[0] == '[')
    {
      ptr = strchr (line, ']');
      *ptr = '\0';

      if (strcmp (&line[1], section) == 0)
      {
	found = true;
	break;
      }
    }
  } 

  if (!found)
  {
    fputs ("\n", rc);
    fprintf (rc, "[%s]\n", section);
  }

  found = false;
  while (old_rc.ReadLine(line, 1024) != NULL)
  {
    ptr = strchr (line, '=');

    if (ptr != NULL)
    {
      *ptr = '\0';

      if (strcmp (line, key) == 0)
      {
	fprintf (rc, "%s=%s\n", key, value);
	found = true;
	break;
      }
      else
      {
	*ptr = '=';
	fputs (line, rc);
      }
    }
    else
      break; // reached end of section
  }

  if (!found)
  {
    fprintf (rc, "%s=%s\n", key, value);
    fputs ("\n", rc);
  }

  while (old_rc.ReadLine(line, 1024) != NULL)
    fputs (line, rc);

  fclose (rc);
  return true;
}

// =============================================================================
// Global functions

bool Sys_ProfileSaveInt (const char *section, const char *key, int value)
{
  char buf[16];
  sprintf (buf, "%d", value);
  return save_var (section, key, buf);
}

bool Sys_ProfileSaveString (const char *section, const char *key, const char *value)
{
  return save_var (section, key, value);
}

int Sys_ProfileLoadInt (const char *section, const char *key, int default_value)
{
  char value[1024];

  if (read_var (section, key, value))
    return atoi (value);
  else
    return default_value;
}

char* Sys_ProfileLoadString (const char *section, const char *key, const char *default_value)
{
  static char value[1024];

  if (!read_var (section, key, value))
    strcpy (value, default_value);

  return value;
}