3
* Copyright (C) 2015 Christian Hergert <christian@hergert.me>
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include <editorconfig/editorconfig.h>
21
#include "editorconfig-glib.h"
24
_g_value_free (gpointer data)
28
g_value_unset (value);
33
editorconfig_glib_read (GFile *file,
34
GCancellable *cancellable,
37
editorconfig_handle handle = { 0 };
38
GHashTable *ret = NULL;
39
gchar *filename = NULL;
44
filename = g_file_get_path (file);
49
* This sucks, but we need to basically rewrite editorconfig library
50
* to support this. Not out of the question, but it is for today.
54
G_IO_ERROR_NOT_SUPPORTED,
55
"only local files are currently supported");
59
handle = editorconfig_handle_init ();
60
code = editorconfig_parse (filename, handle);
67
case EDITORCONFIG_PARSE_NOT_FULL_PATH:
68
case EDITORCONFIG_PARSE_MEMORY_ERROR:
69
case EDITORCONFIG_PARSE_VERSION_TOO_NEW:
74
"Failed to parse editorconfig.");
78
count = editorconfig_handle_get_name_value_count (handle);
80
ret = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _g_value_free);
82
for (i = 0; i < count; i++)
85
const gchar *key = NULL;
86
const gchar *valuestr = NULL;
88
value = g_new0 (GValue, 1);
90
editorconfig_handle_get_name_value (handle, i, &key, &valuestr);
92
if ((g_strcmp0 (key, "tab_width") == 0) ||
93
(g_strcmp0 (key, "max_line_length") == 0) ||
94
(g_strcmp0 (key, "indent_size") == 0))
96
g_value_init (value, G_TYPE_INT);
97
g_value_set_int (value, g_ascii_strtoll (valuestr, NULL, 10));
99
else if ((g_strcmp0 (key, "insert_final_newline") == 0) ||
100
(g_strcmp0 (key, "trim_trailing_whitespace") == 0))
102
g_value_init (value, G_TYPE_BOOLEAN);
103
g_value_set_boolean (value, g_str_equal (valuestr, "true"));
107
g_value_init (value, G_TYPE_STRING);
108
g_value_set_string (value, valuestr);
111
g_hash_table_replace (ret, g_strdup (key), value);
115
editorconfig_handle_destroy (handle);