static CharsetTable *get_charset_table(void) { static CharsetTable *ctable = NULL; EncArray *encarray; gint i; if (!ctable) { ctable = g_malloc(sizeof(CharsetTable)); ctable->num = 0; ctable->charset[ctable->num] = get_default_charset(); ctable->str[ctable->num] = g_strdup_printf(_("Current Locale (%s)"), get_default_charset()); ctable->num++; ctable->charset[ctable->num] = "UTF-8"; ctable->str[ctable->num] = ctable->charset[ctable->num]; ctable->num++; encarray = get_encoding_items(get_encoding_code()); for (i = 0; i < ENCODING_MAX_ITEM_NUM; i++) if (encarray->item[i]) { ctable->charset[ctable->num] = encarray->item[i]; ctable->str[ctable->num] = encarray->item[i]; ctable->num++; } } return ctable; }
gchar* codeslayer_utils_get_utf8_text (const gchar *file_path) { gchar *contents; gsize bytes; const gchar *charset; gchar *result; gint lineend; if (!g_file_get_contents (file_path, &contents, &bytes, NULL)) return NULL; lineend = detect_line_ending (contents); if (lineend != LF) convert_line_ending_to_lf (contents); charset = detect_charset (contents); if (charset == NULL) charset = get_default_charset (); if (g_strcmp0 (charset, "UTF-8") == 0) return contents; result = g_convert (contents, -1, "UTF-8", charset, NULL, NULL, NULL); g_free(contents); return result; }
gint main(gint argc, gchar **argv) { Conf *conf; GtkItemFactory *ifactory; gchar *stdin_data = NULL; bindtextdomain(PACKAGE, LOCALEDIR); //bindtextdomain ?? bind_textdomain_codeset(PACKAGE, "UTF-8"); // ?? textdomain(PACKAGE); // ?? pub = g_malloc(sizeof(PublicData)); pub->fi = g_malloc(sizeof(FileInfo)); pub->fi->filename = NULL; pub->fi->charset = NULL; pub->fi->charset_flag = FALSE; pub->fi->lineend = LF; parse_args(argc, argv, pub->fi); gtk_init(&argc, &argv); //call before using any gtk fuction g_set_application_name(PACKAGE_NAME); g_print("%s\n", PACKAGE_NAME); #if !GTK_CHECK_VERSION(2, 6, 0) add_about_stock(); #endif pub->mw = create_main_window(); //Find create_main_window conf = g_malloc(sizeof(Conf)); //sends default vlues to load and overwrites in the function conf->width = 600; conf->height = 400; conf->fontname = g_strdup("Monospace 12"); conf->wordwrap = FALSE; conf->linenumbers = FALSE; conf->autoindent = FALSE; load_config_file(conf); //gtk options for GUI gtk_window_set_default_size( GTK_WINDOW(pub->mw->window), conf->width, conf->height); set_text_font_by_name(pub->mw->view, conf->fontname); ifactory = gtk_item_factory_from_widget(pub->mw->menubar); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM( gtk_item_factory_get_widget(ifactory, "/Options/Word Wrap")), conf->wordwrap); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM( gtk_item_factory_get_widget(ifactory, "/Options/Line Numbers")), conf->linenumbers); indent_refresh_tab_width(pub->mw->view); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM( gtk_item_factory_get_widget(ifactory, "/Options/Auto Indent")), conf->autoindent); gtk_widget_show_all(pub->mw->window); g_free(conf->fontname); g_free(conf); #ifdef ENABLE_EMACS check_emacs_key_theme(GTK_WINDOW(pub->mw->window), ifactory); #endif hlight_init(pub->mw->buffer); undo_init(pub->mw->view, gtk_item_factory_get_widget(ifactory, "/Edit/Undo"), gtk_item_factory_get_widget(ifactory, "/Edit/Redo")); // hlight_init(pub->mw->buffer); dnd_init(pub->mw->view); if (pub->fi->filename) file_open_real(pub->mw->view, pub->fi); #ifdef G_OS_UNIX else stdin_data = gedit_utils_get_stdin(); #endif if (stdin_data) { gchar *str; GtkTextIter iter; str = g_convert(stdin_data, -1, "UTF-8", get_default_charset(), NULL, NULL, NULL); g_free(stdin_data); // gtk_text_buffer_set_text(buffer, "", 0); gtk_text_buffer_get_start_iter(pub->mw->buffer, &iter); gtk_text_buffer_insert(pub->mw->buffer, &iter, str, strlen(str)); gtk_text_buffer_get_start_iter(pub->mw->buffer, &iter); gtk_text_buffer_place_cursor(pub->mw->buffer, &iter); gtk_text_buffer_set_modified(pub->mw->buffer, FALSE); gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(pub->mw->view), &iter, 0, FALSE, 0, 0); g_free(str); } if (jump_linenum) { GtkTextIter iter; gtk_text_buffer_get_iter_at_line(pub->mw->buffer, &iter, jump_linenum - 1); gtk_text_buffer_place_cursor(pub->mw->buffer, &iter); // gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview), &iter, 0.1, FALSE, 0.5, 0.5); scroll_to_cursor(pub->mw->buffer, 0.25); } set_main_window_title(); // hlight_apply_all(pub->mw->buffer); gtk_main(); return 0; }
static void parse_args(gint argc, gchar **argv, FileInfo *fi) { EncArray *encarray; gint i; GError *error = NULL; #if GLIB_CHECK_VERSION(2, 6, 0) GOptionContext *context; gchar *opt_codeset = NULL; gint opt_tab_width = 0; gboolean opt_jump = 0; gboolean opt_version = FALSE; GOptionEntry entries[] = { { "codeset", 0, 0, G_OPTION_ARG_STRING, &opt_codeset, "Set codeset to open file", "CODESET" }, { "tab-width", 0, 0, G_OPTION_ARG_INT, &opt_tab_width, "Set tab width", "WIDTH" }, { "jump", 0, 0, G_OPTION_ARG_INT, &opt_jump, "Jump to specified line", "LINENUM" }, { "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Show version number", NULL }, { NULL } }; //Structure defined in glib for entries and commandline option parser //4th argument is an enum defined in glib context = g_option_context_new("[Filename]"); //Creates new context for option parsing g_option_context_add_main_entries(context, entries, PACKAGE); //PACKAGE == Translation Domain ?? g_option_context_add_group(context, gtk_get_option_group(TRUE)); g_option_context_set_ignore_unknown_options(context, FALSE); //sets error when unknown command g_option_context_parse(context, &argc, &argv, &error); //passes the arguments g_option_context_free(context); //free for gcontext if (error) { g_print("%s: %s\n", PACKAGE, error->message); //things to do for each option g_error_free(error); exit(-1); } if (opt_version) { g_print("%s\n", PACKAGE_STRING); exit(0); } if (opt_codeset) { g_convert("TEST", -1, "UTF-8", opt_codeset, NULL, NULL, &error); if (error) { g_error_free(error); error = NULL; } else { g_free(fi->charset); fi->charset = g_strdup(opt_codeset); } } if (opt_tab_width) indent_set_default_tab_width(opt_tab_width); if (opt_jump) jump_linenum = opt_jump; #else //code for older version of glib gint c; //getopt also parses the command line argument do { c = getopt_long(argc, argv, "", longopts, NULL); switch (c) { case 0: if (optarg) { g_convert("TEST", -1, "UTF-8", optarg, NULL, NULL, &error); if (error) { g_error_free(error); error = NULL; } else { g_free(fi->charset); fi->charset = g_strdup(optarg); } } break; case 't': if (optarg) indent_set_default_tab_width(atoi(optarg)); break; case 'j': if (optarg) jump_linenum = atoi(optarg); break; case 'v': g_print("%s\n", PACKAGE_STRING); exit(0); case '?': print_usage(); exit(0); } } while (c != -1); #endif if (fi->charset //Find fileinfo encarray data structure && (g_strcasecmp(fi->charset, get_default_charset()) != 0) && (g_strcasecmp(fi->charset, "UTF-8") != 0)) { encarray = get_encoding_items(get_encoding_code()); for (i = 0; i < ENCODING_MAX_ITEM_NUM; i++) if (encarray->item[i]) if (g_strcasecmp(fi->charset, encarray->item[i]) == 0) break; if (i == ENCODING_MAX_ITEM_NUM) fi->charset_flag = TRUE; } #if GLIB_CHECK_VERSION(2, 6, 0) if (argc >= 2) fi->filename = parse_file_uri(argv[1]); #else if (optind < argc) fi->filename = parse_file_uri(argv[optind]); #endif }
const gchar *detect_charset(const gchar *text) { guint8 c = *text; const gchar *charset = NULL; if (g_utf8_validate(text, -1, NULL)) { while ((c = *text++) != '\0') { if (c > 0x7F) { charset = "UTF-8"; break; } if (c == 0x1B) /* ESC */ { c = *text++; if (c == '$') { c = *text++; switch (c) { case 'B': // JIS X 0208-1983 case '@': // JIS X 0208-1978 charset = "ISO-2022-JP"; continue; case 'A': // GB2312-1980 charset = "ISO-2022-JP-2"; break; case '(': c = *text++; switch (c) { case 'C': // KSC5601-1987 case 'D': // JIS X 0212-1990 charset = "ISO-2022-JP-2"; } break; case ')': c = *text++; if (c == 'C') charset = "ISO-2022-KR"; // KSC5601-1987 } break; } } } if (!charset) charset = get_default_charset(); } if (!charset) { switch (get_encoding_code()) { case LATINC: case LATINC_UA: case LATINC_TJ: charset = detect_charset_cylillic(text); // fuzzy... break; case CHINESE_CN: case CHINESE_TW: case CHINESE_HK: charset = detect_charset_chinese(text); break; case JAPANESE: charset = detect_charset_japanese(text); break; case KOREAN: charset = detect_charset_korean(text); break; case VIETNAMESE: case THAI: case GEORGIAN: charset = get_encoding_items(get_encoding_code())->item[OPENI18N]; break; default: if (strcmp(get_default_charset(), "UTF-8") != 0) charset = get_default_charset(); else if (detect_noniso(text)) charset = get_encoding_items(get_encoding_code())->item[CODEPAGE]; else charset = get_encoding_items(get_encoding_code())->item[OPENI18N]; if (!charset) charset = get_encoding_items(get_encoding_code())->item[IANA]; } } return charset; }
static void parse_args(gint argc, gchar **argv, FileInfo *fi) { EncArray *encarray; gint i; GError *error = NULL; GOptionContext *context; gchar *opt_codeset = NULL; gint opt_tab_width = 0; gboolean opt_jump = 0; gboolean opt_version = FALSE; GOptionEntry entries[] = { { "codeset", 0, 0, G_OPTION_ARG_STRING, &opt_codeset, "Set codeset to open file", "CODESET" }, { "tab-width", 0, 0, G_OPTION_ARG_INT, &opt_tab_width, "Set tab width", "WIDTH" }, { "jump", 0, 0, G_OPTION_ARG_INT, &opt_jump, "Jump to specified line", "LINENUM" }, { "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Show version number", NULL }, { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } }; context = g_option_context_new("[filename]"); g_option_context_add_main_entries(context, entries, PACKAGE); g_option_context_add_group(context, gtk_get_option_group(TRUE)); g_option_context_set_ignore_unknown_options(context, FALSE); g_option_context_parse(context, &argc, &argv, &error); g_option_context_free(context); if (error) { g_print("%s: %s\n", PACKAGE, error->message); g_error_free(error); exit(-1); } if (opt_version) { g_print("%s\n", PACKAGE_STRING); exit(0); } if (opt_codeset) { g_convert("TEST", -1, "UTF-8", opt_codeset, NULL, NULL, &error); if (error) { g_error_free(error); error = NULL; } else { g_free(fi->charset); fi->charset = g_strdup(opt_codeset); } } if (opt_tab_width) indent_set_default_tab_width(opt_tab_width); if (opt_jump) jump_linenum = opt_jump; if (fi->charset && (g_ascii_strcasecmp(fi->charset, get_default_charset()) != 0) && (g_ascii_strcasecmp(fi->charset, "UTF-8") != 0)) { encarray = get_encoding_items(get_encoding_code()); for (i = 0; i < ENCODING_MAX_ITEM_NUM; i++) if (encarray->item[i]) if (g_ascii_strcasecmp(fi->charset, encarray->item[i]) == 0) break; if (i == ENCODING_MAX_ITEM_NUM) fi->charset_flag = TRUE; } if (argc >= 2) fi->filename = parse_file_uri(argv[1]); }
gint main(gint argc, gchar **argv) { Conf *conf; gchar *stdin_data = NULL; bindtextdomain(PACKAGE, LOCALEDIR); bind_textdomain_codeset(PACKAGE, "UTF-8"); textdomain(PACKAGE); pub = g_malloc(sizeof(PublicData)); pub->fi = g_malloc(sizeof(FileInfo)); pub->fi->filename = NULL; pub->fi->charset = NULL; pub->fi->charset_flag = FALSE; pub->fi->lineend = LF; parse_args(argc, argv, pub->fi); #if !ENABLE_XINPUT2 gdk_disable_multidevice(); #endif gtk_init(&argc, &argv); g_set_application_name(PACKAGE_NAME); selection_primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY); pub->mw = create_main_window(); conf = g_malloc(sizeof(Conf)); conf->width = 600; conf->height = 400; conf->fontname = g_strdup("Monospace 12"); conf->wordwrap = FALSE; conf->linenumbers = FALSE; conf->autoindent = FALSE; load_config_file(conf); gtk_window_set_default_size( GTK_WINDOW(pub->mw->window), conf->width, conf->height); set_text_font_by_name(pub->mw->view, conf->fontname); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM( gtk_item_factory_get_widget(pub->mw->menubar, "/M/Options/WordWrap")), conf->wordwrap); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM( gtk_item_factory_get_widget(pub->mw->menubar, "/M/Options/LineNumbers")), conf->linenumbers); indent_refresh_tab_width(pub->mw->view); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM( gtk_item_factory_get_widget(pub->mw->menubar, "/M/Options/AutoIndent")), conf->autoindent); gtk_widget_show_all(pub->mw->window); g_free(conf->fontname); g_free(conf); #if ENABLE_EMACS check_emacs_key_theme(GTK_WINDOW(pub->mw->window), pub->mw->menubar); #endif hlight_init(pub->mw->buffer); undo_init(pub->mw->view, gtk_item_factory_get_widget(pub->mw->menubar, "/M/Edit/Undo"), gtk_item_factory_get_widget(pub->mw->menubar, "/M/Edit/Redo")); // hlight_init(pub->mw->buffer); dnd_init(pub->mw->view); if (pub->fi->filename) file_open_real(pub->mw->view, pub->fi); #ifdef G_OS_UNIX else stdin_data = gedit_utils_get_stdin(); #endif if (stdin_data) { gchar *str; GtkTextIter iter; str = g_convert(stdin_data, -1, "UTF-8", get_default_charset(), NULL, NULL, NULL); g_free(stdin_data); // gtk_text_buffer_set_text(buffer, "", 0); gtk_text_buffer_get_start_iter(pub->mw->buffer, &iter); gtk_text_buffer_insert(pub->mw->buffer, &iter, str, strlen(str)); gtk_text_buffer_get_start_iter(pub->mw->buffer, &iter); gtk_text_buffer_place_cursor(pub->mw->buffer, &iter); gtk_text_buffer_set_modified(pub->mw->buffer, FALSE); gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(pub->mw->view), &iter, 0, FALSE, 0, 0); g_free(str); } if (jump_linenum) { GtkTextIter iter; gtk_text_buffer_get_iter_at_line(pub->mw->buffer, &iter, jump_linenum - 1); gtk_text_buffer_place_cursor(pub->mw->buffer, &iter); // gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview), &iter, 0.1, FALSE, 0.5, 0.5); scroll_to_cursor(pub->mw->buffer, 0.25); } set_main_window_title(); indent_refresh_tab_width(pub->mw->view); // hlight_apply_all(pub->mw->buffer); gtk_main(); return 0; }
int g_kbd_codepage(struct graphics_driver *drv) { if (drv->kbd_codepage >= 0) return drv->kbd_codepage; return get_default_charset(); }