Ejemplo n.º 1
0
void process_phrase(const char *loc_str, LibCore & lib)
{
  if(NULL==loc_str)
    return;
#ifndef HAVE_NATSPEC
  gsize bytes_read;
  gsize bytes_written;
  GError *err=NULL;
  char *str=g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err);
  if(NULL==str){
    std::cerr<<"Can not convert "<<loc_str<<" to utf8.\n"<<err->message<<std::endl;
    g_error_free(err);
    return;
  }
#else
  gchar *str = natspec_convert_with_translit(loc_str, "UTF-8", NULL);
#endif
  if(str[0]=='\0')
    return;
  std::vector<LibCore::SearchResult> res;
  bool is_found=false;
  if(str[0]=='/'){
    is_found=lib.LookupWithFuzzy(str+1, res);
  }
  else if(bContainRule(str)){
    is_found=lib.LookupWithRule(str, res);
  }
  else{
    if(!(is_found=lib.SimpleLookup(str, res)))
      is_found=lib.LookupWithFuzzy(str, res);
  }
  if(is_found){
    std::cout<<"Found "<<res.size()<<" items, similar to "<<PrintUTF8(str)<<"."<<std::endl;

    if(res.size()>1){
      for(size_t i=0; i<res.size(); ++i)
	std::cout<<PrintUTF8(res[i].phrase)<<"("<<i<<")"<<std::endl;
      int choise;
      for(;;){
	std::cout<<"Your choice: ";
	if(std::cin>>choise && choise>=0 && choise<int(res.size())){
	  std::cout<<PrintUTF8(res[choise].explanation)<<std::endl;
	  break;
	}
	else{
	  std::cin.clear();
	  char ch;
	  while(std::cin.get(ch) && ch!='\n');
	  std::cout<<"Invalid choise."<<std::endl<<"It must be from 0 to "<<res.size()-1<<"."<<std::endl;
	}
      }
    }
    else{
Ejemplo n.º 2
0
std::ostream &operator<<(std::ostream &os, PrintUTF8 print_utf8)
{
#ifndef HAVE_NATSPEC
  gsize bytes_read, bytes_written;
  GError *err=NULL;


  gchar *res=g_locale_from_utf8(print_utf8.utf8_str.c_str(), -1, &bytes_read, &bytes_written, &err);
  if(NULL==res){
    std::cerr<<"Can not convert "<<print_utf8.utf8_str<<" to current locale.\n"<<err->message<<std::endl;
    g_error_free(err);
    exit(EXIT_FAILURE);
  }
#else
  gchar *res = natspec_convert_with_translit(print_utf8.utf8_str.c_str(), NULL, "UTF-8");
#endif
  os << res;
  g_free(res); 
  return os;
}
Ejemplo n.º 3
0
int main(int argc, const char** argv)
{
	int is_utf8;
#if defined HAVE_LIBPOPT
	int rc = 0;
	poptContext poptCtx;
	poptCtx = poptGetContext("natspec", argc, (const char **)argv, options, 0);
	poptSetOtherOptionHelp(poptCtx, "[OPTION...]");
	while (rc >= 0) {
		if((rc = poptGetNextOpt(poptCtx)) < -1) {
			fprintf(stderr, "Error on option %s: %s.\nRun '%s --help' to see a full list of available command line options.\n",
				poptBadOption(poptCtx, 0),
				poptStrerror(rc),
				argv[0]);
			exit(1);
		}
	}
/*
  { int flag_help=1;
  if (argv) {
      while (poptPeekArg(poptCtx))
		flag_help=0;
		//locale = (char *)poptGetArg(poptCtx);
      poptFreeContext(poptCtx);
	}
*/	
	if(flag_help)
	{
		poptPrintHelp(poptCtx, stdout, 0);
		exit(0);
	}

#else
	fprintf(stderr, "Compiled without popt. Exit\n");
	exit(1);
#endif

        /* Print overall information by default */
	if (argc == 1)
		info = 1;

	if (version || info)
	{
		printf("%s, compiled %s\n", PACKAGE_STRING,__DATE__);
		if (!info)
			exit(0);
	}
	if (transliterate)
	{
		char *str = NULL;
		if (argv) {
#if defined HAVE_LIBPOPT
			while (poptPeekArg(poptCtx))
				str = (char *)poptGetArg(poptCtx);
			poptFreeContext(poptCtx);
#else
			fprintf(stderr, "Compiled without popt. Exit\n");
			exit(1);
#endif
		}
		if ( str != NULL)
		{
			setlocale(LC_ALL,"");
			str = natspec_convert_with_translit(str, "", transliterate);
			puts(str);
		}
		exit(0);
	}
	if (info)
	{
		printf(" === Overall locale information ===\n");
		verbose = 1;
	}
	locale = natspec_get_current_locale();
	if (get_user_locale || info)
	{
		VERBOSE("Current user locale: ");
		printf("%s", locale);
		if (verbose && !natspec_internal_get_locale_from_env())
			printf(" (got from system locale)");
		puts("");
	}
	if (get_system_locale || info)
	{
		VERBOSE("System locale: ");
		printf("%s\n", natspec_get_system_locale());
		if (!info)
			exit(0);
	}
	if (fsenc || info)
	{
		const char *buf;
		VERBOSE("Filename encoding in iconv/nls form: ");
		buf = natspec_get_filename_encoding("");
		printf("%s\n",buf);
		if (!info)
			exit(0);
	}
	if (country_id || info)
	{
		int id;
		VERBOSE("Country ID (for DOS): ");
		id = -1; /* natspec_get_country_id(); */
		printf("%d\n", id);
		if (!info)
			exit(0);
	}
	if (fcodepage || info)
	{
		int cp;
		VERBOSE("Codepage of DOS: ");
		cp = natspec_get_codepage_by_charset(
			natspec_get_charset_by_locale(NATSPEC_DOSCS, locale));
		printf("%d\n", cp);
		if (!info)
			exit(0);
	}

	if (info)
	{
		char *types[]={"UNIX","WIN","DOS","MAC"};
		int i;
		for (i=0; i<sizeof(types)/sizeof(char*);i++)
			get_charset(types[i]);
		setlocale(LC_ALL,"");
		printf("After setlocale:\n");
#ifdef HAVE_LANGINFO_H
		printf("\tnl_langinfo(CODESET): %s\n",nl_langinfo(CODESET));
#endif
		printf("\tnatspec_get_charset: %s\n",natspec_get_charset());
	}

	if (charset_type)
		get_charset(charset_type);
	is_utf8 = natspec_locale_is_utf8("");

	if (utf8 || info)
	{
		VERBOSE("Current locale is%sin UTF8 encoding\n",(is_utf8 ? " " : " NOT "));
		if (!info)
			return is_utf8 ? 0 : 1;
	}
	return 0;
}