本文整理汇总了C++中stringT::substr方法的典型用法代码示例。如果您正苦于以下问题:C++ stringT::substr方法的具体用法?C++ stringT::substr怎么用?C++ stringT::substr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stringT
的用法示例。
在下文中一共展示了stringT::substr方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SetPreference
int CXMLprefs::SetPreference(const stringT &sPath, const stringT &sValue)
{
// Find the node specified by the path, creating it if it does not already exist
// and add the requested value.
// Notes:
// This routine only adds plain character data to the node, see comments at the end.
// If the node already has plain character data, it is replaced.
// If the node exists multiple times with the same path, only the first is altered -
// see description of function "first_element_by_path" in the pugixml manual.
int iRetVal = XML_SUCCESS;
// First see if the node already exists
pugi::xml_node node = m_pXMLDoc->first_element_by_path(sPath.c_str(), _T('\\'));
if (node == NULL) {
// Not there - let's build it
// Split up path and then add all nodes in the path (if they don't exist)
// Start at the top
node = m_pXMLDoc->root();
stringT::size_type pos, lastPos(0);
// Find first "non-delimiter".
pos = sPath.find_first_of(_T('\\'), lastPos);
// Get all nodes in the path, if they exist fine, if not, create them
while (pos != stringT::npos || lastPos != stringT::npos) {
// Retrieve next node name from path
stringT snode = sPath.substr(lastPos, pos - lastPos);
// Try to get it
pugi::xml_node child = node.child(snode.c_str());
// If not there, add it otherwise use it for the next iteration
node = (child == NULL) ? node.append_child(snode.c_str()) : child;
// Skip delimiter and find next "non-delimiter"
lastPos = sPath.find_first_not_of(_T('\\'), pos);
pos = sPath.find_first_of(_T('\\'), lastPos);
}
}
// ***** VERY IMPORTANT *****
// Note, as documented in the pugi manual under "Document object model/Tree Structure",
// nodes can be of various types. Element nodes found above, do not have a value.
// To add a value to an element node, one has to add a child node of type
// 'node_pcdata' (plain character data node) or 'node_cdata' (character data node),
// the latter using <![CDATA[[...]]> to encapsulate the data.
// If the node has data in its first pcdata child use it, otherwise add a pcdata child
pugi::xml_node prefnode = (node.first_child().type() == pugi::node_pcdata) ?
node.first_child() : node.append_child(pugi::node_pcdata);
if (!prefnode.set_value(sValue.c_str()))
iRetVal = XML_PUT_TEXT_FAILED;
return iRetVal;
}
示例2: splitpath
// In following, drive will be empty on non-Windows platforms
bool pws_os::splitpath(const stringT &path,
stringT &drive, stringT &dir,
stringT &file, stringT &ext)
{
if (path.empty())
return false;
drive = _T("");
stringT::size_type last_slash = path.find_last_of(_T("/"));
dir = path.substr(0, last_slash + 1);
stringT::size_type last_dot = path.find_last_of(_T("."));
if (last_dot != stringT::npos && last_dot > last_slash) {
file = path.substr(last_slash + 1, last_dot - last_slash - 1);
ext = path.substr(last_dot + 1);
} else {
file = path.substr(last_slash + 1);
ext = _T("");
}
return true;
}