1 /*
2  * Copyright 2011-2012 EditorConfig Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "editorconfig_handle.h"
28 
29 /*
30  * See header file
31  */
32 EDITORCONFIG_EXPORT
editorconfig_handle_init(void)33 editorconfig_handle editorconfig_handle_init(void)
34 {
35     editorconfig_handle     h;
36 
37     h = (editorconfig_handle)malloc(sizeof(struct editorconfig_handle));
38 
39     if (!h)
40         return (editorconfig_handle)NULL;
41 
42     memset(h, 0, sizeof(struct editorconfig_handle));
43 
44     return h;
45 }
46 
47 /*
48  * See header file
49  */
50 EDITORCONFIG_EXPORT
editorconfig_handle_destroy(editorconfig_handle h)51 int editorconfig_handle_destroy(editorconfig_handle h)
52 {
53     int                             i;
54     struct editorconfig_handle*     eh = (struct editorconfig_handle*)h;
55 
56 
57     if (h == NULL)
58         return 0;
59 
60     /* free name_values */
61     for (i = 0; i < eh->name_value_count; ++i) {
62         free(eh->name_values[i].name);
63         free(eh->name_values[i].value);
64     }
65     free(eh->name_values);
66 
67     /* free err_file */
68     if (eh->err_file)
69         free(eh->err_file);
70 
71     /* free eh itself */
72     free(eh);
73 
74     return 0;
75 }
76 
77 /*
78  * See header file
79  */
80 EDITORCONFIG_EXPORT
editorconfig_handle_get_err_file(const editorconfig_handle h)81 const char* editorconfig_handle_get_err_file(const editorconfig_handle h)
82 {
83     return ((const struct editorconfig_handle*)h)->err_file;
84 }
85 
86 /*
87  * See header file
88  */
89 EDITORCONFIG_EXPORT
editorconfig_handle_get_version(const editorconfig_handle h,int * major,int * minor,int * patch)90 void editorconfig_handle_get_version(const editorconfig_handle h, int* major,
91         int* minor, int* patch)
92 {
93     if (major)
94         *major = ((const struct editorconfig_handle*)h)->ver.major;
95     if (minor)
96         *minor = ((const struct editorconfig_handle*)h)->ver.minor;
97     if (patch)
98         *patch = ((const struct editorconfig_handle*)h)->ver.patch;
99 }
100 
101 /*
102  * See header file
103  */
104 EDITORCONFIG_EXPORT
editorconfig_handle_set_version(editorconfig_handle h,int major,int minor,int patch)105 void editorconfig_handle_set_version(editorconfig_handle h, int major,
106         int minor, int patch)
107 {
108     if (major >= 0)
109         ((struct editorconfig_handle*)h)->ver.major = major;
110 
111     if (minor >= 0)
112         ((struct editorconfig_handle*)h)->ver.minor = minor;
113 
114     if (patch >= 0)
115         ((struct editorconfig_handle*)h)->ver.patch = patch;
116 }
117 
118 /*
119  * See header file
120  */
121 EDITORCONFIG_EXPORT
editorconfig_handle_set_conf_file_name(editorconfig_handle h,const char * conf_file_name)122 void editorconfig_handle_set_conf_file_name(editorconfig_handle h,
123         const char* conf_file_name)
124 {
125     ((struct editorconfig_handle*)h)->conf_file_name = conf_file_name;
126 }
127 
128 /*
129  * See header file
130  */
131 EDITORCONFIG_EXPORT
editorconfig_handle_get_conf_file_name(const editorconfig_handle h)132 const char* editorconfig_handle_get_conf_file_name(const editorconfig_handle h)
133 {
134     return ((const struct editorconfig_handle*)h)->conf_file_name;
135 }
136 
137 EDITORCONFIG_EXPORT
editorconfig_handle_get_name_value(const editorconfig_handle h,int n,const char ** name,const char ** value)138 void editorconfig_handle_get_name_value(const editorconfig_handle h, int n,
139         const char** name, const char** value)
140 {
141     struct editorconfig_name_value* name_value = &((
142                 const struct editorconfig_handle*)h)->name_values[n];
143 
144     if (name)
145         *name = name_value->name;
146 
147     if (value)
148         *value = name_value->value;
149 }
150 
151 EDITORCONFIG_EXPORT
editorconfig_handle_get_name_value_count(const editorconfig_handle h)152 int editorconfig_handle_get_name_value_count(const editorconfig_handle h)
153 {
154     return ((const struct editorconfig_handle*)h)->name_value_count;
155 }
156