Example #1
0
Parts read_parts_file(char *infile, int(*process_records)(Parts, FILE *, off_t))
{
  FILE *istream;
  if ((istream = fopen(infile, "rb")) == NULL) {
    print_error(__FILE__, infile);
    return NULL;
  }

  struct stat infile_stat;
  if (stat(infile, &infile_stat) != 0) {
    print_error(__FILE__, infile);
    return NULL;
  }
  off_t record_size = (off_t)get_part_record_size();
  if (infile_stat.st_size % record_size) {
    fprintf(stderr, "Corrupt file '%s': size must be multiple of %ld\n",
        infile,
        (size_t)record_size);
    return NULL;
  }
  Parts db = new_db(infile_stat.st_size / record_size);
  if (process_records(db, istream, record_size) !=0)  {
    print_error(__FILE__, infile);
    return NULL;
  }

  if (fclose(istream) == EOF) {
    print_error(__FILE__, infile);
    return NULL;
  }

  return db;
}
Example #2
0
bool IndDBase::attach( const std::string & n )
{

  if ( n == "-" || n == "." ) { dettach(); return false; } 

  if ( ! Helper::fileExists(n) )
    {
      new_db(n);
      return true;
    }
  
  sql.open(n);
  
  index();
  
  init();
  
  //
  // Load new meta-information table
  //
  
  set_metatypes( );
  
  return true;
}
int main(int argc, char *argv[])
{
  if (argc != 3)
    invocation_error(argv[0], "[output file] [size]");

  char *output_file = argv[1];
  size_t records = atol(argv[2]);
  size_t i, part_number, noun_count, adj_count;
  char *nouns[NOUN_MAX], noun_file_content[NOUN_BYTES];
  char *adjectives[ADJ_MAX], adj_file_content[ADJ_BYTES];
  int rc;
  Part p;

  Parts db = new_db(CHUNK_SIZE);

  noun_count = get_word_pointers(nouns, NOUN_FILE, noun_file_content, NOUN_BYTES);
  adj_count = get_word_pointers(adjectives, ADJ_FILE, adj_file_content, ADJ_BYTES);

  init_locale();
  srand((unsigned int) time(NULL));
  remove(output_file);

  for (part_number = 0, i = 0; i < records; i++) {
    part_number = jagged_sequence(part_number);
    p = set_part(part_number,
          random_part_name(nouns, noun_count, adjectives, adj_count),
          random_int(),
          random_int());
    printf("%9ld:  ", i + 1);
    print_part(p);
    if ((rc = insert_part(db, p))) {
      fprintf(stderr, "%s: %d  insert_part() failed: return code %d on iteration %ld\n",
          __FILE__, __LINE__, rc, i);
      destroy_db(db);
      exit(EXIT_FAILURE);
    }
    if (i % CHUNK_SIZE == 0) {
      if (flush_to_disk(output_file, db) != 0) {
        destroy_db(db);
        exit_error(argv[0], output_file);
      }
    }
  }

  if (flush_to_disk(output_file, db) != 0) {
    destroy_db(db);
    exit_error(argv[0], output_file);
  }
  destroy_db(db);

  return 0;
}
Example #4
0
int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *keyname;
  GtkWidget *fileselection;

  if (argc > 1) db_file = g_strdup(argv[1]);

  gtk_set_locale ();
  gtk_init (&argc, &argv);

  main_window = create_window ();
  new_db(main_window, db_file);
  init_type_menu();
  gtk_widget_show (main_window);

  gtk_main ();
  return 0;
}
Example #5
0
/*-------------------------------------------------------------------------*/
svalue_t * 
f_sl_open (svalue_t *sp) 

/* EFUN sl_open
 *
 *   int sl_open(string filename)
 *
 * Opens the file <filename> for use as a SQLite database.
 * If the file doesn't exists it will be created.
 * Only one open file per object is allowed. On success this
 * function returns 1, otherwise usually an error is thrown.
 */

{
    string_t *file;
    sqlite3 *db;
    sqlite_dbs_t *tmp;
    int err;
   
    file = check_valid_path(sp->u.str, current_object, STR_SQLITE_OPEN , MY_TRUE);
    if (!file)
        errorf("Illegal use of sl_open('%s')\n", get_txt(sp->u.str));
   
    tmp = find_db (current_object);
    if (tmp)
    {
        free_mstring(file);
        errorf("The current object already has a database open.\n");
    }

    err = sqlite3_open (get_txt(file), &db);
    free_mstring(file);
    if (err)
    {
        const char* msg = sqlite3_errmsg(db);
        sqlite3_close(db);
        errorf("sl_open: %s\n", msg );
        /* NOTREACHED */
    }

    /* create a new chain link and hang on the old chain */
    tmp = new_db(); 
    if(!tmp)
    {
        sqlite3_close(db);
        errorf("(sl_open) Out of memory: (%lu bytes)\n",
                (unsigned long) sizeof(*tmp));
    }
   
    tmp->db = db;
    tmp->obj = current_object;
    current_object->open_sqlite_db = MY_TRUE;

    /* Synchronous is damn slow. Forget it. */
    sqlite3_exec(db, "PRAGMA synchronous = OFF", NULL, NULL, NULL);
    sqlite3_set_authorizer(db, my_sqlite3_authorizer, NULL);
  
    free_string_svalue (sp);
    put_number (sp, 1);
    return sp;
} /* f_sl_open() */