UNIXworkcode

1 /* 2 * Copyright 2021 Olaf Wintermann 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 * DEALINGS IN THE SOFTWARE. 21 */ 22 23 24 #ifndef EDITORCONFIG_H 25 #define EDITORCONFIG_H 26 27 #include <stdlib.h> 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 typedef struct EditorConfig EditorConfig; 34 35 typedef struct ECSection ECSection; 36 typedef struct ECKeyValue ECKeyValue; 37 38 enum ECIndentStyle { EC_INDENT_STYLE_UNSET = 0, EC_TAB, EC_SPACE }; 39 enum ECEndOfLine { EC_EOL_UNSET = 0, EC_LF, EC_CR, EC_CRLF }; 40 enum ECBOM { EC_BOM_UNSET = 0, EC_BOM }; 41 42 struct EditorConfig { 43 int found; /* 1: config found for the requested path; 0: no config found */ 44 enum ECIndentStyle indent_style; 45 int indent_size; 46 int tab_width; 47 enum ECEndOfLine end_of_line; 48 char *charset; 49 enum ECBOM bom; 50 /* trim_trailing_whitespace currently unsupported */ 51 /* insert_final_newline currently unsupported */ 52 }; 53 54 struct ECSection { 55 char *name; 56 57 ECKeyValue *values; 58 59 ECSection *next; 60 }; 61 62 struct ECKeyValue { 63 char *name; 64 char *value; 65 ECKeyValue *next; 66 }; 67 68 typedef struct { 69 char *parent; 70 71 char *content; 72 size_t length; 73 74 ECSection *preamble; 75 ECSection *sections; 76 } ECFile; 77 78 EditorConfig EditorConfigGet(const char *path, const char *name); 79 80 ECFile* ECLoadContent(const char *path); 81 int ECParse(ECFile *ec); 82 83 int ECGetConfig(ECFile *ecf, const char *filepath, EditorConfig *config); 84 85 void ECDestroy(ECFile *ecf); 86 87 88 #ifdef __cplusplus 89 } 90 #endif 91 92 #endif /* EDITORCONFIG_H */ 93 94