#ifndef INCLUDED_BUFFER_H
#define INCLUDED_BUFFER_H
+#include <cstdarg>
+#include <cstdio>
+#include <cstring>
+
namespace u {
using byte = char;
return this->data();
}
- operator byte *() {
+ operator cstring() const {
+ return c_str();
+ }
+
+ std::size_t strlen() const {
+ return ::strlen(c_str());
+ }
+
+ byte *mut() {
return this->data();
}
+
+// operator byte *() {
+// return mut();
+// }
+
+ void terminate(long offset = -1) {
+ if (offset < 0) {
+ offset += this->size();
+ }
+ mut()[offset] = '\0';
+ }
+
+ void copy(cstring str, unsigned int offset = 0, unsigned int limit = 0) {
+ if (!limit) {
+ limit = this->size() - offset;
+ }
+ strncpy(mut() + offset, str, limit);
+ }
+
+ __attribute__((format(printf, 2, 3)))
+ void sprintf(const char *format, ...) {
+ // todo: check for overflow
+ va_list argptr;
+ va_start(argptr, format);
+ vsprintf(this->data(), format, argptr);
+ va_end(argptr);
+ }
};
template<int sz>
auto tmp = u::buffer<32>();
- sprintf( tmp, "%f", normal.x() );
+ tmp.sprintf( "%f", normal.x() );
element.insertAttribute( "nx", tmp );
- sprintf( tmp, "%f", normal.y() );
+ tmp.sprintf( "%f", normal.y() );
element.insertAttribute( "ny", tmp );
- sprintf( tmp, "%f", normal.z() );
+ tmp.sprintf( "%f", normal.z() );
element.insertAttribute( "nz", tmp );
importer.pushElement( element );
{
DynamicElement c( "vertex" );
- sprintf( tmp, "%f", w.points[i].vertex.x() );
+ tmp.sprintf( "%f", w.points[i].vertex.x() );
c.insertAttribute( "x", tmp );
- sprintf( tmp, "%f", w.points[i].vertex.y() );
+ tmp.sprintf( "%f", w.points[i].vertex.y() );
c.insertAttribute( "y", tmp );
- sprintf( tmp, "%f", w.points[i].vertex.z() );
+ tmp.sprintf( "%f", w.points[i].vertex.z() );
c.insertAttribute( "z", tmp );
- sprintf( tmp, "%f", w.points[i].texcoord.x() );
+ tmp.sprintf( "%f", w.points[i].texcoord.x() );
c.insertAttribute( "s", tmp );
- sprintf( tmp, "%f", w.points[i].texcoord.y() );
+ tmp.sprintf( "%f", w.points[i].texcoord.y() );
c.insertAttribute( "t", tmp );
importer.pushElement( c );
}
void visit( const char* name, Accelerator& accelerator ){
auto value = u::buffer<1024>();
- if ( read_var( m_filename, "Commands", name, value ) ) {
+ if ( read_var( m_filename, "Commands", name, value.mut() ) ) {
if ( string_empty( value ) ) {
accelerator.key = 0;
accelerator.modifiers = (GdkModifierType)0;
{
auto value = u::buffer<1024>();
- if ( read_var( strINI.c_str(), "Version", "number", value ) ) {
+ if ( read_var( strINI.c_str(), "Version", "number", value.mut() ) ) {
dataVersion = version_parse( value );
}
}
*/
const char *COM_Parse( const char *data ){
int c;
- int len;
- len = 0;
- com_token[0] = 0;
+ int len = 0;
+ auto buf = com_token.mut();
+ buf[0] = 0;
if ( !data ) {
return 0;
{
c = *data++;
if ( c == '\"' ) {
- com_token[len] = 0;
+ buf[len] = 0;
return data;
}
- com_token[len] = c;
+ buf[len] = c;
len++;
} while ( 1 );
}
// parse single characters
if ( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' ) {
- com_token[len] = c;
+ buf[len] = c;
len++;
- com_token[len] = 0;
+ buf[len] = 0;
return data + 1;
}
// parse a regular word
do
{
- com_token[len] = c;
+ buf[len] = c;
data++;
len++;
c = *data;
}
} while ( c > 32 );
- com_token[len] = 0;
+ buf[len] = 0;
return data;
}
// get the flags
{
// copy to the first /n
- char* p = parms;
+ char* p = parms.mut();
while ( *text && *text != '\n' )
*p++ = *text++;
*p = 0;
if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
g_iLastLightIntensity = intensity;
auto buf = u::buffer<30>();
- sprintf( buf, "255 255 255 %d", intensity );
+ buf.sprintf( "255 255 255 %d", intensity );
Node_getEntity( node )->setKeyValue( "_light", buf );
}
}
if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
g_iLastLightIntensity = intensity;
auto buf = u::buffer<10>();
- sprintf( buf, "%d", intensity );
+ buf.sprintf( "%d", intensity );
Node_getEntity( node )->setKeyValue( "light", buf );
}
}
NormalizeColor( g_entity_globals.color_entity );
auto buffer = u::buffer<128>();
- sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
+ buffer.sprintf( "%g %g %g", g_entity_globals.color_entity[0],
g_entity_globals.color_entity[1],
g_entity_globals.color_entity[2] );
}
auto buffer = u::buffer<128>();
- sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
+ buffer.sprintf( "%g %g %g", g_entity_globals.color_entity[0],
g_entity_globals.color_entity[1],
g_entity_globals.color_entity[2] );
f |= v << spawn_table[i];
}
- sprintf( sz, "%i", f );
+ sz.sprintf( "%i", f );
const char* value = ( f == 0 ) ? "" : sz;
{
globalOutputStream() << "Detected game " << game << " in " << path << "\n";
- sprintf( buf, "-%s-EnginePath", game );
+ buf.sprintf( "-%s-EnginePath", game );
argc = 0;
gamedetect_argv_buffer[argc++] = "-global-gamefile";
gamedetect_argv_buffer[argc++] = game;
}
if ( !nogamedetect ) {
static auto buf = u::buffer<1024 + 64>();
- strncpy( buf, environment_get_app_path(), sizeof( buf ) );
- buf[sizeof( buf ) - 1 - 64] = 0;
- if ( !strlen( buf ) ) {
+ buf.copy(environment_get_app_path());
+ buf.terminate(-64);
+ if ( !buf.strlen() ) {
return;
}
- char *p = buf + strlen( buf ) - 1; // point directly on the slash of get_app_path
+ char *p = buf.mut() + buf.strlen() - 1; // point directly on the slash of get_app_path
while ( p != buf )
{
// TODO add more games to this
#elif defined( __APPLE__ )
if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "Nexuiz.app/Contents/Info.plist", buf, p - buf ) )
#else
- if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf, p - buf ) )
+ if ( gamedetect_check_game( "nexuiz.game", "data/common-spog.pk3", "nexuiz-linux-glx.sh", buf.mut(), p - buf.c_str() ) )
#endif
{ return; }
// try to detect Quetoo installs
- if ( gamedetect_check_game( "quetoo.game", "default/icons/quetoo.png", NULL, buf, p - buf ) ) {
+ if ( gamedetect_check_game( "quetoo.game", "default/icons/quetoo.png", NULL, buf.mut(), p - buf.c_str() ) ) {
return;
}
// try to detect Warsow installs
- if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf, p - buf ) ) {
+ if ( gamedetect_check_game( "warsow.game", "basewsw/dedicated_autoexec.cfg", NULL, buf.mut(), p - buf.c_str() ) ) {
return;
}
auto text = u::buffer<4096>();
va_start( argptr,error );
- vsprintf( text, error,argptr );
+ vsprintf( text.mut(), error,argptr );
va_end( argptr );
- strcat( text, "\n" );
+ strcat( text.mut(), "\n" );
#ifdef WIN32
if ( GetLastError() != 0 ) {
}
#else
if ( errno != 0 ) {
- strcat( text, "errno: " );
- strcat( text, strerror( errno ) );
- strcat( text, "\n" );
+ strcat( text.mut(), "errno: " );
+ strcat( text.mut(), strerror( errno ) );
+ strcat( text.mut(), "\n" );
}
#endif
}
#endif
- strcat( text, "An unrecoverable error has occured.\n" );
+ strcat( text.mut(), "An unrecoverable error has occured.\n" );
ERROR_MESSAGE( text );
public:
SearchFileTypeList( const char* ext )
: m_moduleName( "" ){
- m_pattern[0] = '*';
- m_pattern[1] = '.';
- strncpy( m_pattern + 2, ext, 125 );
- m_pattern[127] = '\0';
+ m_pattern.mut()[0] = '*';
+ m_pattern.mut()[1] = '.';
+ m_pattern.copy(ext, 2);
+ m_pattern.terminate();
}
void addType( const char* moduleName, filetype_t type ){
if ( extension_equal( m_pattern, type.pattern ) ) {
// Initialize with last used values
auto buf = u::buffer<16>();
- sprintf( buf, "%f", last_used_texture_layout_scale_x );
+ buf.sprintf( "%f", last_used_texture_layout_scale_x );
x.text( buf );
-
- sprintf( buf, "%f", last_used_texture_layout_scale_y );
+
+ buf.sprintf( "%f", last_used_texture_layout_scale_y );
y.text( buf );
// Set focus after intializing the values
}
auto buf = u::buffer<16>();
- sprintf( buf, "%d", *intensity );
+ buf.sprintf( "%d", *intensity );
intensity_entry.text(buf);
EMessageBoxReturn ret = modal_dialog_show( window, dialog );
}
if ( domain ) {
- strcpy( buf, domain );
+ buf.copy(domain);
}
else{
- strcpy( buf, "**" );
+ buf.copy("**");
}
- strcat( buf, "-" );
+ strcat( buf.mut(), "-" );
switch ( log_level )
{
case G_LOG_LEVEL_ERROR:
if ( in_recursion ) {
- strcat( buf, "ERROR (recursed) **: " );
+ strcat( buf.mut(), "ERROR (recursed) **: " );
}
else{
- strcat( buf, "ERROR **: " );
+ strcat( buf.mut(), "ERROR **: " );
}
break;
case G_LOG_LEVEL_CRITICAL:
if ( in_recursion ) {
- strcat( buf, "CRITICAL (recursed) **: " );
+ strcat( buf.mut(), "CRITICAL (recursed) **: " );
}
else{
- strcat( buf, "CRITICAL **: " );
+ strcat( buf.mut(), "CRITICAL **: " );
}
break;
case G_LOG_LEVEL_WARNING:
if ( in_recursion ) {
- strcat( buf, "WARNING (recursed) **: " );
+ strcat( buf.mut(), "WARNING (recursed) **: " );
}
else{
- strcat( buf, "WARNING **: " );
+ strcat( buf.mut(), "WARNING **: " );
}
break;
case G_LOG_LEVEL_MESSAGE:
if ( in_recursion ) {
- strcat( buf, "Message (recursed): " );
+ strcat( buf.mut(), "Message (recursed): " );
}
else{
- strcat( buf, "Message: " );
+ strcat( buf.mut(), "Message: " );
}
break;
case G_LOG_LEVEL_INFO:
if ( in_recursion ) {
- strcat( buf, "INFO (recursed): " );
+ strcat( buf.mut(), "INFO (recursed): " );
}
else{
- strcat( buf, "INFO: " );
+ strcat( buf.mut(), "INFO: " );
}
break;
case G_LOG_LEVEL_DEBUG:
if ( in_recursion ) {
- strcat( buf, "DEBUG (recursed): " );
+ strcat( buf.mut(), "DEBUG (recursed): " );
}
else{
- strcat( buf, "DEBUG: " );
+ strcat( buf.mut(), "DEBUG: " );
}
break;
default:
* try to make the best out of it.
*/
if ( in_recursion ) {
- strcat( buf, "LOG (recursed:" );
+ strcat( buf.mut(), "LOG (recursed:" );
}
else{
- strcat( buf, "LOG (" );
+ strcat( buf.mut(), "LOG (" );
}
if ( log_level ) {
gchar string[] = "0x00): ";
*p += 'A' - '9' - 1;
}
- strcat( buf, string );
+ strcat( buf.mut(), string );
}
else{
- strcat( buf, "): " );
+ strcat( buf.mut(), "): " );
}
}
- strcat( buf, message );
+ strcat( buf.mut(), message );
if ( is_fatal ) {
- strcat( buf, "\naborting...\n" );
+ strcat( buf.mut(), "\naborting...\n" );
}
else{
- strcat( buf, "\n" );
+ strcat( buf.mut(), "\n" );
}
// spam it...
TextFileInputStream file( filename );
if ( !file.failed() ) {
auto buf = u::buffer<10>();
- buf[file.read( buf, 9 )] = '\0';
+ auto mut_buf = buf.mut();
+ mut_buf[file.read(mut_buf, 9 )] = '\0';
// chomp it (the hard way)
int chomp = 0;
- while ( buf[chomp] >= '0' && buf[chomp] <= '9' )
+ while (mut_buf[chomp] >= '0' && mut_buf[chomp] <= '9' )
chomp++;
- buf[chomp] = '\0';
+ mut_buf[chomp] = '\0';
- return string_equal( buf, version );
+ return string_equal(mut_buf, version );
}
return false;
}
void operator()( const char* name ) const {
auto fullname = u::buffer<1024>();
ASSERT_MESSAGE( strlen( m_path ) + strlen( name ) < 1024, "" );
- strcpy( fullname, m_path );
- strcat( fullname, name );
+ fullname.copy(m_path);
+ strcat( fullname.mut(), name );
globalOutputStream() << "Found '" << fullname << "'\n";
GlobalModuleServer_loadModule( fullname );
}
void makeUnique( const char* name, const NameCallback& setName ) const {
auto buffer = u::buffer<1024>();
- name_write( buffer, m_uniqueNames.make_unique( name_read( name ) ) );
+ name_write( buffer.mut(), m_uniqueNames.make_unique( name_read( name ) ) );
setName( buffer );
}
uniqueNames.insert( uniqueName );
auto buffer = u::buffer<1024>();
- name_write( buffer, uniqueName );
+ name_write( buffer.mut(), uniqueName );
//globalOutputStream() << "renaming " << makeQuoted((*i).first.c_str()) << " to " << makeQuoted(buffer) << "\n";
for ( EntityBreakdown::iterator i = entitymap.begin(); i != entitymap.end(); ++i )
{
auto tmp = u::buffer<16>();
- sprintf( tmp, "%u", Unsigned( ( *i ).second ) );
+ tmp.sprintf( "%u", Unsigned( ( *i ).second ) );
GtkTreeIter iter;
gtk_list_store_append( GTK_LIST_STORE( EntityBreakdownWalker ), &iter );
gtk_list_store_set( GTK_LIST_STORE( EntityBreakdownWalker ), &iter, 0, ( *i ).first.c_str(), 1, tmp, -1 );
EntityBreakdownWalker.unref();
auto tmp = u::buffer<16>();
- sprintf( tmp, "%u", Unsigned( g_brushCount.get() ) );
+ tmp.sprintf( "%u", Unsigned( g_brushCount.get() ) );
brushes_entry.text(tmp);
- sprintf( tmp, "%u", Unsigned( g_entityCount.get() ) );
+ tmp.sprintf( "%u", Unsigned( g_entityCount.get() ) );
entities_entry.text(tmp);
modal_dialog_show( window, dialog );
// write the info_playerstart
auto sTmp = u::buffer<1024>();
- sprintf( sTmp, "%d %d %d", (int)vOrig[0], (int)vOrig[1], (int)vOrig[2] );
+ sTmp.sprintf( "%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] );
+ sTmp.sprintf( "%d", (int)Camera_getAngles( *g_pParentWnd->GetCamWnd() )[CAMERA_YAW] );
Node_getEntity( *startpoint )->setKeyValue( "angle", sTmp );
}
int ent, br;
GetSelectionIndex( &ent, &br );
- sprintf( buf, "%i", ent );
+ buf.sprintf( "%i", ent );
entity.text(buf);
- sprintf( buf, "%i", br );
+ buf.sprintf( "%i", br );
brush.text(buf);
if ( modal_dialog_show( window, dialog ) == eIDOK ) {
void MRU_Activate( std::size_t index ){
auto text = u::buffer<1024>();
- strcpy( text, MRU_GetText( index ) );
+ text.copy( MRU_GetText( index ) );
if ( file_readable( text ) ) { //\todo Test 'map load succeeds' instead of 'file is readable'.
MRU_AddFile( text );
{
auto width = u::buffer<16>();
auto height = u::buffer<16>();
- sprintf( width, "%u", Unsigned( m_width ) );
- sprintf( height, "%u", Unsigned( m_height ) );
+ width.sprintf( "%u", Unsigned( m_width ) );
+ height.sprintf( "%u", Unsigned( m_height ) );
StaticElement element( "matrix" );
element.insertAttribute( "width", width );
element.insertAttribute( "height", height );
for ( std::size_t i = 0; i < m_countRows; ++i )
{
auto buffer = u::buffer<16>();
- sprintf( buffer, "%u", Unsigned( i ) );
+ buffer.sprintf( "%u", Unsigned( i ) );
gtk_combo_box_text_append_text( m_pRowCombo, buffer );
}
for ( std::size_t i = 0; i < m_countCols; ++i )
{
auto buffer = u::buffer<16>();
- sprintf( buffer, "%u", Unsigned( i ) );
+ buffer.sprintf( "%u", Unsigned( i ) );
gtk_combo_box_text_append_text( m_pColCombo, buffer );
}
void QE_UpdateStatusBar(){
auto buffer = u::buffer<128>();
- sprintf( buffer, "Brushes: %d Entities: %d", g_numbrushes, g_numentities );
+ buffer.sprintf( "Brushes: %d Entities: %d", g_numbrushes, g_numentities );
g_pParentWnd->SetStatusText( g_pParentWnd->m_brushcount_status, buffer );
}
u::BufferVal<256> g_gammatable;
void ResampleGamma( float fGamma ){
int i,inf;
- if ( fGamma == 1.0 ) {
+ auto buf = g_gammatable.mut();
+ if (fGamma == 1.0 ) {
for ( i = 0; i < 256; i++ )
- g_gammatable[i] = i;
+ buf[i] = i;
}
else
{
if ( inf > 255 ) {
inf = 255;
}
- g_gammatable[i] = inf;
+ buf[i] = inf;
}
}
}
gchar* buffer;
gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
- strcpy( dirName, buffer );
+ dirName.copy(buffer);
g_free( buffer );
g_TextureBrowser.m_searchedTags = false;
if ( !TextureBrowser_showWads() ) {
- strcat( dirName, "/" );
+ strcat( dirName.mut(), "/" );
}
ScopeDisableScreenUpdates disableScreenUpdates( dirName, "Loading Textures" );
gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
if ( selected != NULL ) {
- strcpy( buffer, "/root/*/*[tag='" );
- strcpy( tags_searched, "[TAGS] " );
+ buffer.copy( "/root/*/*[tag='" );
+ tags_searched.copy( "[TAGS] " );
for ( node = selected; node != NULL; node = node->next )
{
if ( gtk_tree_model_get_iter( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iter, path ) ) {
gtk_tree_model_get( GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list ), &iter, TAG_COLUMN, &tag, -1 );
- strcat( buffer, tag );
- strcat( tags_searched, tag );
+ strcat( buffer.mut(), tag );
+ strcat( tags_searched.mut(), tag );
if ( node != g_slist_last( node ) ) {
- strcat( buffer, "' and tag='" );
- strcat( tags_searched, ", " );
+ strcat( buffer.mut(), "' and tag='" );
+ strcat( tags_searched.mut(), ", " );
}
}
}
}
- strcat( buffer, "']" );
+ strcat( buffer.mut(), "']" );
g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
gchar* buffer;
gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
- strcpy( dirName, buffer );
+ dirName.copy( buffer );
g_free( buffer );
if ( !TextureBrowser_showWads() ) {
- strcat( dirName, "/" );
+ strcat( dirName.mut(), "/" );
}
TextureBrowser_ShowDirectory( GlobalTextureBrowser(), dirName );
TextureBrowser_queueDraw( GlobalTextureBrowser() );
const char* Cull_GetStats(){
#if defined( DEBUG_CULLING )
- sprintf( g_cull_stats, "dots: %d | planes %d + %d | bboxs %d + %d", g_count_dots, g_count_planes, g_count_oriented_planes, g_count_bboxs, g_count_oriented_bboxs );
+ g_cull_stats.sprintf( "dots: %d | planes %d + %d | bboxs %d + %d", g_count_dots, g_count_planes, g_count_oriented_planes, g_count_bboxs, g_count_oriented_bboxs );
return g_cull_stats;
#else
return "";
va_list args;
va_start( args, msg );
- vsprintf( saxMsgBuffer, msg, args );
+ vsprintf( saxMsgBuffer.mut(), msg, args );
va_end( args );
globalOutputStream() << "XML warning: " << saxMsgBuffer << "\n";
}
va_list args;
va_start( args, msg );
- vsprintf( saxMsgBuffer, msg, args );
+ vsprintf( saxMsgBuffer.mut(), msg, args );
va_end( args );
globalErrorStream() << "XML error: " << saxMsgBuffer << "\n";
}
va_list args;
va_start( args, msg );
- vsprintf( buffer, msg, args );
+ vsprintf( buffer.mut(), msg, args );
va_end( args );
globalErrorStream() << "XML fatal error: " << buffer << "\n";
}
}
auto fileNameWithoutExt = u::buffer<512>();
- strncpy( fileNameWithoutExt, relative, sizeof( fileNameWithoutExt ) - 1 );
- fileNameWithoutExt[512 - 1] = '\0';
- fileNameWithoutExt[strlen( fileNameWithoutExt ) - 4] = '\0';
+ fileNameWithoutExt.copy(relative);
+ fileNameWithoutExt.terminate();
+ fileNameWithoutExt.mut()[fileNameWithoutExt.strlen() - 4] = '\0';
Image *image = QERApp_LoadImage( 0, fileNameWithoutExt );
if ( !image ) {
float offy = m_vOrigin[nDim1] - w + 4 / m_fScale;
for ( x = xb - fmod( xb, stepx ); x <= xe ; x += stepx ) {
glRasterPos2f( x, offx );
- sprintf( text, "%g", x );
+ text.sprintf( "%g", x );
GlobalOpenGL().drawString( text );
}
for ( y = yb - fmod( yb, stepy ); y <= ye ; y += stepy ) {
glRasterPos2f( offy, y );
- sprintf( text, "%g", y );
+ text.sprintf( "%g", y );
GlobalOpenGL().drawString( text );
}
for ( y = yb ; y < ye ; y += g_xywindow_globals_private.blockSize )
{
glRasterPos2f( x + ( g_xywindow_globals_private.blockSize / 2 ), y + ( g_xywindow_globals_private.blockSize / 2 ) );
- sprintf( text, "%i,%i",(int)floor( x / g_xywindow_globals_private.blockSize ), (int)floor( y / g_xywindow_globals_private.blockSize ) );
+ text.sprintf( "%i,%i",(int)floor( x / g_xywindow_globals_private.blockSize ), (int)floor( y / g_xywindow_globals_private.blockSize ) );
GlobalOpenGL().drawString( text );
}
}