| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include <glib.h> |
|---|
| 7 | #include <stdio.h> |
|---|
| 8 | #include <string.h> |
|---|
| 9 | #include <stdlib.h> |
|---|
| 10 | #include "defines.h" |
|---|
| 11 | #include "file.h" |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | static bool read_var (const char *section, const char *key, char *value) |
|---|
| 17 | { |
|---|
| 18 | char line[1024], *ptr, filename[LC_MAXPATH]; |
|---|
| 19 | FILE *rc; |
|---|
| 20 | |
|---|
| 21 | sprintf (filename, "%s/.leocad", g_get_home_dir ()); |
|---|
| 22 | rc = fopen (filename, "rt"); |
|---|
| 23 | |
|---|
| 24 | if (rc == NULL) |
|---|
| 25 | return false; |
|---|
| 26 | |
|---|
| 27 | while (fgets (line, 1024, rc) != 0) |
|---|
| 28 | { |
|---|
| 29 | |
|---|
| 30 | if (line[0] != '[') |
|---|
| 31 | continue; |
|---|
| 32 | |
|---|
| 33 | ptr = strchr (line, ']'); |
|---|
| 34 | *ptr = '\0'; |
|---|
| 35 | |
|---|
| 36 | if (strcmp (&line[1], section) == 0) |
|---|
| 37 | { |
|---|
| 38 | while (fgets (line, 1024, rc) != 0) |
|---|
| 39 | { |
|---|
| 40 | ptr = strchr (line, '='); |
|---|
| 41 | |
|---|
| 42 | if (ptr == NULL) |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | fclose (rc); |
|---|
| 46 | return false; |
|---|
| 47 | } |
|---|
| 48 | *ptr = '\0'; |
|---|
| 49 | |
|---|
| 50 | if (strcmp (line, key) == 0) |
|---|
| 51 | { |
|---|
| 52 | strcpy (value, ptr+1); |
|---|
| 53 | fclose (rc); |
|---|
| 54 | |
|---|
| 55 | while (value[strlen (value)-1] == 10 || |
|---|
| 56 | value[strlen (value)-1] == 13 || |
|---|
| 57 | value[strlen (value)-1] == 32) |
|---|
| 58 | value[strlen (value)-1] = 0; |
|---|
| 59 | return true; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | fclose (rc); |
|---|
| 66 | return false; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | static bool save_var (const char *section, const char *key, const char *value) |
|---|
| 70 | { |
|---|
| 71 | char line[1024], *ptr, filename[LC_MAXPATH]; |
|---|
| 72 | FileMem old_rc; |
|---|
| 73 | bool found; |
|---|
| 74 | FILE *rc; |
|---|
| 75 | |
|---|
| 76 | sprintf (filename, "%s/.leocad", g_get_home_dir ()); |
|---|
| 77 | rc = fopen (filename, "rb"); |
|---|
| 78 | |
|---|
| 79 | if (rc != NULL) |
|---|
| 80 | { |
|---|
| 81 | guint32 len; |
|---|
| 82 | void *buf; |
|---|
| 83 | |
|---|
| 84 | fseek (rc, 0, SEEK_END); |
|---|
| 85 | len = ftell (rc); |
|---|
| 86 | rewind (rc); |
|---|
| 87 | buf = g_malloc (len); |
|---|
| 88 | fread (buf, len, 1, rc); |
|---|
| 89 | old_rc.Write (buf, len); |
|---|
| 90 | g_free (buf); |
|---|
| 91 | fclose (rc); |
|---|
| 92 | old_rc.Seek (0, SEEK_SET); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | rc = fopen (filename, "wt"); |
|---|
| 96 | if (rc == NULL) |
|---|
| 97 | return false; |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | found = false; |
|---|
| 101 | while (old_rc.ReadLine(line, 1024) != NULL) |
|---|
| 102 | { |
|---|
| 103 | fputs (line, rc); |
|---|
| 104 | |
|---|
| 105 | if (line[0] == '[') |
|---|
| 106 | { |
|---|
| 107 | ptr = strchr (line, ']'); |
|---|
| 108 | *ptr = '\0'; |
|---|
| 109 | |
|---|
| 110 | if (strcmp (&line[1], section) == 0) |
|---|
| 111 | { |
|---|
| 112 | found = true; |
|---|
| 113 | break; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | if (!found) |
|---|
| 119 | { |
|---|
| 120 | fputs ("\n", rc); |
|---|
| 121 | fprintf (rc, "[%s]\n", section); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | found = false; |
|---|
| 125 | while (old_rc.ReadLine(line, 1024) != NULL) |
|---|
| 126 | { |
|---|
| 127 | ptr = strchr (line, '='); |
|---|
| 128 | |
|---|
| 129 | if (ptr != NULL) |
|---|
| 130 | { |
|---|
| 131 | *ptr = '\0'; |
|---|
| 132 | |
|---|
| 133 | if (strcmp (line, key) == 0) |
|---|
| 134 | { |
|---|
| 135 | fprintf (rc, "%s=%s\n", key, value); |
|---|
| 136 | found = true; |
|---|
| 137 | break; |
|---|
| 138 | } |
|---|
| 139 | else |
|---|
| 140 | { |
|---|
| 141 | *ptr = '='; |
|---|
| 142 | fputs (line, rc); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | else |
|---|
| 146 | break; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | if (!found) |
|---|
| 150 | { |
|---|
| 151 | fprintf (rc, "%s=%s\n", key, value); |
|---|
| 152 | fputs ("\n", rc); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | while (old_rc.ReadLine(line, 1024) != NULL) |
|---|
| 156 | fputs (line, rc); |
|---|
| 157 | |
|---|
| 158 | fclose (rc); |
|---|
| 159 | return true; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | bool Sys_ProfileSaveInt (const char *section, const char *key, int value) |
|---|
| 166 | { |
|---|
| 167 | char buf[16]; |
|---|
| 168 | sprintf (buf, "%d", value); |
|---|
| 169 | return save_var (section, key, buf); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | bool Sys_ProfileSaveString (const char *section, const char *key, const char *value) |
|---|
| 173 | { |
|---|
| 174 | return save_var (section, key, value); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | int Sys_ProfileLoadInt (const char *section, const char *key, int default_value) |
|---|
| 178 | { |
|---|
| 179 | char value[1024]; |
|---|
| 180 | |
|---|
| 181 | if (read_var (section, key, value)) |
|---|
| 182 | return atoi (value); |
|---|
| 183 | else |
|---|
| 184 | return default_value; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | char* Sys_ProfileLoadString (const char *section, const char *key, const char *default_value) |
|---|
| 188 | { |
|---|
| 189 | static char value[1024]; |
|---|
| 190 | |
|---|
| 191 | if (!read_var (section, key, value)) |
|---|
| 192 | strcpy (value, default_value); |
|---|
| 193 | |
|---|
| 194 | return value; |
|---|
| 195 | } |
|---|