Logo Search packages:      
Sourcecode: easyh10 version File versions  Download package

int h10db_read ( h10db_t *  h10db,
const ucs2_char_t *  path 
)

Read an existing H10 database.

Parameters:
h10db The pointer to the h10db_t instance.
path The path to the H10 database.
Returns:
0 if succeeded, otherwise -1.

Definition at line 379 of file h10db.c.

{
      int ret, i, num_valid_idx = 0, idx_count = 0;
      struct bfile *bfp = NULL;
      ucs2_char_t filename[MAX_PATH+1];
      ucs2_char_t filepart[MAX_PATH+1];

      /* Notify the start of H10 database reading. */
      h10db_progress(h10db, H10DB_PROGRESS_READ_START, 0, 0);

      /* Open H10DB.hdr */
      h10db->msg = H10DB_PROGRESS_READ_HDR;
      combine_path(filename, MAX_PATH, path, ucs2cs_hdr_filename);
      bfp = bf_open(filename, 0, h10db, h10db_read_progress);
      if (!bfp) {
            return H10DBE_HDR_OPENR;
      } else {
            /* Notify the start of H10DB.hdr parsing. */
            h10db_progress(h10db, H10DB_PROGRESS_PARSE_HDR, 0, 1);

            /* Parse H10DB.hdr */
            ret = h10db_hdr_serialize(bfp, h10db->hdr, 0, 0);
            bf_close(bfp);
            if (ret != 0) {
                  return H10DBE_HDR_READ;
            }

            /* Successful parse of H10DB.hdr */
            h10db_progress(h10db, H10DB_PROGRESS_PARSE_HDR, 1, 1);
      }

      /* Open H10DB.dat */
      h10db->msg = H10DB_PROGRESS_READ_DAT;
      get_filepart(filepart, MAX_PATH, h10db->hdr->pathname_dat, H10DB_PATHLENGTH);
      combine_path(filename, MAX_PATH, path, filepart);
      bfp = bf_open(filename, 0, h10db, h10db_read_progress);
      if (!bfp) {
            return H10DBE_DAT_OPENR;
      } else {
            uint32_t num_dat = 0;

            /* Notify the start of H10DB.dat parsing. */
            h10db_progress(h10db, H10DB_PROGRESS_PARSE_DAT, 0, 1);

            /* Parse H10DB.dat */
            ret = (int)h10db_dat_read(bfp, &h10db->dat, &num_dat, h10db->hdr);
            bf_close(bfp);
            if (ret != 0) {
                  return ret;
            }

            /* Check the consistency of number of read entries. */
            if (num_dat != h10db->hdr->num_dat_entries) {
                  return H10DBE_DBINCONSISTENCY;
            }

            /* Successful parse of H10DB.dat */
            h10db_progress(h10db, H10DB_PROGRESS_PARSE_DAT, 1, 1);
      }

      /* Allocate the array of h10db_idx_t */
      h10db->idx = (h10db_idx_t**)calloc(h10db->hdr->num_dat_fields, sizeof(h10db_idx_t*));
      if (!h10db->idx) {
            return H10DBE_OUTOFMEMORY;
      }

      /* Count the number of valid indexes (only for progress report). */
      for (i = 0;i < H10DB_NUM_DAT_FIELDS;i++) {
            if (h10db->hdr->fd[i].has_index && h10db->hdr->fd[i].index_pathname) {
                  num_valid_idx++;
            }
      }

      /* Open and parse all H10DB_*.idx */
      idx_count = 0;
      for (i = 0;i < (int)h10db->hdr->num_dat_fields;i++) {
            if (h10db->hdr->fd[i].has_index && h10db->hdr->fd[i].index_pathname) {
                  /* Notify the start of an index file parsing. */
                  h10db_progress(h10db, H10DB_PROGRESS_IDX_COUNT, idx_count++, num_valid_idx);

                  /* Open the idx file. */
                  h10db->msg = H10DB_PROGRESS_READ_IDX;
                  get_filepart(filepart, MAX_PATH, h10db->hdr->fd[i].index_pathname, H10DB_PATHLENGTH);
                  combine_path(filename, MAX_PATH, path, filepart);
                  bfp = bf_open(filename, 0, h10db, h10db_read_progress);
                  if (!bfp) {
                        return H10DBE_IDX_OPENR;
                  } else {
                        int num_idx_elems = 0;
                        h10db_idx_t* h10db_idx = NULL;

                        /* Notify the start of an index file parsing. */
                        h10db_progress(h10db, H10DB_PROGRESS_PARSE_IDX, 0, 1);
                        ret = h10db_idx_read(bfp, &h10db->idx[i], &num_idx_elems);
                        bf_close(bfp);
                        if (ret != 0) {
                              return ret;
                        }

                        /* Check the consistency of number of read entries. */
                        if (num_idx_elems != h10db->hdr->num_dat_entries) {
                              return H10DBE_DBINCONSISTENCY;
                        }

                        /* Successful parse of an index. */
                        h10db_progress(h10db, H10DB_PROGRESS_PARSE_IDX, 1, 1);
                  }
            }
      }
      /* Notify the finish of all index files. */
      h10db_progress(h10db, H10DB_PROGRESS_IDX_COUNT, idx_count, num_valid_idx);

      /* Disabled the usage of H10DB.upd. */
#ifdef      H10DB_USE_UPD
      /* Open and parse H10DB.upd */
      if (h10db->flags & H10DB_FLAG_INCREMENTAL) {
            free(h10db->upd);
            h10db->upd = (h10db_upd_t*)malloc(sizeof(h10db_upd_t));
            if (h10db->upd) {
                  combine_path(filename, MAX_PATH, path, ucs2cs_upd_filename);
                  bfp = bf_open(filename, 0, h10db, h10db_read_progress);
                  h10db_progress(h10db, H10DB_PROGRESS_PARSE_UPD, 0, 1);
                  h10db_upd_read(h10db->upd, bfp);    /* Don't care about H10DB.upd error. */
                  h10db_progress(h10db, H10DB_PROGRESS_PARSE_UPD, 1, 1);
            }
            /* We don't issue an error but continue. */
      }
#endif

      /* Notify the end of H10 database reading. */
      h10db_progress(h10db, H10DB_PROGRESS_READ_END, 0, 0);

      return 0;
}


Generated by  Doxygen 1.6.0   Back to index