Esempio n. 1
0
void
Dictionary::load(const MeguroEnvironment* env,const char* filename) 
{
  TCADB* tcdb = tcadbnew();
  char path[4096];
  root_ = NULL;

  snprintf(path,4096,"%s#mode=ref#opts=l", (char*) filename);
  if(!tcadbopen(tcdb,path)) {
    throw DictionaryException();
  }

  if(!tcadbiterinit(tcdb)) {
    throw DictionaryException();
  }

  uint64_t rnum = tcadbrnum(tcdb);

  Progress* progress = new Progress("Loading Dictionary",rnum, env->verbose_progress);

  char* kbuf = NULL;
  while((kbuf = tcadbiternext2(tcdb)) != NULL) {
    progress->tick();
    char* vbuf = tcadbget2(tcdb,kbuf);
    if (!vbuf)
      vbuf = strdup("");
    dict_elem_t* elem = (dict_elem_t*) malloc(sizeof(dict_elem_t));
    elem->key = kbuf;
    elem->value = vbuf;
    HASH_ADD_KEYPTR(hh,root_,elem->key,strlen(kbuf),elem);
  }

  progress->done();
  delete progress;

  if (!tcadbclose(tcdb)) {
  }

  tcadbdel(tcdb);
}
Esempio n. 2
0
int main (int argc, char** argv)
{
  try
  {
    // We don't want any signals causing the program to quit in mid output, as
    // this would lead to odd colors persisting in the terminal.
    signal (SIGHUP,    SIG_IGN);
    signal (SIGINT,    SIG_IGN);
    signal (SIGKILL,   SIG_IGN);
    signal (SIGPIPE,   SIG_IGN);
    signal (SIGTERM,   SIG_IGN);
    signal (SIGUSR1,   SIG_IGN);
    signal (SIGUSR2,   SIG_IGN);

    long        arg_current    = 0;
#ifdef WAITING_FOR_VITAPI
    std::string arg_done       = "";
#endif
    bool        arg_elapsed    = false;
    bool        arg_estimate   = false;
    std::string arg_label;
    long        arg_max        = 0;
    long        arg_min        = 0;
    bool        arg_percentage = false;
#ifdef WAITING_FOR_VITAPI
    std::string arg_remaining  = "";
#endif
    bool        arg_remove     = false;
    time_t      arg_start      = 0;
    int         arg_width      = 80;
    std::string arg_style      = "";

    // Dynamically determine terminal width.
    unsigned short buff[4];
    if (ioctl (fileno(stdout), TIOCGWINSZ, &buff) != -1)
      arg_width = buff[1];

    static struct option longopts[] = {
      { "current",    required_argument, NULL, 'c' },
#ifdef WAITING_FOR_VITAPI
      { "done",       required_argument, NULL, 'd' },
#endif
      { "elapsed",    no_argument,       NULL, 'e' },
      { "estimate",   no_argument,       NULL, 't' },
      { "label",      required_argument, NULL, 'l' },
      { "max",        required_argument, NULL, 'x' },
      { "min",        required_argument, NULL, 'm' },
      { "now",        no_argument,       NULL, 'n' },
      { "percentage", no_argument,       NULL, 'p' },
#ifdef WAITING_FOR_VITAPI
      { "remaining",  required_argument, NULL, 'a' },
#endif
      { "remove",     no_argument,       NULL, 'r' },
      { "start",      required_argument, NULL, 's' },
      { "version",    no_argument,       NULL, 'v' },
      { "width",      required_argument, NULL, 'w' },
      { "style",      required_argument, NULL, 'y' },
      { "help",       no_argument,       NULL, 'h' },
      { NULL,         0,                 NULL, 0   }
    };

    int ch;
#ifdef WAITING_FOR_VITAPI
    while ((ch = getopt_long (argc, argv, "c:d:etl:x:m:npa:rs:vw:h", longopts, NULL)) != -1)
#else
    while ((ch = getopt_long (argc, argv, "c:etl:x:m:nprs:vw:h", longopts, NULL)) != -1)
#endif
    {
      switch (ch)
      {
      case 'c': arg_current    = atol (optarg);        break;
#ifdef WAITING_FOR_VITAPI
      case 'd': arg_done       = optarg;               break;
#endif
      case 'e': arg_elapsed    = true;                 break;
      case 't': arg_estimate   = true;                 break;
      case 'l': arg_label      = optarg;               break;
      case 'x': arg_max        = atol (optarg);        break;
      case 'm': arg_min        = atol (optarg);        break;
      case 'n': std::cout << time (NULL) << std::endl; exit (0);
      case 'p': arg_percentage = true;                 break;
#ifdef WAITING_FOR_VITAPI
      case 'a': arg_remaining  = optarg;               break;
#endif
      case 'r': arg_remove     = true;                 break;
      case 's': arg_start      = atoi (optarg);        break;
      case 'v': showVersion ();                        break;
      case 'w': arg_width      = atoi (optarg);        break;
      case 'y': arg_style      = optarg;               break;
      case 'h': showUsage ();                          break;

      default:
        std::cout << "<default>" << std::endl;
        break;
      }
    }

    argc -= optind;
    argv += optind;

    // Sanity check arguments.
    if (arg_min || arg_max)
      if (arg_min > arg_max)
        throw std::string ("The --max value must not be less than the --min value.");

    if (arg_min || arg_max || arg_current)
      if (arg_min > arg_current || arg_current > arg_max)
        throw std::string ("The --current value must not lie outside the --min/--max range.");

    if (arg_width && arg_label.length ())
      if (arg_label.length () >= arg_width)
        throw std::string ("The --label string is longer than the allowed --width value.");

    if (! arg_remove && ! (arg_min || arg_current || arg_max))
      showUsage ();

    if (arg_elapsed && arg_start == 0)
      throw std::string ("To use the --elapsed feature, --start must be provided.");

    if (arg_estimate && arg_start == 0)
      throw std::string ("To use the --estimate feature, --start must be provided.");

    // Disallow signals from stopping the program while it is displaying color codes
    // Set up and render Progress object.
    Progress p (arg_label, arg_width, arg_min, arg_max, arg_percentage, arg_remove);
    p.setStyle (arg_style);
    p.setStart (arg_start);
    p.showElapsed (arg_elapsed);
    p.showEstimate (arg_estimate);
    p.removeAfter (arg_remove);
    p.update (arg_current);

    if (arg_remove)
      p.done ();
  }

  catch (const std::string& e) { std::cerr << "Error: " << e << std::endl; }
  catch (...)                  { std::cerr << "Unknown error occurred - please report." << std::endl; }

  return 0;
}