int main(int argc, char* argv[]) { std::cout << "Test print unicode text file\n"; if (argc < 2) return -1; std::vector<char> buf; buf.reserve(1024); if (!ReadFirstBytes(argv[1], std::back_inserter(buf))) return -1; if (buf.empty()) return -1; std::for_each(cbegin(buf), cend(buf), [](char c) { std::cout << std::hex << std::showbase << c; }); std::wifstream in(argv[1], std::ios::binary | std::ios::ate); int64_t fsize = in.tellg(); in.seekg(0, std::ios::beg); in.imbue(DetectLocale(buf.data(), buf.data() + buf.size(), in.getloc())); std::cout << "\nConverted to following UTF-16 by wifstream: \n"; std::wstring line; for (bool first = true; fsize && in.good(); first = false) { //getline(in, line); SafeGetLine(in, line); if (in.bad() || in.fail()) { if (first) { in.close(); in.open(argv[1], std::ios::binary); in.seekg(0, std::ios::beg); in.imbue(std::locale(in.getloc(), new std::codecvt<wchar_t, char, mbstate_t>)); continue; } break; } else { for (auto c : line) std::cout << "U+" << std::hex << std::setw(4) << std::setfill('0') << c << ' '; std::cout << std::endl; if (in.eof()) break; } } return 0; }
static void init_locale(void) { char locale[PATH_MAX]; setlocale (LC_ALL, ""); strcpy(locale, "locale"); bindtextdomain (PACKAGE, locale); textdomain (PACKAGE); DetectLocale(); }
std::shared_ptr<IListModel> UnicodeFile::OpenUnicodeFile(const wchar_t* fileName, std::locale& locale) { static const size_t BufferSizeForEncodingDetector = 1024; std::ios::sync_with_stdio(false); std::vector<char> buf; buf.reserve(BufferSizeForEncodingDetector); if (!ReadFirstBytes(fileName, BufferSizeForEncodingDetector, back_inserter(buf))) return nullptr; std::wifstream in(fileName, std::ios::binary | std::ios::ate); if (!in.is_open()) return nullptr; in.seekg(0, std::ios::beg); if (!buf.empty()) { locale = DetectLocale(buf.data(), buf.data() + buf.size(), in.getloc()); in.imbue(locale); } return std::shared_ptr<UnicodeFile>(new UnicodeFile(in, buf.size())); }
/* The main installer code */ int main(int argc, char **argv) { int exit_status, get_out = 0; int i, c; install_state state; char *xml_file = SETUP_CONFIG; log_level verbosity = LOG_NORMAL; char install_path[PATH_MAX]; char binary_path[PATH_MAX]; const char *product_prefix = NULL, *str; struct enabled_option *enabled_opt; #if defined(darwin) // If we're on Mac OS, we need to make sure the current working directoy // is the same directoy as the .APP is in. With Mac OS X, running from // the finder and most other places makes the current directory to root. #define CARBON_MAX_PATH 1024 char carbon_app_path[CARBON_MAX_PATH]; carbon_GetAppPath(carbon_app_path, CARBON_MAX_PATH); chdir(carbon_app_path); #endif install_path[0] = '\0'; binary_path[0] = '\0'; /* Set a good default umask value (022) */ umask(DEFAULT_UMASK); /* Set the locale */ setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); DetectLocale(); /* Parse the command-line options */ while ( (c=getopt(argc, argv, #ifdef RPM_SUPPORT "hnc:f:r:v:Vi:b:mo:p:" #else "hnc:f:v:Vi:b:o:p:" #endif )) != EOF ) { switch (c) { case 'c': if ( chdir(optarg) < 0 ) { perror(optarg); exit(3); } break; case 'f': xml_file = optarg; break; case 'n': force_console = 1; break; #ifdef RPM_SUPPORT case 'r': rpm_root = optarg; break; #endif case 'v': if ( optarg ) { verbosity = atoi(optarg); if ( (verbosity < LOG_DEBUG) || (verbosity > LOG_FATAL) ){ fprintf(stderr, _("Out of range value, setting verbosity level to normal.\n")); verbosity = LOG_NORMAL; } } else { verbosity = LOG_DEBUG; } break; case 'V': printf("Loki Setup version " SETUP_VERSION ", built on "__DATE__"\n"); exit(0); case 'i': strncpy(install_path, optarg, sizeof(install_path)); disable_install_path = 1; break; case 'b': strncpy(binary_path, optarg, sizeof(binary_path)); disable_binary_path = 1; break; case 'p': product_prefix = optarg; break; case 'o': /* Store the enabled options for later processing */ enabled_opt = (struct enabled_option *)malloc(sizeof(struct enabled_option)); enabled_opt->option = strdup(optarg); enabled_opt->next = enabled_options; enabled_options = enabled_opt; break; #ifdef RPM_SUPPORT case 'm': force_manual = 1; break; #endif default: print_usage(argv[0]); exit(0); } } InitPlugins(); if ( verbosity == LOG_DEBUG ) { DumpPlugins(stderr); } log_init(verbosity); /* Initialize the XML setup configuration */ info = create_install(xml_file, install_path, binary_path, product_prefix); if ( info == NULL ) { fprintf(stderr, _("Couldn't load '%s'\n"), xml_file); exit(3); } /* Get the appropriate setup UI */ for ( i=0; GUI_okay[i]; ++i ) { if ( GUI_okay[i](&UI, &argc, &argv) ) { break; } } if ( ! GUI_okay[i] ) { log_debug(_("No UI drivers available\n")); exit(2); } /* Setup the interrupt handlers */ state = SETUP_INIT; signal(SIGINT, signal_abort); signal(SIGQUIT, signal_abort); signal(SIGHUP, signal_abort); signal(SIGTERM, signal_abort); /* Run the little state machine */ exit_status = 0; while ( ! get_out ) { char buf[1024]; int num_cds = 0; switch (state) { case SETUP_INIT: num_cds = GetProductCDROMDescriptions(info); /* Check for the presence of a CDROM if required */ if ( GetProductCDROMRequired(info) ) { if ( ! GetProductCDROMFile(info) ) { log_fatal(_("The 'cdromfile' attribute is now mandatory when using the 'cdrom' attribute.")); } add_cdrom_entry(info, info->name, info->desc, GetProductCDROMFile(info)); ++ num_cds; } state = UI.init(info,argc,argv, enabled_options != NULL); if ( state == SETUP_ABORT ) { exit_status = 3; } /* Check if getcwd() works now */ if ( getcwd(buf, sizeof(buf)) == NULL ) { UI.prompt(_("Unable to determine the current directory.\n" "Please check the permissions of the parent directories.\n"), RESPONSE_OK); state = SETUP_EXIT; continue; } /* Check if we should be root. Under the Mac, we'll do the standard authorization stuff that most installers do at startup. */ if ( GetProductRequireRoot(info) && geteuid()!=0 ) { #if defined(darwin) carbon_AuthorizeUser(); state = SETUP_EXIT; break; #else UI.prompt(_("You need to run this installer as the super-user.\n"), RESPONSE_OK); state = SETUP_EXIT; continue; #endif } if ( info->product && GetProductInstallOnce(info) ) { UI.prompt(_("\nThis product is already installed.\nUninstall it before running this program again.\n"), RESPONSE_OK); state = SETUP_EXIT; continue; } if ( info->product && GetProductReinstall(info) ) { UI.prompt(_("Warning: You are about to reinstall\non top of an existing installation.\n"), RESPONSE_OK); info->options.reinstalling = 1; /* Restore the initial environment */ loki_put_envvars(info->product); } /* Check for the presence of the product if we install a component */ if ( GetProductComponent(info) ) { if ( GetProductNumComponents(info) > 0 ) { UI.prompt(_("\nIllegal installation: do not mix components with a component installation.\n"), RESPONSE_OK); state = SETUP_EXIT; continue; } else if ( info->product ) { if ( ! info->component ) { snprintf(buf, sizeof(buf), _("\nThe %s component is already installed.\n" "Please uninstall it beforehand.\n"), GetProductComponent(info)); UI.prompt(buf, RESPONSE_OK); state = SETUP_EXIT; continue; } } else { snprintf(buf, sizeof(buf), _("\nYou must install %s before running this\n" "installation program.\n"), info->desc); UI.prompt(buf, RESPONSE_OK); state = SETUP_EXIT; continue; } } /* Check for the presence of a CDROM if required */ if ( num_cds > 0) { detect_cdrom(info); } if ( GetProductCDROMRequired(info) && ! get_cdrom(info, info->name) ) { state = SETUP_EXIT; break; } if ( ! CheckRequirements(info) ) { state = SETUP_ABORT; break; } if ( enabled_options ) { enabled_opt = enabled_options; while ( enabled_opt ) { if ( enable_option(info, enabled_opt->option) == 0 ) { log_warning(_("Could not enable option: %s"), enabled_opt->option); } enabled_opt = enabled_opt->next; } state = SETUP_INSTALL; } break; case SETUP_CLASS: state = UI.pick_class(info); break; case SETUP_LICENSE: state = UI.license(info); break; case SETUP_README: state = UI.readme(info); break; case SETUP_OPTIONS: state = UI.setup(info); break; case SETUP_INSTALL: install_preinstall(info); state = install(info, UI.update); install_postinstall(info); break; case SETUP_WEBSITE: state = UI.website(info); break; case SETUP_COMPLETE: state = UI.complete(info); /* Check for a post-install message */ str = GetProductPostInstallMsg(info); if ( str ) { UI.prompt(str, RESPONSE_OK); } break; case SETUP_PLAY: if ( UI.shutdown ) UI.shutdown(info); state = launch_game(info); break; case SETUP_ABORT: abort_install(); break; case SETUP_EXIT: /* Optional cleanup */ if ( UI.exit ) { UI.exit(info); } get_out = 1; break; } } /* Free enabled_options */ while ( enabled_options ) { enabled_opt = enabled_options; enabled_options = enabled_options->next; free(enabled_opt->option); free(enabled_opt); } exit_setup(exit_status); return 0; }