/// \brief Text-stream interfaces.
#include <cstddef>
+#include <util/buffer.h>
#include "generic/static.h"
/// \brief A read-only character-stream.
return ostream_write( ostream, t );
}
+inline TextOutputStream& ostream_write( TextOutputStream& ostream, const u::Buffer& b ){
+ return ostream << b.c_str();
+}
+
class NullOutputStream : public TextOutputStream
{
public:
add_subdirectory(stream)
add_subdirectory(string)
add_subdirectory(uilib)
+add_subdirectory(util)
add_subdirectory(xml)
add_library(libs
--- /dev/null
+add_library(util
+ buffer.cpp
+ )
--- /dev/null
+#include "buffer.h"
--- /dev/null
+#ifndef INCLUDED_BUFFER_H
+#define INCLUDED_BUFFER_H
+
+namespace u {
+
+ using byte = char;
+
+ using cstring = const char *;
+
+ namespace details {
+ struct Reference {
+ private:
+ int m_size;
+ byte *m_data;
+ public:
+ Reference(int m_size, byte *m_data)
+ : m_size(m_size), m_data(m_data) {}
+
+ int size() const {
+ return m_size;
+ }
+
+ const byte *data() const {
+ return m_data;
+ }
+
+ byte *data() {
+ return m_data;
+ }
+ };
+
+ template<int sz>
+ struct Value {
+ byte m_data[sz];
+
+ int size() const {
+ return sz;
+ }
+
+ const byte *data() const {
+ return m_data;
+ }
+
+ byte *data() {
+ return m_data;
+ }
+ };
+ }
+
+ template<class Self>
+ class IBuffer : Self {
+ using Self::Self;
+ public:
+ operator const IBuffer<details::Reference>() const {
+ return IBuffer<details::Reference>{this->size(), const_cast<byte *>(this->data())};
+ }
+
+ cstring c_str() const {
+ return this->data();
+ }
+
+ operator byte *() {
+ return this->data();
+ }
+ };
+
+ template<int sz>
+ using BufferVal = IBuffer<details::Value<sz>>;
+
+ using Buffer = IBuffer<details::Reference>;
+
+ template<int sz>
+ BufferVal<sz> buffer() {
+ return BufferVal<sz>();
+ }
+
+}
+
+#endif
#if !defined( INCLUDED_BRUSHXML_H )
#define INCLUDED_BRUSHXML_H
+#include <util/buffer.h>
#include "stream/stringstream.h"
#include "xml/xmlelement.h"
inline void FacePolygon_exportXML( const Winding& w, const BasicVector3<double>& normal, XMLImporter& importer ){
DynamicElement element( "polygon" );
- char tmp[32];
+ auto tmp = u::buffer<32>();
sprintf( tmp, "%f", normal.x() );
element.insertAttribute( "nx", tmp );
#include <uilib/uilib.h>
#include <gdk/gdkkeysyms.h>
+#include <util/buffer.h>
#include "gtkutil/dialog.h"
#include "mainframe.h"
ReadCommandMap( const char* filename ) : m_filename( filename ), m_count( 0 ){
}
void visit( const char* name, Accelerator& accelerator ){
- char value[1024];
+ auto value = u::buffer<1024>();
if ( read_var( m_filename, "Commands", name, value ) ) {
if ( string_empty( value ) ) {
accelerator.key = 0;
Version dataVersion = { 0, 0 };
{
- char value[1024];
+ auto value = u::buffer<1024>();
if ( read_var( strINI.c_str(), "Version", "number", value ) ) {
dataVersion = version_parse( value );
}
#include "debugging/debugging.h"
#include <map>
+#include <util/buffer.h>
#include "ifilesystem.h"
typedef std::map<const char*, EntityClass*, RawStringLessNoCase> EntityClasses;
EntityClasses g_entityClasses;
EntityClass *eclass_bad = 0;
-char eclass_directory[1024];
+u::BufferVal<1024> eclass_directory;
typedef std::map<CopiedString, ListAttributeType> ListAttributeTypes;
ListAttributeTypes g_listTypes;
}
#include "stream/textfilestream.h"
#include "modulesystem/moduleregistry.h"
#include "os/path.h"
+#include <util/buffer.h>
const char* EClass_GetExtension(){
return "def";
#include <stdlib.h>
-char com_token[1024];
+u::BufferVal<1024> com_token;
bool com_eof;
/*
}
}
- char parms[256];
+ auto parms = u::buffer<256>();
// get the flags
{
// copy to the first /n
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <util/buffer.h>
#include "entity.h"
#include "ientity.h"
if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
g_iLastLightIntensity = intensity;
- char buf[30];
+ auto buf = u::buffer<30>();
sprintf( buf, "255 255 255 %d", intensity );
Node_getEntity( node )->setKeyValue( "_light", buf );
}
if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
g_iLastLightIntensity = intensity;
- char buf[10];
+ auto buf = u::buffer<10>();
sprintf( buf, "%d", intensity );
Node_getEntity( node )->setKeyValue( "light", buf );
}
g_entity_globals.color_entity = rgb;
NormalizeColor( g_entity_globals.color_entity );
- char buffer[128];
+ auto buffer = u::buffer<128>();
sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
g_entity_globals.color_entity[1],
g_entity_globals.color_entity[2] );
NormalizeColor( g_entity_globals.color_entity );
}
- char buffer[128];
+ auto buffer = u::buffer<128>();
sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
g_entity_globals.color_entity[1],
g_entity_globals.color_entity[2] );
#include <set>
#include <gdk/gdkkeysyms.h>
#include <uilib/uilib.h>
+#include <util/buffer.h>
#include "os/path.h"
void EntityInspector_applySpawnflags(){
int f, i, v;
- char sz[32];
+ auto sz = u::buffer<32>();
f = 0;
for ( i = 0; i < g_spawnflag_count; ++i )
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <util/buffer.h>
#include "environment.h"
#include "stream/textstream.h"
const char *gamedetect_argv_buffer[1024];
void gamedetect_found_game( const char *game, char *path ){
int argc;
- static char buf[128];
+ static auto buf = u::buffer<128>();
if ( g_argv == gamedetect_argv_buffer ) {
return;
++i;
}
if ( !nogamedetect ) {
- static char buf[1024 + 64];
+ static auto buf = u::buffer<1024 + 64>();
strncpy( buf, environment_get_app_path(), sizeof( buf ) );
buf[sizeof( buf ) - 1 - 64] = 0;
if ( !strlen( buf ) ) {
{
// get path to the editor
- char filename[MAX_PATH + 1];
+ auto filename = u::buffer<MAX_PATH + 1>();
GetModuleFileName( 0, filename, MAX_PATH );
char* last_separator = strrchr( filename, '\\' );
if ( last_separator != 0 ) {
#else
#include <errno.h>
#include <unistd.h>
+#include <util/buffer.h>
+
#endif
void Error( const char *error, ... ){
va_list argptr;
- char text[4096];
+ auto text = u::buffer<4096>();
va_start( argptr,error );
vsprintf( text, error,argptr );
#include "os/path.h"
#include <vector>
#include <map>
+#include <util/buffer.h>
class RadiantFileTypeRegistry : public IFileTypeRegistry
{
const char* findModuleName( IFileTypeRegistry* registry, const char* moduleType, const char* extension ){
class SearchFileTypeList : public IFileTypeList
{
- char m_pattern[128];
+ u::BufferVal<128> m_pattern;
const char* m_moduleName;
public:
SearchFileTypeList( const char* ext )
#include <gdk/gdkkeysyms.h>
#include <uilib/uilib.h>
+#include <util/buffer.h>
#include "os/path.h"
#include "math/aabb.h"
}
// Initialize with last used values
- char buf[16];
-
+ auto buf = u::buffer<16>();
+
sprintf( buf, "%f", last_used_texture_layout_scale_x );
x.text( buf );
}
}
- char buf[16];
+ auto buf = u::buffer<16>();
sprintf( buf, "%d", *intensity );
intensity_entry.text(buf);
#include "referencecache.h"
#include "stacktrace.h"
+#include <util/buffer.h>
+
#ifdef WIN32
#include <windows.h>
#endif
void error_redirect( const gchar *domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data ){
gboolean in_recursion;
gboolean is_fatal;
- char buf[256];
+ auto buf = u::buffer<256>();
in_recursion = ( log_level & G_LOG_FLAG_RECURSION ) != 0;
is_fatal = ( log_level & G_LOG_FLAG_FATAL ) != 0;
bool check_version_file( const char* filename, const char* version ){
TextFileInputStream file( filename );
if ( !file.failed() ) {
- char buf[10];
+ auto buf = u::buffer<10>();
buf[file.read( buf, 9 )] = '\0';
// chomp it (the hard way)
#include <ctime>
#include <gdk/gdkkeysyms.h>
+#include <util/buffer.h>
#include "cmdlib.h"
CLoadModule( const char* path ) : m_path( path ){
}
void operator()( const char* name ) const {
- char fullname[1024];
+ auto fullname = u::buffer<1024>();
ASSERT_MESSAGE( strlen( m_path ) + strlen( name ) < 1024, "" );
strcpy( fullname, m_path );
strcat( fullname, name );
#include <set>
#include <gdk/gdkkeysyms.h>
+#include <util/buffer.h>
#include "uilib/uilib.h"
#include "scenelib.h"
}
void makeUnique( const char* name, const NameCallback& setName ) const {
- char buffer[1024];
+ auto buffer = u::buffer<1024>();
name_write( buffer, m_uniqueNames.make_unique( name_read( name ) ) );
setName( buffer );
}
name_t uniqueName( uniqueNames.make_unique( name_read( ( *i ).first.c_str() ) ) );
uniqueNames.insert( uniqueName );
- char buffer[1024];
+ auto buffer = u::buffer<1024>();
name_write( buffer, uniqueName );
//globalOutputStream() << "renaming " << makeQuoted((*i).first.c_str()) << " to " << makeQuoted(buffer) << "\n";
for ( EntityBreakdown::iterator i = entitymap.begin(); i != entitymap.end(); ++i )
{
- char tmp[16];
+ auto tmp = u::buffer<16>();
sprintf( tmp, "%u", Unsigned( ( *i ).second ) );
GtkTreeIter iter;
gtk_list_store_append( GTK_LIST_STORE( EntityBreakdownWalker ), &iter );
EntityBreakdownWalker.unref();
- char tmp[16];
+ auto tmp = u::buffer<16>();
sprintf( tmp, "%u", Unsigned( g_brushCount.get() ) );
brushes_entry.text(tmp);
sprintf( tmp, "%u", Unsigned( g_entityCount.get() ) );
}
// write the info_playerstart
- char sTmp[1024];
+ auto sTmp = u::buffer<1024>();
sprintf( sTmp, "%d %d %d", (int)vOrig[0], (int)vOrig[1], (int)vOrig[2] );
Node_getEntity( *startpoint )->setKeyValue( "origin", sTmp );
sprintf( sTmp, "%d", (int)Camera_getAngles( *g_pParentWnd->GetCamWnd() )[CAMERA_YAW] );
}
// Initialize dialog
- char buf[16];
+ auto buf = u::buffer<16>();
int ent, br;
GetSelectionIndex( &ent, &br );
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>
+#include <util/buffer.h>
#include "os/file.h"
#include "generic/callback.h"
}
void MRU_Activate( std::size_t index ){
- char text[1024];
+ auto text = u::buffer<1024>();
strcpy( text, MRU_GetText( index ) );
if ( file_readable( text ) ) { //\todo Test 'map load succeeds' instead of 'file is readable'.
#include <set>
#include <limits>
+#include <util/buffer.h>
#include "math/frustum.h"
#include "string/string.h"
}
{
- char width[16], height[16];
+ auto width = u::buffer<16>();
+ auto height = u::buffer<16>();
sprintf( width, "%u", Unsigned( m_width ) );
sprintf( height, "%u", Unsigned( m_height ) );
StaticElement element( "matrix" );
m_countRows = m_Patch->getHeight();
for ( std::size_t i = 0; i < m_countRows; ++i )
{
- char buffer[16];
+ auto buffer = u::buffer<16>();
sprintf( buffer, "%u", Unsigned( i ) );
gtk_combo_box_text_append_text( m_pRowCombo, buffer );
}
m_countCols = m_Patch->getWidth();
for ( std::size_t i = 0; i < m_countCols; ++i )
{
- char buffer[16];
+ auto buffer = u::buffer<16>();
sprintf( buffer, "%u", Unsigned( i ) );
gtk_combo_box_text_append_text( m_pColCombo, buffer );
}
#include <map>
#include <uilib/uilib.h>
+#include <util/buffer.h>
#include "stream/textfilestream.h"
#include "cmdlib.h"
int g_numentities = 0;
void QE_UpdateStatusBar(){
- char buffer[128];
+ auto buffer = u::buffer<128>();
sprintf( buffer, "Brushes: %d Entities: %d", g_numbrushes, g_numentities );
g_pParentWnd->SetStatusText( g_pParentWnd->m_brushcount_status, buffer );
}
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <util/buffer.h>
#include "textures.h"
#include "debugging/debugging.h"
-byte g_gammatable[256];
+u::BufferVal<256> g_gammatable;
void ResampleGamma( float fGamma ){
int i,inf;
if ( fGamma == 1.0 ) {
int r,g,b;
int i;
int inf;
- byte gammatable[256];
+ auto gammatable = u::buffer<256>();
float gamma;
gamma = g_texture_globals.fGamma;
#include <vector>
#include <uilib/uilib.h>
+#include <util/buffer.h>
#include "signal/signal.h"
#include "math/vector.h"
GtkTreeModel* model = gtk_tree_view_get_model( GTK_TREE_VIEW( treeview ) );
if ( gtk_tree_model_get_iter( model, &iter, path ) ) {
- gchar dirName[1024];
+ auto dirName = u::buffer<1024>();
gchar* buffer;
gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
GSList* selected = NULL;
GSList* node;
gchar* tag;
- char buffer[256];
- char tags_searched[256];
+ auto buffer = u::buffer<256>();
+ auto tags_searched = u::buffer<256>();
GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( g_TextureBrowser.m_treeViewTags ) );
GtkTreeIter iter;
if ( gtk_tree_selection_get_selected (selection, &model, &iter) )
{
- gchar dirName[1024];
+ auto dirName = u::buffer<1024>();
gchar* buffer;
gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
#if defined( DEBUG_CULLING )
#include <stdio.h>
+#include <util/buffer.h>
-char g_cull_stats[1024];
+u::BufferVal<1024> g_cull_stats;
int g_count_dots;
int g_count_planes;
int g_count_oriented_planes;
#include <glib.h>
#include <uilib/uilib.h>
+#include <util/buffer.h>
#include "xmlstuff.h"
class CWatchBSP
}
static void saxWarning( void *ctx, const char *msg, ... ){
- char saxMsgBuffer[4096];
+ auto saxMsgBuffer = u::buffer<4096>();
va_list args;
va_start( args, msg );
}
static void saxError( void *ctx, const char *msg, ... ){
- char saxMsgBuffer[4096];
+ auto saxMsgBuffer = u::buffer<4096>();
va_list args;
va_start( args, msg );
}
static void saxFatal( void *ctx, const char *msg, ... ){
- char buffer[4096];
+ auto buffer = u::buffer<4096>();
va_list args;
#include <uilib/uilib.h>
#include <gdk/gdkkeysyms.h>
+#include <util/buffer.h>
#include "generic/callback.h"
#include "string/string.h"
globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
}
- char fileNameWithoutExt[512];
+ auto fileNameWithoutExt = u::buffer<512>();
strncpy( fileNameWithoutExt, relative, sizeof( fileNameWithoutExt ) - 1 );
fileNameWithoutExt[512 - 1] = '\0';
fileNameWithoutExt[strlen( fileNameWithoutExt ) - 4] = '\0';
void XYWnd::XY_DrawGrid( void ) {
float x, y, xb, xe, yb, ye;
float w, h, a;
- char text[32];
+ auto text = u::buffer<32>();
float step, minor_step, stepx, stepy;
step = minor_step = stepx = stepy = GetGridSize();
float x, y, xb, xe, yb, ye;
float w, h;
- char text[32];
+ auto text = u::buffer<32>();
glDisable( GL_TEXTURE_2D );
glDisable( GL_TEXTURE_1D );