add clang-format and apply to sdcv.cpp

This commit is contained in:
Evgeniy A. Dushistov
2016-06-02 14:52:58 +03:00
parent b20f27d27b
commit aa0c5ff051
2 changed files with 171 additions and 115 deletions

56
.clang-format Normal file
View File

@@ -0,0 +1,56 @@
---
Language: Cpp
AccessModifierOffset: -4
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AlwaysBreakTemplateDeclarations: false
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeBinaryOperators: true
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BinPackParameters: true
ColumnLimit: 0
ConstructorInitializerAllOnOneLineOrOnePerLine: false
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: false
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
MaxEmptyLinesToKeep: 1
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: Inner
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
SpacesBeforeTrailingComments: 1
Cpp11BracedListStyle: false
Standard: Cpp11
IndentWidth: 4
TabWidth: 8
UseTab: Never
BreakBeforeBraces: Linux
SpacesInParentheses: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: true
SpaceBeforeAssignmentOperators: true
ContinuationIndentWidth: 4
CommentPragmas: '^ IWYU pragma:'
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
SpaceBeforeParens: ControlStatements
DisableFormat: false
...

View File

@@ -19,17 +19,17 @@
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" #include "config.h"
#endif #endif
#include <cerrno> #include <cerrno>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <clocale> #include <clocale>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include <glib.h> #include <glib.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
@@ -41,63 +41,65 @@
static const char gVersion[] = VERSION; static const char gVersion[] = VERSION;
namespace { namespace
static void free_str_array(gchar **arr) {
{ static void free_str_array(gchar **arr)
gchar **p; {
gchar **p;
for (p = arr; *p; ++p) for (p = arr; *p; ++p)
g_free(*p); g_free(*p);
g_free(arr); g_free(arr);
}
} }
namespace glib { }
typedef ResourceWrapper<gchar *, gchar *, free_str_array> StrArr; namespace glib
{
typedef ResourceWrapper<gchar *, gchar *, free_str_array> StrArr;
} }
int main(int argc, char *argv[]) try { int main(int argc, char *argv[]) try {
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
#if ENABLE_NLS #if ENABLE_NLS
bindtextdomain("sdcv", bindtextdomain("sdcv",
//"./locale"//< for testing //"./locale"//< for testing
GETTEXT_TRANSLATIONS_PATH//< should be GETTEXT_TRANSLATIONS_PATH //< should be
); );
textdomain("sdcv"); textdomain("sdcv");
#endif #endif
gboolean show_version = FALSE; gboolean show_version = FALSE;
gboolean show_list_dicts = FALSE; gboolean show_list_dicts = FALSE;
glib::StrArr use_dict_list; glib::StrArr use_dict_list;
gboolean non_interactive = FALSE; gboolean non_interactive = FALSE;
gboolean utf8_output = FALSE; gboolean utf8_output = FALSE;
gboolean utf8_input = FALSE; gboolean utf8_input = FALSE;
glib::CharStr opt_data_dir; glib::CharStr opt_data_dir;
gboolean colorize = FALSE; gboolean colorize = FALSE;
const GOptionEntry entries[] = { const GOptionEntry entries[] = {
{"version", 'v', 0, G_OPTION_ARG_NONE, &show_version, { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
_("display version information and exit"), nullptr }, _("display version information and exit"), nullptr },
{"list-dicts", 'l', 0, G_OPTION_ARG_NONE, &show_list_dicts, { "list-dicts", 'l', 0, G_OPTION_ARG_NONE, &show_list_dicts,
_("display list of available dictionaries and exit"), nullptr}, _("display list of available dictionaries and exit"), nullptr },
{"use-dict", 'u', 0, G_OPTION_ARG_STRING_ARRAY, get_addr(use_dict_list), { "use-dict", 'u', 0, G_OPTION_ARG_STRING_ARRAY, get_addr(use_dict_list),
_("for search use only dictionary with this bookname"), _("for search use only dictionary with this bookname"),
_("bookname")}, _("bookname") },
{"non-interactive", 'n', 0, G_OPTION_ARG_NONE, &non_interactive, { "non-interactive", 'n', 0, G_OPTION_ARG_NONE, &non_interactive,
_("for use in scripts"), nullptr}, _("for use in scripts"), nullptr },
{"utf8-output", '0', 0, G_OPTION_ARG_NONE, &utf8_output, { "utf8-output", '0', 0, G_OPTION_ARG_NONE, &utf8_output,
_("output must be in utf8"), nullptr}, _("output must be in utf8"), nullptr },
{"utf8-input", '1', 0, G_OPTION_ARG_NONE, &utf8_input, { "utf8-input", '1', 0, G_OPTION_ARG_NONE, &utf8_input,
_("input of sdcv in utf8"), nullptr}, _("input of sdcv in utf8"), nullptr },
{"data-dir", '2', 0, G_OPTION_ARG_STRING, get_addr(opt_data_dir), { "data-dir", '2', 0, G_OPTION_ARG_STRING, get_addr(opt_data_dir),
_("use this directory as path to stardict data directory"), _("use this directory as path to stardict data directory"),
_("path/to/dir")}, _("path/to/dir") },
{"color", 'c', 0, G_OPTION_ARG_NONE, &colorize, { "color", 'c', 0, G_OPTION_ARG_NONE, &colorize,
_("colorize the output"), nullptr }, _("colorize the output"), nullptr },
{ nullptr }, { nullptr },
}; };
glib::Error error; glib::Error error;
GOptionContext *context = g_option_context_new(_(" words")); GOptionContext *context = g_option_context_new(_(" words"));
g_option_context_set_help_enabled(context, TRUE); g_option_context_set_help_enabled(context, TRUE);
g_option_context_add_main_entries(context, entries, nullptr); g_option_context_add_main_entries(context, entries, nullptr);
const gboolean parse_res = g_option_context_parse(context, &argc, &argv, get_addr(error)); const gboolean parse_res = g_option_context_parse(context, &argc, &argv, get_addr(error));
@@ -108,54 +110,53 @@ int main(int argc, char *argv[]) try {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (show_version) { if (show_version) {
printf(_("Console version of Stardict, version %s\n"), gVersion); printf(_("Console version of Stardict, version %s\n"), gVersion);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
const gchar *stardict_data_dir = g_getenv("STARDICT_DATA_DIR");
std::string data_dir;
if (!opt_data_dir) {
if (stardict_data_dir)
data_dir = stardict_data_dir;
else
data_dir = "/usr/share/stardict/dic";
} else {
data_dir = get_impl(opt_data_dir);
}
const gchar *stardict_data_dir = g_getenv("STARDICT_DATA_DIR"); const char *homedir = g_getenv("HOME");
std::string data_dir; if (!homedir)
if (!opt_data_dir) { homedir = g_get_home_dir();
if (stardict_data_dir)
data_dir = stardict_data_dir;
else
data_dir = "/usr/share/stardict/dic";
} else {
data_dir = get_impl(opt_data_dir);
}
const char *homedir = g_getenv ("HOME"); const std::list<std::string> dicts_dir_list = {
if (!homedir)
homedir = g_get_home_dir ();
const std::list<std::string> dicts_dir_list = {
std::string(homedir) + G_DIR_SEPARATOR + ".stardict" + G_DIR_SEPARATOR + "dic", std::string(homedir) + G_DIR_SEPARATOR + ".stardict" + G_DIR_SEPARATOR + "dic",
data_dir data_dir
}; };
if (show_list_dicts) { if (show_list_dicts) {
printf(_("Dictionary's name Word count\n")); printf(_("Dictionary's name Word count\n"));
std::list<std::string> order_list, disable_list; std::list<std::string> order_list, disable_list;
for_each_file(dicts_dir_list, ".ifo", order_list, for_each_file(dicts_dir_list, ".ifo", order_list,
disable_list, [](const std::string& filename, bool) -> void { disable_list, [](const std::string &filename, bool) -> void {
DictInfo dict_info; DictInfo dict_info;
if (dict_info.load_from_ifo_file(filename, false)) { if (dict_info.load_from_ifo_file(filename, false)) {
const std::string bookname = utf8_to_locale_ign_err(dict_info.bookname); const std::string bookname = utf8_to_locale_ign_err(dict_info.bookname);
printf("%s %d\n", bookname.c_str(), dict_info.wordcount); printf("%s %d\n", bookname.c_str(), dict_info.wordcount);
} }
}); });
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
std::list<std::string> disable_list; std::list<std::string> disable_list;
if (use_dict_list) { if (use_dict_list) {
std::list<std::string> empty_list; std::list<std::string> empty_list;
for_each_file(dicts_dir_list, ".ifo", empty_list, empty_list, for_each_file(dicts_dir_list, ".ifo", empty_list, empty_list,
[&disable_list, &use_dict_list](const std::string& filename, bool) -> void { [&disable_list, &use_dict_list](const std::string &filename, bool) -> void {
DictInfo dict_info; DictInfo dict_info;
const bool load_ok = dict_info.load_from_ifo_file(filename, false); const bool load_ok = dict_info.load_from_ifo_file(filename, false);
if (!load_ok) if (!load_ok)
@@ -166,37 +167,36 @@ int main(int argc, char *argv[]) try {
return; return;
disable_list.push_back(dict_info.ifo_file_name); disable_list.push_back(dict_info.ifo_file_name);
}); });
} }
const std::string conf_dir = std::string(g_get_home_dir()) + G_DIR_SEPARATOR + ".stardict"; const std::string conf_dir = std::string(g_get_home_dir()) + G_DIR_SEPARATOR + ".stardict";
if (g_mkdir(conf_dir.c_str(), S_IRWXU) == -1 && errno!=EEXIST) if (g_mkdir(conf_dir.c_str(), S_IRWXU) == -1 && errno != EEXIST)
fprintf(stderr, _("g_mkdir failed: %s\n"), strerror(errno)); fprintf(stderr, _("g_mkdir failed: %s\n"), strerror(errno));
Library lib(utf8_input, utf8_output, colorize);
std::list<std::string> empty_list;
lib.load(dicts_dir_list, empty_list, disable_list);
Library lib(utf8_input, utf8_output, colorize); std::unique_ptr<IReadLine> io(create_readline_object());
std::list<std::string> empty_list; if (optind < argc) {
lib.load(dicts_dir_list, empty_list, disable_list); for (int i = optind; i < argc; ++i)
if (!lib.process_phrase(argv[i], *io, non_interactive))
std::unique_ptr<IReadLine> io(create_readline_object()); return EXIT_FAILURE;
if (optind < argc) { } else if (!non_interactive) {
for(int i = optind; i < argc; ++i)
if (!lib.process_phrase(argv[i], *io, non_interactive))
return EXIT_FAILURE;
} else if (!non_interactive) {
std::string phrase; std::string phrase;
while (io->read(_("Enter word or phrase: "), phrase)) { while (io->read(_("Enter word or phrase: "), phrase)) {
if (!lib.process_phrase(phrase.c_str(), *io)) if (!lib.process_phrase(phrase.c_str(), *io))
return EXIT_FAILURE; return EXIT_FAILURE;
phrase.clear(); phrase.clear();
} }
putchar('\n'); putchar('\n');
} else } else {
fprintf(stderr, _("There are no words/phrases to translate.\n")); fprintf(stderr, _("There are no words/phrases to translate.\n"));
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} catch (const std::exception& ex) { } catch (const std::exception &ex) {
fprintf(stderr, "Internal error: %s\n", ex.what()); fprintf(stderr, "Internal error: %s\n", ex.what());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }