Skip to content

Commit

Permalink
Added almost complete support for properties files.
Browse files Browse the repository at this point in the history
  • Loading branch information
wyuka committed Jun 17, 2012
1 parent add9b69 commit 4d8d08d
Show file tree
Hide file tree
Showing 10 changed files with 650 additions and 58 deletions.
361 changes: 361 additions & 0 deletions demos/browser.properties

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions demos/search.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ cmd_showSuggestions=Show Suggestions
cmd_showSuggestions_accesskey=S

cmd_addFoundEngine=Add "%S"

cmd_pasteAndSearch,cmd_clearHistory
6 changes: 6 additions & 0 deletions src/filetype.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ void file_type_read_file (FileType *self, Translatable *tr, gchar *file_name)
FILE_TYPE_GET_CLASS (self)->read_file (self, tr, file_name);
}

void file_type_write_file (FileType *self, Translatable *tr, gchar *file_name)
{
FILE_TYPE_GET_CLASS (self)->write_file (self, tr, file_name);
}

/* This is called when the class is initialized */
void file_type_class_init (gpointer klass, gpointer klass_data)
{
FileTypeClass *this_class = FILE_TYPE_CLASS (klass);

/* pure virtual methods */
this_class->read_file = 0;
this_class->write_file = 0;
}

/* this is the constructor */
Expand Down
2 changes: 2 additions & 0 deletions src/filetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct _FileTypeClass
GObjectClass parent_class;

void (*read_file) (FileType *self, struct _Translatable *tr, gchar *fileName);
void (*write_file) (FileType *self, struct _Translatable *tr, gchar *fileName);
};

struct _FileType
Expand All @@ -50,5 +51,6 @@ void file_type_instance_init (GTypeInstance *instance, gpointer klass);

/* virtual public methods */
void file_type_read_file (FileType *self, struct _Translatable *tr, gchar *file_name);
void file_type_write_file (FileType *self, struct _Translatable *tr, gchar *file_name);

#endif /* __FILE_TYPE_H__ */
14 changes: 13 additions & 1 deletion src/hashvalue.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@

void hash_value_add_localestring (HashValue *self, LocaleString *string)
{
self->list = g_list_append (self->list, string);
GList *current = self->list;
gchar *locale = locale_string_get_locale(string);
while (current != NULL)
{
if (g_strcmp0 (locale_string_get_locale(current->data), locale) == 0)
{
locale_string_destroy(current->data);
self->list = g_list_remove_link(self->list, current);
break;
}
current = current->next;
}
self->list = g_list_prepend (self->list, string);
}

LocaleString* hash_value_find_localestring (HashValue *self, gchar *locale)
Expand Down
229 changes: 181 additions & 48 deletions src/propertiesfiletype.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,32 @@
#include <stdio.h>
#include <string.h>

void process_lines(gchar *contents, Translatable *tr);
void process_line(gchar *line, Translatable *tr);
void process_lines_for_read(gchar *input_contents, Translatable *tr);
gchar* process_lines_for_write(gchar *input_contents, Translatable *tr);

/* Public methods */

void properties_file_type_read_file (PropertiesFileType *self, Translatable *tr, gchar *file_name)
{
gchar *contents = 0;
GError *read_error = 0;
gchar *input_contents = NULL;
GError *read_error = NULL;
gsize length = -1;
g_file_get_contents(file_name, &contents, &length, &read_error);
process_lines (contents, tr);
g_free (contents);
g_file_get_contents(file_name, &input_contents, &length, &read_error);
process_lines_for_read (input_contents, tr);
g_free (input_contents);
}

void properties_file_type_write_file (PropertiesFileType *self, Translatable *tr, gchar *file_name)
{
gchar *input_contents = NULL;
gchar *output_contents = NULL;
GError *read_error = NULL;
gsize length = -1;
g_file_get_contents(file_name, &input_contents, &length, &read_error);
output_contents = process_lines_for_write (input_contents, tr);
g_free (input_contents);
g_printf("%s", output_contents);
g_free(output_contents);
}

