// prints the required arguments first (if any) then the optional // ones (if any) ostream& CommandOptionParser::displayUsage(ostream& out, bool doPretty) { CommandOptionVec::size_type index; CommandOption *trailing = NULL; char *colch = getenv("COLUMNS"); int columns = 80; unsigned maxlen = 0; if (colch) { string colStr(colch); columns = asInt(colStr); } // find the trailing argument if any, and max option string length for (index = 0; index < optionVec.size(); index++) { if (optionVec[index]->optType == CommandOption::trailingType) trailing = optionVec[index]; else if (optionVec[index]->optType == CommandOption::stdType) maxlen = std::max(maxlen, unsigned(optionVec[index]->getFullOptionString().length())); } out << "Usage: " << progName; if (hasRequiredArguments || hasOptionalArguments) out << " [OPTION] ..."; if (trailing) out << " " << trailing->description; out << endl << (doPretty ? prettyPrint(text,"\n","","",columns) : text); // << endl // << endl // << "Command options:" << endl; for(int required = 1; required >= 0; required--) { if (required==1 && hasRequiredArguments) out << endl << "Required arguments:" << endl; else if (required==0 && hasOptionalArguments) out << endl << "Optional arguments:" << endl; for(index = 0; index < optionVec.size(); index++) { if ((optionVec[index]->required == (required==1)) && (optionVec[index]->optType == CommandOption::stdType)) { string optstr(optionVec[index]->getFullOptionString()); string desc(optionVec[index]->description); string indent(maxlen, ' '); if(doPretty) { leftJustify(optstr, maxlen); prettyPrint(desc, "\n", indent, optstr, columns); } out << desc; if(!doPretty) out << endl; } } } return out; }
Island::Island(const std::string& filename, int pos_x, int pos_y, std::vector<bool>& waterTiles) : m_x(pos_x), m_y(pos_y), m_tiles(NULL), m_defaultTile(0) { m_defaultTile = Tile::getDefaultTile(); std::cout << "Trying to load island \"" << filename << "\"" << std::endl; TiXmlDocument document; if (!document.PHYSFS_LoadFile(filename)) { throw FileLoadException(filename, document.ErrorDesc()); } if(!document.RootElement() || document.RootElement()->ValueStr() != "island") throw XMLException(filename, -1, "This is no valid island XML file (missing island root element)"); TiXmlElement *island = document.RootElement(); if (!island->Attribute("cols")) throw XMLException(filename, island->Row(), "Missing attribute cols"); if (!island->Attribute("rows")) throw XMLException(filename, island->Row(), "Missing attribute rows"); if (!island->Attribute("clime")) throw XMLException(filename, island->Row(), "Missing attribute clime"); std::stringstream attr; attr << island->Attribute("cols") << " " << island->Attribute("rows"); attr >> m_cols >> m_rows; m_clime = island->Attribute("clime"); std::cout << "Creating " << (m_rows * m_cols) << " tiles" << std::endl; m_tiles = new Tile* [m_rows * m_cols]; memset(m_tiles, 0, sizeof(Tile*) * m_rows * m_cols); TiXmlElement *terrain = island->FirstChildElement("terrain"); if (!terrain) throw XMLException(filename, island->Row(), "Missing toplevel element terrain"); TiXmlElement *row = terrain->FirstChildElement("row"); if (!terrain) throw XMLException(filename, terrain->Row(), "Missing row subelements"); while (row) { if (!row->Attribute("value")) throw XMLException(filename, row->Row(), "Missing attribute value"); std::stringstream rowStr(row->Attribute("value")); int rowInt; rowStr >> rowInt; rowInt--; TiXmlElement *col = row->FirstChildElement("col"); if (!col) throw XMLException(filename, row->Row(), "Missing col subelements"); while (col) { if (!col->Attribute("value")) throw XMLException(filename, col->Row(), "Missing attribute value"); std::stringstream colStr(col->Attribute("value")); int colInt; colStr >> colInt; colInt--; TiXmlElement *tile = col->FirstChildElement("tile"); if (!tile) throw XMLException(filename, col->Row(), "Missing tile subelement"); if (((rowInt * m_cols) + colInt) >= m_cols * m_rows) std::cout << "WARNING! Index out of bounds. Row: " << rowInt << ", column: " << colInt << std::endl; else { m_tiles[(rowInt * m_cols) + colInt] = new Tile(tile); if (m_tiles[(rowInt * m_cols) + colInt] == NULL) { std::cout << "TILE CREATION FAILED" << std::endl; } waterTiles[(rowInt * m_cols) + colInt] = false; } col = col->NextSiblingElement("col"); } row = row->NextSiblingElement("row"); } std::cout << "Succesfully loaded island \"" << filename << "\"" << std::endl; std::cout << "\tColums: " << m_cols << std::endl; std::cout << "\tRows: " << m_rows << std::endl; /* std::cout << "debug-listing 0,0 to 9,9" << std::endl; for (int y = 0; y < 10; y++) { for (int x = 0; x < 10; x++) { std::cout << m_tiles[(y * m_cols) + x]->getName(); std::cout << ","; } std::cout << std::endl; }*/ }