/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #include #include #include #include #include using namespace osg; ApplicationUsage::ApplicationUsage(const std::string& commandLineUsage): _commandLineUsage(commandLineUsage) { } ApplicationUsage* ApplicationUsage::instance() { static osg::ref_ptr s_applicationUsage = new ApplicationUsage; return s_applicationUsage.get(); } void ApplicationUsage::addUsageExplanation(Type type,const std::string& option,const std::string& explanation) { switch(type) { case(COMMAND_LINE_OPTION): addCommandLineOption(option,explanation); break; case(ENVIRONMENTAL_VARIABLE): addEnvironmentalVariable(option,explanation); break; case(KEYBOARD_MOUSE_BINDING): addKeyboardMouseBinding(option,explanation); break; } } void ApplicationUsage::addCommandLineOption(const std::string& option,const std::string& explanation,const std::string& defaultValue) { _commandLineOptions[option]=explanation; _commandLineOptionsDefaults[option]=defaultValue; } void ApplicationUsage::addEnvironmentalVariable(const std::string& option,const std::string& explanation, const std::string& defaultValue) { _environmentalVariables[option]=explanation; _environmentalVariablesDefaults[option]=defaultValue; } void ApplicationUsage::addKeyboardMouseBinding(const std::string& option,const std::string& explanation) { _keyboardMouse[option]=explanation; } void ApplicationUsage::getFormattedString(std::string& str, const UsageMap& um,unsigned int widthOfOutput,bool showDefaults,const UsageMap& ud) { unsigned int maxNumCharsInOptions = 0; ApplicationUsage::UsageMap::const_iterator citr; for(citr=um.begin(); citr!=um.end(); ++citr) { maxNumCharsInOptions = maximum(maxNumCharsInOptions,(unsigned int)citr->first.length()); } unsigned int fullWidth = widthOfOutput; unsigned int optionPos = 2; unsigned int explanationPos = optionPos+maxNumCharsInOptions+2; unsigned int defaultPos = 0; if (showDefaults) { defaultPos = explanationPos; explanationPos = optionPos+8; } unsigned int explanationWidth = fullWidth-explanationPos; std::string line; for(citr=um.begin(); citr!=um.end(); ++citr) { line.assign(fullWidth,' '); line.replace(optionPos,citr->first.length(),citr->first); if (showDefaults) { UsageMap::const_iterator ditr = ud.find(citr->first); if (ditr != ud.end()) { line.replace(defaultPos, std::string::npos, ""); if (ditr->second != "") { line += "["; line += ditr->second; line += "]"; } str += line; str += "\n"; line.assign(fullWidth,' '); } } const std::string& explanation = citr->second; std::string::size_type pos = 0; std::string::size_type offset = 0; bool firstInLine = true; if (!explanation.empty()) { while (pos0 && explanation[pos+width]!=' ' && explanation[pos+width]!='\n') --width; if (width==0) { // word must be longer than a whole line so will need // to concatinate it. width = explanationWidth-1; concatinated = true; } } line.replace(explanationPos+offset,explanationWidth, explanation, pos, width); if (concatinated) { str += line; str += "-\n"; } else { str += line; str += "\n"; } // move to the next line of output. line.assign(fullWidth,' '); pos += width+extraSkip; } } else { str += line; str += "\n"; } } } void ApplicationUsage::write(std::ostream& output, const ApplicationUsage::UsageMap& um,unsigned int widthOfOutput,bool showDefaults,const ApplicationUsage::UsageMap& ud) { std::string str; getFormattedString(str, um, widthOfOutput, showDefaults, ud); output << str << std::endl; } void ApplicationUsage::write(std::ostream& output, unsigned int type, unsigned int widthOfOutput, bool showDefaults) { output << "Usage: "<first.find_first_of("\n\r\t "); if (len == std::string::npos) len = citr->first.size(); maxNumCharsInOptions = maximum( maxNumCharsInOptions,static_cast(len)); } unsigned int optionPos = 2; std::string line; for(citr=getEnvironmentalVariables().begin(); citr!=getEnvironmentalVariables().end(); ++citr) { line.assign(optionPos+maxNumCharsInOptions+2,' '); std::string::size_type len = citr->first.find_first_of("\n\r\t "); if (len == std::string::npos) len = citr->first.size(); line.replace(optionPos,len,citr->first.substr(0,len)); const char *cp = getenv(citr->first.substr(0, len).c_str()); if (!cp) cp = "[not set]"; else if (!*cp) cp = "[set]"; line += std::string(cp) + "\n"; output << line; } output << std::endl; }