/* This is called when the class is initialized */
Expand All @@ -30,6 +43,7 @@ void properties_file_type_class_init (gpointer klass, gpointer klass_data)

/* virtual methods */
parent_class->read_file = (void *)(properties_file_type_read_file);
parent_class->write_file = (void *)(properties_file_type_write_file);
}

/* this is the constructor */
Expand Down Expand Up @@ -66,63 +80,182 @@ GType properties_file_type_get_type (void)

/* Private methods */

void process_lines(gchar *contents, Translatable *tr)
GList* find_elements_for_note(gchar *start, int length)
{
gchar *comma = NULL, *keystr = start;
GList *next = NULL;
int lengthleft = length;
while(comma = g_strstr_len(keystr, lengthleft, ","))
{
*comma = '\0';
next = g_list_prepend(next, g_strdup(keystr));
*comma = ',';
lengthleft -= comma - keystr + 1;
keystr = comma + 1;
if (keystr >= start + length)
return;
}
start[length] = '\0';
next = g_list_prepend(next, g_strdup(keystr));
start[length] = ')';
return next;
}

void process_lines_for_read(gchar *input_contents, Translatable *tr)
{
gchar *end, *cur = contents;
gchar *equalpos, *key, *value, *stripped_line, *note = NULL;
GList *key_list = NULL, *key_list_for_note = NULL, *note_list_for_keys = NULL, *note_list = NULL;
gchar *end, *line = input_contents;
gboolean note_on = FALSE;
int entry_number = -1;
while (1)
{
end = g_strstr_len(cur, -1, "\n");
end = g_strstr_len(line, -1, "\n");
if (end)
{
*end = 0;
//printf("%s\n", cur);
process_line(cur, tr);
*end = '\0';

stripped_line = g_strdup(line);
g_strstrip(stripped_line);
if (*stripped_line == '#')
{
if (g_str_has_prefix(stripped_line, "# LOCALIZATION NOTE (") || g_str_has_prefix(stripped_line, "# LOCALIZATION NOTE ("))
{
if (note_on)
{
GList *notekey = key_list_for_note;
if (notekey != NULL && note != NULL)
{
note_list = g_list_prepend(note_list, note);
}
while (notekey != NULL)
{
note_list_for_keys = g_list_prepend(note_list_for_keys, note);
key_list = g_list_prepend(key_list, notekey->data);
notekey = notekey->next;
}
}

note = NULL;
if (key_list_for_note)
g_list_free(key_list_for_note);
key_list_for_note = NULL;

gchar *keystart = g_strstr_len(stripped_line, -1, "(");
gchar *keyend = g_strstr_len(keystart, -1, ")");

if (keystart && keyend)
{
note_on = TRUE;
key_list_for_note = find_elements_for_note(keystart + 1, keyend - keystart - 1);
gchar *notestart = g_strstr_len(keyend, -1, ":");
if (notestart && notestart[1] != '\0')
{
note = g_strdup(notestart+1);
g_strstrip(note);
}
}
}
else if (note_on == TRUE)
{
gchar *noteline = g_strstrip(g_strdup(stripped_line+1));
if (note)
{
gchar *tmp = note;
note = g_strjoin("\n", note, noteline, NULL);
g_free(tmp);
g_free(noteline);
}
else
{
note = noteline;
}
}
}
else
{
if (note_on == TRUE)
{
GList *notekey = key_list_for_note;
if (notekey != NULL && note != NULL)
{
note_list = g_list_prepend(note_list, note);
}
while (notekey != NULL)
{
note_list_for_keys = g_list_prepend(note_list_for_keys, note);
key_list = g_list_prepend(key_list, notekey->data);
notekey = notekey->next;
}
}
note_on = FALSE;
if (equalpos = g_strstr_len(stripped_line, -1, "="))
{
entry_number++;
key = g_strstrip(g_strndup(stripped_line, equalpos - stripped_line));
value = g_strstrip(g_strdup(equalpos+1));
translatable_add_entry (tr, entry_number, key, NULL, "en", value);
g_free(key);
g_free(value);
}
}
g_free(stripped_line);

*end = '\n';
cur = end+1;
line = end + 1;
}
else
break;
}
GList *note_item = note_list_for_keys, *key_item = key_list;
while (key_item != NULL)
{
translatable_set_note(tr, key_item->data, note_item->data);
key_item = key_item->next;
note_item = note_item->next;
}

g_list_free(note_list_for_keys);
g_list_free_full(note_list, g_free);
g_list_free_full(key_list, g_free);
}

