Skip to content

Instantly share code, notes, and snippets.

@wjx
Created April 24, 2015 06:21
Show Gist options
  • Save wjx/16c0368b9c4aa88ca1b9 to your computer and use it in GitHub Desktop.
Save wjx/16c0368b9c4aa88ca1b9 to your computer and use it in GitHub Desktop.
Plymouth parse config file like /lib/plymouth/themes/ubuntu-logo/ubuntu-logo.plymouth
plymouth/src/libply/ply-key-file.c
static bool
ply_key_file_load_groups (ply_key_file_t *key_file)
{
int items_matched;
bool added_group = false;
bool has_comments = false;
do {
char *group_name;
int first_byte;
ply_key_file_group_t *group;
first_byte = fgetc (key_file->fp);
if (first_byte == '#') {
char *line_to_toss;
size_t number_of_bytes;
line_to_toss = NULL;
number_of_bytes = 0;
getline (&line_to_toss, &number_of_bytes,
key_file->fp);
free (line_to_toss);
has_comments = true;
items_matched = 0;
continue;
}
ungetc (first_byte, key_file->fp);
group_name = NULL;
items_matched = fscanf (key_file->fp, " [ %m[^]] ] ", &group_name);
if (items_matched <= 0) {
ply_trace ("key file has no %sgroups",
added_group ? "more " : "");
break;
}
assert (group_name != NULL);
group = ply_key_file_load_group (key_file, group_name);
free (group_name);
if (group == NULL)
break;
ply_hashtable_insert (key_file->groups, group->name, group);
added_group = true;
} while (items_matched != EOF);
if (!added_group && has_comments)
ply_trace ("key file has comments but no groups");
return added_group;
}
static ply_key_file_group_t *
ply_key_file_load_group (ply_key_file_t *key_file,
const char *group_name)
{
int items_matched;
ply_key_file_group_t *group;
group = calloc (1, sizeof(ply_key_file_group_t));
group->name = strdup (group_name);
group->entries = ply_hashtable_new (ply_hashtable_string_hash, ply_hashtable_string_compare);
ply_trace ("trying to load group %s", group_name);
do {
ply_key_file_entry_t *entry;
char *key;
char *value;
off_t offset;
int first_byte;
key = NULL;
value = NULL;
do {
first_byte = fgetc (key_file->fp);
} while (isspace (first_byte));
if (first_byte == '#') {
char *line_to_toss;
size_t number_of_bytes;
line_to_toss = NULL;
number_of_bytes = 0;
getline (&line_to_toss, &number_of_bytes,
key_file->fp);
free (line_to_toss);
items_matched = 0;
continue;
}
ungetc (first_byte, key_file->fp);
offset = ftello (key_file->fp);
items_matched = fscanf (key_file->fp, " %m[^= \t\n] = %m[^\n] ", &key, &value);
if (items_matched != 2) {
if (items_matched == 1)
fseeko (key_file->fp, offset, SEEK_SET);
free (key);
free (value);
break;
}
entry = calloc (1, sizeof(ply_key_file_entry_t));
entry->key = key;
entry->value = value;
ply_hashtable_insert (group->entries, entry->key, entry);
} while (items_matched != EOF);
return group;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment