Exemplo n.º 1
0
// MAPPED
//
const char * Section::getComment(const char *name) const
{
	if(name)
	{
		std::string myname=name;
		KeyValue *mydata = d_kv_map[myname];
		if(mydata)
		{
			return mydata->getComment();
		}
	}
	return "";
}
Exemplo n.º 2
0
void Writer::visitKeyValue(const KeyValue& dataline) const
{
	if(dataline.isDeleted() && ( !d_commentchar || !d_preservedeleted))
	{
		// Don't preserve deleted data when comments are null
		// or we don't want to preserve them
		return;
	}
	else
	{
		string key = dataline.getName();
		string value = dataline.getValue();
		string comment = dataline.getComment();

		string commentchar(1,d_commentchar);
		

		if(dataline.isDeleted())
		{
			// print the comment character
			//
			*d_outputstream << d_commentchar << " ";
		}

		if(key != "")
		{


			int position = 0;
			size_t location;

			// escape all backslashes
			//
			while( (location = key.find("\\", position)) != string::npos )
			{
				// location points right at the '\'
				key.insert(location,  "\\");
				position = (int) location + 2;
			}


			// escape comment characters
			// unless the comment character is a backslash, which we've already escaped....
			//
			if(d_commentchar && d_commentchar != '\\')
			{
				position = 0;

				while( (location = key.find(commentchar, position)) != string::npos )
				{
					// location points right at the '"'
					key.insert(location,  "\\");
					position = (int) location + 2;
				}
			}

			// escape all delimiters, unless the delimiter is a backslash or the same as the comment, god forbid.
			//
			//
			if(d_delimiter != '\\' && d_delimiter != d_commentchar)
			{
				if(d_delimiter)
				{
					string delimiter(1, d_delimiter);
					position = 0;

					while( (location = key.find(delimiter, position)) != string::npos )
					{
						// location points right at the '"'
						key.insert(location,  "\\");
						position = (int) location + 2;
					}
				}
				else
				{
					position = 0;

					while( (location = key.find("\t", position)) != string::npos )
					{
						// location points right at the '"'
						key.insert(location,  "\\");
						position = (int) location + 2;
					}
					
					position = 0;

					while( (location = key.find(" ", position)) != string::npos )
					{
						// location points right at the '"'
						key.insert(location,  "\\");
						position = (int) location + 2;
					}					

				}				
			}

			// print the key
			//
			*d_outputstream << key;
		}

		if(value != "")
		{
			// print out the delimiter
			//
			*d_outputstream  << " " << ( d_delimiter ? d_delimiter : '\t' ) << " ";

			int position = 0;
			size_t location;

			// escape all backslashes
			//
			string backslash(1,'\\');

			while( (location = value.find("\\", position)) != string::npos )
			{
				// location points right at the '\'
				value.insert(location,  "\\");
				position = (int) location + 2;
			}

			// escape all quotes
			//
			string quote(1,'"');
			position = 0;

			while( (location = value.find("\"", position)) != string::npos )
			{
				// location points right at the '"'
				value.insert(location,  "\\");
				position = (int) location + 2;
			}

			// escape comment characters
			// unless the comment character is a backslash or a quote, which we've already escaped....
			//
			if(d_commentchar && d_commentchar != '\\' && d_commentchar != '"')
			{
				position = 0;

				while( (location = value.find(commentchar, position)) != string::npos )
				{
					// location points right at the '"'
					value.insert(location,  "\\");
					position = (int) location + 2;
				}
			}





			// if value starts with whitespace, ends with whitespace or contains the comment character or CRLF's, quote the value, 
			//
			int size = value.size();

			if(		isspace(value[0]) || 
						isspace(value[size-1]) || 
						(value.find("\r", 0) != string::npos) || 
						(value.find("\f", 0) != string::npos) || 
						(value.find("\n", 0)!= string::npos) 
			)
			{


				value.insert(0, "\"");
				value += "\"";
			}

			// if data is deleted and there are CRLFS
			// then each line must start with a comment character.
			// The safest way is just to follow every newline character with a comment
			//
			if(		dataline.isDeleted() && ( 
						(value.find("\r", 0) != string::npos) || 
						(value.find("\f", 0) != string::npos) || 
						(value.find("\n", 0)!= string::npos) )
			  )
			{	
				position = 0;				

				while( (location = value.find("\r", position)) != string::npos )
				{
					// location points right at the '"'
					value.insert(location + 1,  commentchar);
					position = (int) location + 2;
				}
				position = 0;				
				while( (location = value.find("\f", position)) != string::npos )
				{
					// location points right at the '"'
					value.insert(location + 1,  commentchar);
					position = (int) location + 2;
				}

				position = 0;				
				while( (location = value.find("\n", position)) != string::npos )
				{
					// location points right at the '"'
					value.insert(location + 1,  commentchar);
					position = (int) location + 2;
				}
			}			

			*d_outputstream << value;

		}

		if( comment != "" && d_commentchar && d_preservecomments)
		{
			// add value's comment at end if it exists
			// and there is a comment character
			//
					*d_outputstream << "\t " << d_commentchar << comment;
		}

		// end the entry with a newline
		//
		*d_outputstream << "\n";
	}
}