~codygarver/+junk/gnome-builder

« back to all changes in this revision

Viewing changes to libide/editorconfig/editorconfig-glib.c

  • Committer: Cody Garver
  • Date: 2015-11-21 00:50:38 UTC
  • Revision ID: cody@elementary.io-20151121005038-8wygis63zt0ljqlz
Import https://github.com/chergert/gnome-builder 06e3158922a02a27f4abca250d70aa7b2970e06a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* editorconfig-glib.c
 
2
 *
 
3
 * Copyright (C) 2015 Christian Hergert <christian@hergert.me>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#include <editorconfig/editorconfig.h>
 
20
 
 
21
#include "editorconfig-glib.h"
 
22
 
 
23
static void
 
24
_g_value_free (gpointer data)
 
25
{
 
26
  GValue *value = data;
 
27
 
 
28
  g_value_unset (value);
 
29
  g_free (value);
 
30
}
 
31
 
 
32
GHashTable *
 
33
editorconfig_glib_read (GFile         *file,
 
34
                        GCancellable  *cancellable,
 
35
                        GError       **error)
 
36
{
 
37
  editorconfig_handle handle = { 0 };
 
38
  GHashTable *ret = NULL;
 
39
  gchar *filename = NULL;
 
40
  gint code;
 
41
  gint count;
 
42
  guint i;
 
43
 
 
44
  filename = g_file_get_path (file);
 
45
 
 
46
  if (!filename)
 
47
    {
 
48
      /*
 
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.
 
51
       */
 
52
      g_set_error (error,
 
53
                   G_IO_ERROR,
 
54
                   G_IO_ERROR_NOT_SUPPORTED,
 
55
                   "only local files are currently supported");
 
56
      return NULL;
 
57
    }
 
58
 
 
59
  handle = editorconfig_handle_init ();
 
60
  code = editorconfig_parse (filename, handle);
 
61
 
 
62
  switch (code)
 
63
    {
 
64
    case 0:
 
65
      break;
 
66
 
 
67
    case EDITORCONFIG_PARSE_NOT_FULL_PATH:
 
68
    case EDITORCONFIG_PARSE_MEMORY_ERROR:
 
69
    case EDITORCONFIG_PARSE_VERSION_TOO_NEW:
 
70
    default:
 
71
      g_set_error (error,
 
72
                   G_IO_ERROR,
 
73
                   G_IO_ERROR_FAILED,
 
74
                   "Failed to parse editorconfig.");
 
75
      goto cleanup;
 
76
    }
 
77
 
 
78
  count = editorconfig_handle_get_name_value_count (handle);
 
79
 
 
80
  ret = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _g_value_free);
 
81
 
 
82
  for (i = 0; i < count; i++)
 
83
    {
 
84
      GValue *value;
 
85
      const gchar *key = NULL;
 
86
      const gchar *valuestr = NULL;
 
87
 
 
88
      value = g_new0 (GValue, 1);
 
89
 
 
90
      editorconfig_handle_get_name_value (handle, i, &key, &valuestr);
 
91
 
 
92
      if ((g_strcmp0 (key, "tab_width") == 0) ||
 
93
          (g_strcmp0 (key, "max_line_length") == 0) ||
 
94
          (g_strcmp0 (key, "indent_size") == 0))
 
95
        {
 
96
          g_value_init (value, G_TYPE_INT);
 
97
          g_value_set_int (value, g_ascii_strtoll (valuestr, NULL, 10));
 
98
        }
 
99
      else if ((g_strcmp0 (key, "insert_final_newline") == 0) ||
 
100
               (g_strcmp0 (key, "trim_trailing_whitespace") == 0))
 
101
        {
 
102
          g_value_init (value, G_TYPE_BOOLEAN);
 
103
          g_value_set_boolean (value, g_str_equal (valuestr, "true"));
 
104
        }
 
105
      else
 
106
        {
 
107
          g_value_init (value, G_TYPE_STRING);
 
108
          g_value_set_string (value, valuestr);
 
109
        }
 
110
 
 
111
      g_hash_table_replace (ret, g_strdup (key), value);
 
112
    }
 
113
 
 
114
cleanup:
 
115
  editorconfig_handle_destroy (handle);
 
116
  g_free (filename);
 
117
 
 
118
  return ret;
 
119
}