int main(int argc, char * argv[]){
  MemoryChunk* chunk;
  chunk = new MemoryChunk();
  int i = 12;
  chunk->set_content(0, &i, sizeof(int));

  int * p = (int *)chunk->begin();
  assert(chunk->size() == sizeof(int));
  std::cout<<*p<<std::endl;
  std::cout<<chunk->capacity()<<std::endl;
  p = & i;
  chunk->set_chunk(p, sizeof(int), NULL);
  short t = 5;
  chunk->set_content(sizeof(int), &t, sizeof(short));
  assert( sizeof(int) + sizeof(short) == chunk->size());
  std::cout<<chunk->capacity()<<std::endl;

  p = (int *)chunk->begin();
  short * p2 =(short *)(((char *) (chunk->begin())) + sizeof(int));
  std::cout<<*p<<'\t'<<*p2<<std::endl;

  chunk->set_content(sizeof(int) + sizeof(short), &t, sizeof(short));
  
  assert( sizeof(int) + (sizeof(short) << 1) == chunk->size());
  std::cout<<chunk->capacity()<<std::endl;
  p = (int *)chunk->begin();
  p2 =(short *)(((char *) (chunk->begin())) + sizeof(int));
  std::cout<<*p<<'\t'<<*p2<<'\t'<<*(p2 + 1)<<std::endl;

  chunk->set_size(sizeof(int) + sizeof(short) *3);
  p = (int *)chunk->begin();
  p2 =(short *)(((char *) (chunk->begin())) + sizeof(int));

  chunk->set_content(0, &i, sizeof(int));

  *(p2+2) = 3;
  std::cout<<*p<<'\t'<<*p2<<'\t'<<*(p2 + 1)<<'\t'<<*(p2+2)<<std::endl;

  int m = 10;
  chunk->set_chunk(&m, sizeof(int), NULL);
  int n = 12;
  chunk->insert_content(sizeof(int), &n, sizeof(int));
  n = 11;
  chunk->insert_content(sizeof(int), &n, sizeof(int));

  int * p3 = (int *)chunk->begin();
  std::cout<<*p3<<'\t'<<*(p3+1)<<'\t'<<*(p3+2)<<std::endl;
	
  chunk->remove_content(sizeof(int), sizeof(int));
  std::cout<<*p3<<'\t'<<*(p3+1)<<std::endl;

  int tmp;
  assert(chunk->get_content(sizeof(int), &tmp, sizeof(int)));
  std::cout<<tmp<<std::endl;
  
  
  delete chunk;

  const char * filename =  "/tmp/version";
  const char * version = "0.2.0";

  chunk =  new MemoryChunk;
  bool retval = chunk->load(filename);
  if ( !retval ){
      std::cerr<<"can't find chunk"<<std::endl;
  }else{
      if ( memcmp(version, chunk->begin(), strlen(version) + 1) == 0){
	  std::cout<<"match"<<std::endl;
      }

  }

  chunk->set_content(0, version, strlen(version) + 1);
  chunk->save(filename);

  retval = chunk->load(filename);
  if ( !retval ){
      std::cerr<<"can't find chunk"<<std::endl;
  }
  if ( memcmp(version, chunk->begin(), strlen(version) + 1) == 0){
      std::cout<<"match"<<std::endl;
  }

  return 0;
}
예제 #2
0
int main(int argc, char * argv[]){
    FILE * input = stdin;
    const char * bigram_filename = "bigram.db";

    PhraseLargeTable phrases;

    MemoryChunk * chunk = new MemoryChunk;
    bool retval = chunk->load("phrase_index.bin");
    if (!retval) {
        fprintf(stderr, "open phrase_index.bin failed!\n");
        exit(ENOENT);
    }
    phrases.load(chunk);

    FacadePhraseIndex phrase_index;

    //gb_char binary file
    chunk = new MemoryChunk;
    retval = chunk->load("gb_char.bin");
    if (!retval) {
        fprintf(stderr, "open gb_char.bin failed!\n");
        exit(ENOENT);
    }
    phrase_index.load(1, chunk);

    //gbk_char binary file
    chunk = new MemoryChunk;
    retval = chunk->load("gbk_char.bin");
    if (!retval) {
        fprintf(stderr, "open gbk_char.bin failed!\n");
        exit(ENOENT);
    }
    phrase_index.load(2, chunk);

    Bigram bigram;
    retval = bigram.attach(bigram_filename, ATTACH_CREATE|ATTACH_READWRITE);
    if (!retval) {
        fprintf(stderr, "open %s failed!\n", bigram_filename);
        exit(ENOENT);
    }

    taglib_init();

    values = g_ptr_array_new();
    required = g_hash_table_new(g_str_hash, g_str_equal);

    //enter "\data" line
    assert(taglib_add_tag(BEGIN_LINE, "\\data", 0, "model", ""));
    ssize_t result = my_getline(input);
    if ( result == -1 ) {
        fprintf(stderr, "empty file input.\n");
        exit(ENODATA);
    }

    //read "\data" line
    if ( !taglib_read(linebuf, line_type, values, required) ) {
        fprintf(stderr, "error: interpolation model expected.\n");
        exit(ENODATA);
    }

    assert(line_type == BEGIN_LINE);
    char * value = NULL;
    assert(g_hash_table_lookup_extended(required, "model", NULL, (gpointer *)&value));
    if ( !( strcmp("interpolation", value) == 0 ) ) {
        fprintf(stderr, "error: interpolation model expected.\n");
        exit(ENODATA);
    }

    result = my_getline(input);
    if ( result != -1 )
        parse_body(input, &phrases, &phrase_index, &bigram);

    taglib_fini();

    chunk = new MemoryChunk;
    phrase_index.store(1, chunk);
    chunk->save("gb_char.bin");
    phrase_index.load(1, chunk);

    chunk = new MemoryChunk;
    phrase_index.store(2, chunk);
    chunk->save("gbk_char.bin");
    phrase_index.load(2, chunk);

    return 0;
}