void process_line(gchar *line, Translatable *tr)
gchar* process_lines_for_write(gchar *input_contents, Translatable *tr)
{
gchar *equalpos, *key, *value, *stripped_line, *comment = NULL;
static gboolean note_on = FALSE;
static int entry_index = -1;
stripped_line = g_strdup(line);
if (*stripped_line == '#')
gchar *equalpos, *key, *value, *stripped_line, *output_contents = g_strdup(""), *tmp;
gchar *end, *line = input_contents;

while (1)
{
if (g_str_has_prefix(stripped_line, "# LOCALIZATION NOTE ("))
{
note_on = TRUE;
gchar *note = stripped_line + strlen("# LOCALIZATION NOTE (");
//GList *list = NULL;
//find_elements_for_note(note, list);
//g_list_free_full(list, g_free);
}
else if (note_on == TRUE)
end = g_strstr_len(line, -1, "\n");
if (end)
{
gchar *tmp = comment;
comment = g_strjoin(NULL, comment, stripped_line, NULL);
if (tmp != NULL)
*end = '\0';

stripped_line = g_strdup(line);
g_strstrip(stripped_line);
if (*stripped_line != '#' && (equalpos = g_strstr_len(stripped_line, -1, "=")))
{
key = g_strstrip(g_strndup(stripped_line, equalpos - stripped_line));
value = translatable_get_string_for_uik(tr, key, "en");
tmp = output_contents;
output_contents = g_strjoin("", output_contents, key, "=", value, "\n", NULL);
g_free(tmp);
g_free(key);
}
else
{
tmp = output_contents;
output_contents = g_strjoin("", output_contents, line, "\n", NULL);
g_free(tmp);
}
g_free(stripped_line);

*end = '\n';
line = end + 1;
}
else
break;
}
else
{
note_on = FALSE;
if (equalpos = g_strstr_len(stripped_line, -1, "="))
{
entry_index++;
*equalpos = 0;
key = g_strdup(stripped_line);
*equalpos = '=';
value = g_strdup(equalpos+1);
//g_printf("--- key = %s, value = %s\n", key, value);
translatable_add_entry (tr, entry_index, key, NULL, "en", value);
g_free(value);
}
}
g_free(stripped_line);
return output_contents;
}
1 change: 1 addition & 0 deletions src/propertiesfiletype.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ void properties_file_type_instance_init (GTypeInstance *instance, gpointer klass

/* virtual public methods */
void properties_file_type_read_file (PropertiesFileType *self, Translatable *tr, gchar *file_name);
void properties_file_type_write_file (PropertiesFileType *self, Translatable *tr, gchar *file_name);

#endif /* __PROPERTIES_FILE_TYPE_H__ */
7 changes: 3 additions & 4 deletions src/test2.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ int main(int argc, char* argv[])
{
Translatable *tr = NULL;
PropertiesFileType *ty = NULL;
int k;

if (argc < 2)
return 0;
Expand All @@ -22,11 +23,9 @@ int main(int argc, char* argv[])

translatable_init (tr, FILE_TYPE(ty));
translatable_read_file (tr, argv[1]);

g_printf("%s\n", translatable_get_string_for_uik (tr, "cmd_clearHistory", "en"));
g_printf("%d\n", translatable_get_entry_index_for_uik (tr, "cmd_clearHistory"));
g_printf("%s\n", translatable_get_string_for_entry_index (tr, 1, "en"));

translatable_set_string_for_uik(tr, "contextMenuSearchText", "en", "haha");
translatable_write_file (tr, argv[1]);
translatable_destroy (tr);

return 0;
Expand Down
Loading

0 comments on commit 4d8d08d

Please sign in to comment.