File diff r27505:d725794d218c → r27506:5d6fb3346eaa
src/strings_internal.h
Show inline comments
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD 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 GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file strings_interal.h Types and functions related to the internal workings of formatting OpenTTD's strings. */
 

	
 
#ifndef STRINGS_INTERNAL_H
 
#define STRINGS_INTERNAL_H
 

	
 
#include "strings_func.h"
 

	
 
/**
 
 * Equivalent to the std::back_insert_iterator in function, with some
 
 * convenience helpers for string concatenation.
 
 *
 
 * The formatter is currently backed by an external char buffer, and has some
 
 * extra functions to ease the migration from char buffers to std::string.
 
 */
 
class StringBuilder {
 
	char *current; ///< The current location to add strings
 
	const char *last; ///< The last element of the buffer.
 

	
 
public:
 
	/* Required type for this to be an output_iterator; mimics std::back_insert_iterator. */
 
	using value_type = void;
 
	using difference_type = void;
 
	using iterator_category = std::output_iterator_tag;
 
	using pointer = void;
 
	using reference = void;
 

	
 
	/**
 
	 * Create the builder of an external buffer.
 
	 * @param start The start location to write to.
 
	 * @param last  The last location to write to.
 
	 */
 
@@ -109,25 +111,31 @@ public:
 
		*this->current = '\0';
 
		return this->current;
 
	}
 

	
 
	/**
 
	 * Get the remaining number of characters that can be placed.
 
	 * @return The number of characters.
 
	 */
 
	ptrdiff_t Remaining()
 
	{
 
		return (ptrdiff_t)(this->last - this->current);
 
	}
 

	
 
	/**
 
	 * Add a string using the strecpy/strecat-esque calling signature.
 
	 * @param function The function to pass the current and last location to,
 
	 *                 that will then return the new current location.
 
	 */
 
	void AddViaStreCallback(std::function<char*(char*, const char*)> function)
 
	{
 
		this->current = function(this->current, this->last);
 
	}
 
};
 

	
 
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters *args, uint case_index = 0, bool game_script = false);
 

	
 
/* Do not leak the StringBuilder to everywhere. */
 
void GetTownName(StringBuilder &builder, const struct Town *t);
 
void GRFTownNameGenerate(StringBuilder &builder, uint32 grfid, uint16 gen, uint32 seed);
 

	
 
#endif /* STRINGS_INTERNAL_H */