Provide (experimental) clang-format file
[TortoiseGit.git] / src / TortoiseMerge / EditorConfigWrapper.cpp
blob82760c711f1647489611e7f47ef718ec24b7c8e8
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2014 - TortoiseGit
4 // Copyright (C) 2014 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "stdafx.h"
22 #include "EditorConfigWrapper.h"
23 #include "UnicodeUtils.h"
24 #include "editorconfig/editorconfig.h"
26 bool CEditorConfigWrapper::Load(CString filename)
28 if (filename.IsEmpty())
29 return false;
30 CStringA filenameA = CUnicodeUtils::GetUTF8(filename);
31 editorconfig_handle eh = editorconfig_handle_init();
32 if (editorconfig_parse(filenameA, eh))
34 editorconfig_handle_destroy(eh);
35 return false;
38 int count = editorconfig_handle_get_name_value_count(eh);
39 if (count == 0)
41 editorconfig_handle_destroy(eh);
42 return false;
44 for (int i = 0; i < count; ++i)
46 const char* name, *value;
47 editorconfig_handle_get_name_value(eh, i, &name, &value);
48 if (!strcmp(name, "indent_style"))
50 if (!strcmp(value, "space"))
51 m_bIndentStyle = true;
52 else if (!strcmp(value, "tab"))
53 m_bIndentStyle = false;
54 else
55 m_bIndentStyle = nullptr;
57 else if (!strcmp(name, "indent_size"))
58 m_nIndentSize = atoi(value);
59 else if (!strcmp(name, "tab_width"))
60 m_nTabWidth = atoi(value);
61 else if (!strcmp(name, "end_of_line"))
63 if (!strcmp(value, "lf"))
64 m_EndOfLine = EOL::EOL_LF;
65 else if (!strcmp(value, "cr"))
66 m_EndOfLine = EOL::EOL_CR;
67 else if (!strcmp(value, "crlf"))
68 m_EndOfLine = EOL::EOL_CRLF;
69 else
70 m_EndOfLine = nullptr;
72 else if (!strcmp(name, "charset"))
74 if (!strcmp(value, "utf-8"))
75 m_Charset = CFileTextLines::UnicodeType::UTF8;
76 else if (!strcmp(value, "utf-8-bom"))
77 m_Charset = CFileTextLines::UnicodeType::UTF8BOM;
78 else if (!strcmp(value, "utf-16be"))
79 m_Charset = CFileTextLines::UnicodeType::UTF16_BE;
80 else if (!strcmp(value, "utf-16le"))
81 m_Charset = CFileTextLines::UnicodeType::UTF16_LE;
82 else
83 m_Charset = nullptr;
85 else if (!strcmp(name, "trim_trailing_whitespace"))
87 if (!strcmp(value, "true"))
88 m_bTrimTrailingWhitespace = true;
89 else if (!strcmp(value, "false"))
90 m_bTrimTrailingWhitespace = false;
91 else
92 m_bTrimTrailingWhitespace = nullptr;
94 else if (!strcmp(name, "insert_final_newline"))
96 if (!strcmp(value, "true"))
97 m_bInsertFinalNewline = true;
98 else if (!strcmp(value, "false"))
99 m_bInsertFinalNewline = false;
100 else
101 m_bInsertFinalNewline = nullptr;
105 editorconfig_handle_destroy(eh);
106 return true;