static bool readLine(G_FILE *f, std::string *line) { const int BUFFER_SIZE = 256; char buffer[BUFFER_SIZE]; std::string line2; for (;;) { if (g_fgets(buffer, BUFFER_SIZE, f) == NULL) // eof? { *line = line2; return !line2.empty(); // check whether read something } size_t len = strlen(buffer); if (len == 0 || buffer[len - 1] != '\n') { line2 += buffer; } else { buffer[--len] = 0; // do not include '\n' if (len > 0 && buffer[len - 1] == '\r') // do not include '\r' buffer[len - 1] = 0; line2 += buffer; *line = line2; return true; // read at least an '\n' } } }
void ApplicationManager::loadLuaFiles() { std::vector<std::string> luafiles; G_FILE* fis = g_fopen("luafiles.txt", "rt"); if (fis) { char line[1024]; while (true) { if (g_fgets(line, 1024, fis) == NULL) break; size_t len = strlen(line); if (len > 0 && line[len - 1] == 0xa) line[--len] = 0; if (len > 0 && line[len - 1] == 0xd) line[--len] = 0; if (len > 0) luafiles.push_back(line); } g_fclose(fis); } GStatus status; for (size_t i = 0; i < luafiles.size(); ++i) { application_->loadFile(luafiles[i].c_str(), &status); if (status.error()) break; } if (!status.error()) { gapplication_enqueueEvent(GAPPLICATION_START_EVENT, NULL, 0); application_->tick(&status); } if (status.error()) luaError(status.errorString()); }
void TexturePack::readTextureList(const char* texturelistfile, std::vector<Rect>& textures_, std::map<std::string, int>& filenameMap_, int* pwidth, int* pheight) { G_FILE* fis = g_fopen(texturelistfile, "rt"); if (fis == NULL) { throw GiderosException(GStatus(6000, texturelistfile)); // Error #6000: %s: No such file or directory. return; } textures_.clear(); filenameMap_.clear(); int width = 0; int height = 0; char line[1024]; while (true) { line[0] = line[1023] = 0; if (g_fgets(line, 1024, fis) == NULL) break; char* c; if ((c = strrchr(line, 0xa))) *c = '\0'; if ((c = strrchr(line, 0xd))) *c = '\0'; if (line[0] == '\0') break; std::vector<std::string> result; pystring::split(line, result, ","); for (std::size_t i = 0; i < result.size(); ++i) { if (result[i].empty() == false) result[i] = pystring::strip(result[i]); } if (result.size() >= 9) { Rect rect; rect.x = atoi(result[1].c_str()); rect.y = atoi(result[2].c_str()); rect.width = atoi(result[3].c_str()); rect.height = atoi(result[4].c_str()); rect.dx1 = atoi(result[5].c_str()); rect.dy1 = atoi(result[6].c_str()); rect.dx2 = atoi(result[7].c_str()); rect.dy2 = atoi(result[8].c_str()); filenameMap_[result[0]] = textures_.size(); textures_.push_back(rect); width += rect.width + rect.dx1 + rect.dx2; height += rect.height + rect.dy1 + rect.dy2; } } g_fclose(fis); if (pwidth) *pwidth = width; if (pheight) *pheight = height; if (textures_.empty() == true) { throw GiderosException(GStatus(6008, texturelistfile)); // Error #6008: %s: File does not contain texture region information. return; } }