//------------------------------------------------- // load a game name and offset into an // indexed array //------------------------------------------------- int datfile_manager::index_datafile(fileptr &&fp, dataindex &index, int &swcount, std::string &rev, std::string const &tag, char sep) { std::string readbuf; auto const tag_size = tag.size(); auto const t_info = TAG_INFO.size(); auto const t_bio = TAG_BIO.size(); char rbuf[64 * 1024]; while (std::fgets(rbuf, 64 * 1024, fp.get()) != nullptr) { readbuf = chartrimcarriage(rbuf); if (!tag.empty()) { if (rev.empty() && readbuf.compare(0, tag_size, tag) == 0) { if (sep != 's') rev = readbuf.substr(tag_size + 1, readbuf.find(sep, tag_size + 1) - tag_size); else rev = readbuf.substr(tag_size + 1); } } if (readbuf.compare(0, t_info, TAG_INFO) == 0) { // search for game info auto rd = readbuf.substr(t_info + 1); std::vector<std::string> gamelist = tokenize(rd, ','); for (auto & e : gamelist) { auto game_index = driver_list::find(e.c_str()); if (game_index != -1) index.emplace(&driver_list::driver(game_index), std::ftell(fp.get())); } } else if (!readbuf.empty() && readbuf[0] == DATAFILE_TAG[0]) { // search for software info std::fgets(rbuf, 64 * 1024, fp.get()); std::string readbuf_2(chartrimcarriage(rbuf)); if (readbuf_2.compare(0, t_bio, TAG_BIO) == 0) { auto eq_sign = readbuf.find('='); std::string s_list(readbuf.substr(1, eq_sign - 1)); std::string s_roms(readbuf.substr(eq_sign + 1)); std::vector<std::string> token_list = tokenize(s_list, ','); std::vector<std::string> token_roms = tokenize(s_roms, ','); for (auto & li : token_list) for (auto & ro : token_roms) m_swindex[li].emplace(ro, std::ftell(fp.get())); swcount++; } } } return index.size(); }
//------------------------------------------------- // load a game name and offset into an // indexed array //------------------------------------------------- int datfile_manager::index_datafile(dataindex &index, int &swcount) { std::string readbuf, name; size_t t_hist = TAG_HISTORY_R.size(); size_t t_story = TAG_STORY_R.size(); size_t t_sysinfo = TAG_SYSINFO_R.size(); size_t t_info = TAG_INFO.size(); size_t t_bio = TAG_BIO.size(); char rbuf[64 * 1024]; while (fgets(rbuf, 64 * 1024, fp) != nullptr) { readbuf = chartrimcarriage(rbuf); if (m_history_rev.empty() && readbuf.compare(0, t_hist, TAG_HISTORY_R) == 0) { size_t found = readbuf.find(" ", t_hist + 1); m_history_rev = readbuf.substr(t_hist + 1, found - t_hist); } else if (m_sysinfo_rev.empty() && readbuf.compare(0, t_sysinfo, TAG_SYSINFO_R) == 0) { size_t found = readbuf.find(".", t_sysinfo + 1); m_sysinfo_rev = readbuf.substr(t_sysinfo + 1, found - t_sysinfo); } else if (m_story_rev.empty() && readbuf.compare(0, t_story, TAG_STORY_R) == 0) m_story_rev = readbuf.substr(t_story + 1); // TAG_INFO identifies the driver else if (readbuf.compare(0, t_info, TAG_INFO) == 0) { int curpoint = t_info + 1; int ends = readbuf.size(); while (curpoint < ends) { // search for comma size_t found = readbuf.find(",", curpoint); // found it if (found != std::string::npos) { // copy data and validate driver name = readbuf.substr(curpoint, found - curpoint); // validate driver int game_index = driver_list::find(name.c_str()); if (game_index != -1) index.emplace(&driver_list::driver(game_index), ftell(fp)); // update current point curpoint = ++found; } // if comma not found, copy data while until reach the end of string else if (curpoint < ends) { name = readbuf.substr(curpoint); int game_index = driver_list::find(name.c_str()); if (game_index != -1) index.emplace(&driver_list::driver(game_index), ftell(fp)); // update current point curpoint = ends; } } } // search for software info else if (!readbuf.empty() && readbuf[0] == DATAFILE_TAG[0]) { fgets(rbuf, 64 * 1024, fp); std::string readbuf_2(chartrimcarriage(rbuf)); // TAG_BIO identifies software list if (readbuf_2.compare(0, t_bio, TAG_BIO) == 0) { size_t eq_sign = readbuf.find("="); std::string s_list(readbuf.substr(1, eq_sign - 1)); std::string s_roms(readbuf.substr(eq_sign + 1)); int ends = s_list.size(); int curpoint = 0; while (curpoint < ends) { size_t found = s_list.find(",", curpoint); // found it if (found != std::string::npos) { name = s_list.substr(curpoint, found - curpoint); curpoint = ++found; } else { name = s_list; curpoint = ends; } // search for a software list in the index, if not found then allocates std::string lname(name); int cpoint = 0; int cends = s_roms.size(); while (cpoint < cends) { // search for comma size_t found = s_roms.find(",", cpoint); // found it if (found != std::string::npos) { // copy data name = s_roms.substr(cpoint, found - cpoint); // add a SoftwareItem m_swindex[lname].emplace(name, ftell(fp)); // update current point cpoint = ++found; swcount++; } else { // if reach the end, bail out if (s_roms[cpoint] == '\r' || s_roms[cpoint] == '\n') break; // copy data name = s_roms.substr(cpoint); // add a SoftwareItem m_swindex[lname].emplace(name, ftell(fp)); // update current point cpoint = cends; swcount++; } } } } } } return index.size(); }