⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 helpformatter.cpp

📁 This software aims to create an applet and panel tools to manage a wireless interface card, such as
💻 CPP
字号:
//
// HelpFormatter.cpp
//
// $Id: //poco/Main/Util/src/HelpFormatter.cpp#2 $
//
// Copyright (c) 2004-2005, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
//    how to obtain complete source code for this software and any
//    accompanying software that uses this software.  The source code
//    must either be included in the distribution or be available for no
//    more than the cost of distribution plus a nominal fee, and must be
//    freely redistributable under reasonable conditions.  For an
//    executable file, complete source code means the source code for all
//    modules it contains.  It does not include source code for modules or
//    files that typically accompany the major components of the operating
//    system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//


#include "Util/HelpFormatter.h"
#include "Util/OptionSet.h"
#include "Util/Option.h"


Util_BEGIN


const int HelpFormatter::TAB_WIDTH  = 4;
const int HelpFormatter::LINE_WIDTH = 78;


HelpFormatter::HelpFormatter(const OptionSet& options): 
	_options(options),
	_width(LINE_WIDTH),
	_indent(calcIndent()),
	_unixStyle(true)
{
#if !defined(POCO_OS_FAMILY_UNIX)
	_unixStyle = false;
#endif
}


HelpFormatter::~HelpFormatter()
{
}


void HelpFormatter::setCommand(const std::string& command)
{
	_command = command;
}


void HelpFormatter::setUsage(const std::string& usage)
{
	_usage = usage;
}


void HelpFormatter::setHeader(const std::string& header)
{
	_header = header;
}


void HelpFormatter::setFooter(const std::string& footer)
{
	_footer = footer;
}


void HelpFormatter::format(std::ostream& ostr) const
{
	ostr << "usage: " << _command;
	if (!_usage.empty())
	{
		ostr << ' ';
		formatText(ostr, _usage, (int) _command.length() + 1);
	}
	ostr << '\n';
	if (!_header.empty())
	{
		formatText(ostr, _header, 0);
		ostr << "\n\n";
	}
	formatOptions(ostr);
	if (!_footer.empty())
	{
		ostr << '\n';
		formatText(ostr, _footer, 0);
		ostr << '\n';
	}
}


void HelpFormatter::setWidth(int width)
{
	poco_assert (width > 0);
	
	_width = width;
}


void HelpFormatter::setIndent(int indent)
{
	poco_assert (indent >= 0 && indent < _width);
	
	_indent = indent;
}


void HelpFormatter::setAutoIndent()
{
	_indent = calcIndent();
}


void HelpFormatter::setUnixStyle(bool flag)
{
	_unixStyle = flag;
}


int HelpFormatter::calcIndent() const
{
	int indent = 0;
	for (OptionSet::Iterator it = _options.begin(); it != _options.end(); ++it)
	{
		int shortLen = (int) it->shortName().length();
		int fullLen  = (int) it->fullName().length();
		int n = 0;
		if (_unixStyle && shortLen > 0)
		{
			n += shortLen + (int) shortPrefix().length();
			if (it->takesArgument())
				n += (int) it->argumentName().length() + (it->argumentRequired() ? 0 : 2);
			if (fullLen > 0) n += 2;
		}
		if (fullLen > 0)
		{
			n += fullLen + (int) longPrefix().length();
			if (it->takesArgument())
				n += 1 + (int) it->argumentName().length() + (it->argumentRequired() ? 0 : 2);
		}
		n += 2;
		if (n > indent)
			indent = n;
	}
	return indent;
}


void HelpFormatter::formatOptions(std::ostream& ostr) const
{
	int optWidth = calcIndent();
	for (OptionSet::Iterator it = _options.begin(); it != _options.end(); ++it)
	{
		formatOption(ostr, *it, optWidth);
		formatText(ostr, it->description(), _indent, optWidth);
		ostr << '\n';
	}
}


void HelpFormatter::formatOption(std::ostream& ostr, const Option& option, int width) const
{
	int shortLen = (int) option.shortName().length();
	int fullLen  = (int) option.fullName().length();

	int n = 0;
	if (_unixStyle && shortLen > 0)
	{
		ostr << shortPrefix() << option.shortName();
		n += (int) shortPrefix().length() + (int) option.shortName().length();
		if (option.takesArgument())
		{
			if (!option.argumentRequired()) { ostr << '['; ++n; }
			ostr << option.argumentName();
			n += (int) option.argumentName().length();
			if (!option.argumentRequired()) { ostr << ']'; ++n; }
		}
		if (fullLen > 0) { ostr << ", "; n += 2; }
	}
	if (fullLen > 0)
	{
		ostr << longPrefix() << option.fullName();
		n += (int) longPrefix().length() + (int) option.fullName().length();
		if (option.takesArgument())
		{
			if (!option.argumentRequired()) { ostr << '['; ++n; }
			ostr << '=';
			++n;
			ostr << option.argumentName();
			n += (int) option.argumentName().length();
			if (!option.argumentRequired()) { ostr << ']'; ++n; }
		}
	}
	while (n < width) { ostr << ' '; ++n; }
}


void HelpFormatter::formatText(std::ostream& ostr, const std::string& text, int indent) const
{
	formatText(ostr, text, indent, indent);
}


void HelpFormatter::formatText(std::ostream& ostr, const std::string& text, int indent, int firstIndent) const
{
	int pos = firstIndent;
	int maxWordLen = _width - indent;
	std::string word;
	for (std::string::const_iterator it = text.begin(); it != text.end(); ++it)
	{
		if (*it == '\n')
		{
			clearWord(ostr, pos, word, indent);
			ostr << '\n';
			pos = 0;
			while (pos < indent) { ostr << ' '; ++pos; }
		}
		else if (*it == '\t')
		{
			clearWord(ostr, pos, word, indent);
			if (pos < _width) ++pos;
			while (pos < _width && pos % TAB_WIDTH != 0)
			{
				ostr << ' ';
				++pos;
			}
		}
		else if (*it == ' ')
		{
			clearWord(ostr, pos, word, indent);
			if (pos < _width) { ostr << ' '; ++pos; }
		}
		else 
		{
			if (word.length() == maxWordLen)
			{
				clearWord(ostr, pos, word, indent);
			}
			else word += *it;
		}
	}
	clearWord(ostr, pos, word, indent);
}


void HelpFormatter::formatWord(std::ostream& ostr, int& pos, const std::string& word, int indent) const
{
	if (pos + word.length() > _width)
	{
		ostr << '\n';
		pos = 0;
		while (pos < indent) { ostr << ' '; ++pos; }
	}
	ostr << word;
	pos += (int) word.length();
}


void HelpFormatter::clearWord(std::ostream& ostr, int& pos, std::string& word, int indent) const
{
	formatWord(ostr, pos, word, indent);
	word.clear();
}


std::string HelpFormatter::shortPrefix() const
{
#if defined(POCO_OS_FAMILY_UNIX)
	return "-";
#else
	return _unixStyle ? "-" : "/";
#endif
}


std::string HelpFormatter::longPrefix() const
{
#if defined(POCO_OS_FAMILY_UNIX)
	return "--";
#else
	return _unixStyle ? "--" : "/";
#endif
}


Util_END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -