1
0
Fork 0

(svn r15682) -Codechange: some coding style

release/0.7
rubidium 2009-03-12 11:28:03 +00:00
parent 55ce735c2c
commit 84e5e44ca9
1 changed files with 13 additions and 22 deletions

View File

@ -64,17 +64,14 @@ IniGroup::~IniGroup()
IniItem *IniGroup::GetItem(const char *name, bool create) IniItem *IniGroup::GetItem(const char *name, bool create)
{ {
IniItem *item; for (IniItem *item = this->item; item != NULL; item = item->next) {
size_t len = strlen(name);
for (item = this->item; item != NULL; item = item->next) {
if (strcmp(item->name, name) == 0) return item; if (strcmp(item->name, name) == 0) return item;
} }
if (!create) return NULL; if (!create) return NULL;
/* otherwise make a new one */ /* otherwise make a new one */
return new IniItem(this, name, len); return new IniItem(this, name, strlen(name));
} }
void IniGroup::Clear() void IniGroup::Clear()
@ -97,19 +94,17 @@ IniFile::~IniFile()
IniGroup *IniFile::GetGroup(const char *name, size_t len) IniGroup *IniFile::GetGroup(const char *name, size_t len)
{ {
IniGroup *group;
if (len == 0) len = strlen(name); if (len == 0) len = strlen(name);
/* does it exist already? */ /* does it exist already? */
for (group = this->group; group != NULL; group = group->next) { for (IniGroup *group = this->group; group != NULL; group = group->next) {
if (!memcmp(group->name, name, len) && group->name[len] == 0) { if (!memcmp(group->name, name, len) && group->name[len] == 0) {
return group; return group;
} }
} }
/* otherwise make a new one */ /* otherwise make a new one */
group = new IniGroup(this, name, len); IniGroup *group = new IniGroup(this, name, len);
group->comment = strdup("\n"); group->comment = strdup("\n");
return group; return group;
} }
@ -145,9 +140,8 @@ void IniFile::LoadFromDisk(const char *filename)
{ {
assert(this->last_group == &this->group); assert(this->last_group == &this->group);
char buffer[1024], c, *s, *t, *e; char buffer[1024];
IniGroup *group = NULL; IniGroup *group = NULL;
IniItem *item = NULL;
char *comment = NULL; char *comment = NULL;
uint comment_size = 0; uint comment_size = 0;
@ -173,11 +167,12 @@ void IniFile::LoadFromDisk(const char *filename)
/* for each line in the file */ /* for each line in the file */
while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) { while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) {
char c, *s;
/* trim whitespace from the left side */ /* trim whitespace from the left side */
for (s = buffer; *s == ' ' || *s == '\t'; s++) {} for (s = buffer; *s == ' ' || *s == '\t'; s++) {}
/* trim whitespace from right side. */ /* trim whitespace from right side. */
e = s + strlen(s); char *e = s + strlen(s);
while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--; while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
*e = '\0'; *e = '\0';
@ -185,14 +180,13 @@ void IniFile::LoadFromDisk(const char *filename)
if (*s == '#' || *s == ';' || *s == '\0') { if (*s == '#' || *s == ';' || *s == '\0') {
uint ns = comment_size + (e - s + 1); uint ns = comment_size + (e - s + 1);
uint a = comment_alloc; uint a = comment_alloc;
uint pos;
/* add to comment */ /* add to comment */
if (ns > a) { if (ns > a) {
a = max(a, 128U); a = max(a, 128U);
do a *= 2; while (a < ns); do a *= 2; while (a < ns);
comment = ReallocT(comment, comment_alloc = a); comment = ReallocT(comment, comment_alloc = a);
} }
pos = comment_size; uint pos = comment_size;
comment_size += (e - s + 1); comment_size += (e - s + 1);
comment[pos + e - s] = '\n'; // comment newline comment[pos + e - s] = '\n'; // comment newline
memcpy(comment + pos, s, e - s); // copy comment contents memcpy(comment + pos, s, e - s); // copy comment contents
@ -213,6 +207,7 @@ void IniFile::LoadFromDisk(const char *filename)
comment_size = 0; comment_size = 0;
} }
} else if (group) { } else if (group) {
char *t;
/* find end of keyname */ /* find end of keyname */
if (*s == '\"') { if (*s == '\"') {
s++; s++;
@ -223,7 +218,7 @@ void IniFile::LoadFromDisk(const char *filename)
} }
/* it's an item in an existing group */ /* it's an item in an existing group */
item = new IniItem(group, s, t-s); IniItem *item = new IniItem(group, s, t - s);
if (comment_size) { if (comment_size) {
item->comment = strndup(comment, comment_size); item->comment = strndup(comment, comment_size);
comment_size = 0; comment_size = 0;
@ -258,17 +253,13 @@ void IniFile::LoadFromDisk(const char *filename)
bool IniFile::SaveToDisk(const char *filename) bool IniFile::SaveToDisk(const char *filename)
{ {
FILE *f; FILE *f = fopen(filename, "w");
IniGroup *group;
IniItem *item;
f = fopen(filename, "w");
if (f == NULL) return false; if (f == NULL) return false;
for (group = this->group; group != NULL; group = group->next) { for (const IniGroup *group = this->group; group != NULL; group = group->next) {
if (group->comment) fputs(group->comment, f); if (group->comment) fputs(group->comment, f);
fprintf(f, "[%s]\n", group->name); fprintf(f, "[%s]\n", group->name);
for (item = group->item; item != NULL; item = item->next) { for (const IniItem *item = group->item; item != NULL; item = item->next) {
assert(item->value != NULL); assert(item->value != NULL);
if (item->comment != NULL) fputs(item->comment, f); if (item->comment != NULL) fputs(item->comment, f);