Skip to content

Commit

Permalink
Fixed #30: asprintf() is a static function, use return values.
Browse files Browse the repository at this point in the history
This fixes an error introduced by a patch porting to C++17 and Qt6.
  • Loading branch information
blais committed Sep 24, 2022
1 parent 090aa68 commit fe42f80
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 59 deletions.
11 changes: 6 additions & 5 deletions src/app.cpp
Expand Up @@ -3530,11 +3530,12 @@ void XxApp::diffFilesAtCursor()

// Add the title there if we have it.
if ( 0 > _cmdline._userFilenames[ii].indexOf('%') ) {
QString * tmpTitle = new QString();
tmpTitle->asprintf( "--title%d=%s",
ii+1,
_cmdline._userFilenames[ii].toLocal8Bit().constData() );
titles[ii] = tmpTitle;
QString* tmpTitle = new QString();
*tmpTitle = QString::asprintf(
"--title%d=%s",
ii+1,
_cmdline._userFilenames[ii].toLocal8Bit().constData() );
titles[ii] = tmpTitle;
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/buffer.cpp
Expand Up @@ -691,13 +691,15 @@ uint XxBuffer::getNbDigits() const

//------------------------------------------------------------------------------
//
const QString& XxBuffer::renderLineNumber(
QString XxBuffer::renderLineNumber(
const XxFln lineNumber,
const QString& format
)
{
_lnBuffer.asprintf( format.toLocal8Bit().constData(), lineNumber );
return _lnBuffer;
// Note: It would be ideal to find a way to reuse the output buffer here and
// not allocate a new QString every time, and return a "const QString&" to a
// local instead. It used to be this way, and QString::sprintf() was removed.
return QString::asprintf( format.toLocal8Bit().constData(), lineNumber );
}

//------------------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions src/buffer.h
Expand Up @@ -184,7 +184,7 @@ class XxBuffer {
uint getNbDigits() const;

// Renders the line number. See renderTextWithTabs.
const QString& renderLineNumber(
QString renderLineNumber(
const XxFln lineNumber,
const QString& format
);
Expand Down Expand Up @@ -281,7 +281,6 @@ class XxBuffer {

char* _renderBuffer;
int _renderBufferSize;
QString _lnBuffer;

QStringList _directoryEntries;

Expand Down
7 changes: 5 additions & 2 deletions src/lineNumbers.cpp
Expand Up @@ -123,7 +123,7 @@ void XxLineNumbers::paintEvent( QPaintEvent *e )
int nbLines = std::min( displayLines, diffs->getNbLines() - (topLine - 1) );

QString lnFormat;
lnFormat.asprintf( "%%%dd", _app->getMaxDigits() );
QTextStream(&lnFormat) << "%" << _app->getMaxDigits() << "d";

p.setPen( palette().color( backgroundRole() ).darker( 200 ) );

Expand Down Expand Up @@ -156,7 +156,10 @@ void XxLineNumbers::paintEvent( QPaintEvent *e )
if ( fline != -1 ) {
XxFln dfline = file->getDisplayLineNo( fline );

const QString& renderedNums =
// Note: this allocates a new QString per line on render on screetn. We
// could probably do away with the allocation, but we'd have to use a
// non-Qt unicode renderer (not asprintf()).
QString renderedNums =
file->renderLineNumber( dfline, lnFormat );

p.drawText( x+2, y + fm.ascent(), renderedNums );
Expand Down
5 changes: 3 additions & 2 deletions src/text.cpp
Expand Up @@ -45,6 +45,7 @@
#include <QResizeEvent>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QTextStream>

#include <math.h>
#include <stdio.h>
Expand Down Expand Up @@ -1258,7 +1259,7 @@ QString XxText::formatClipboard(
break;
}
QString buf;
buf.asprintf( "%d", fileno );
QTextStream(&buf) << fileno;
forline.replace( spos, 2, buf );
pos = spos + buf.length();
}
Expand All @@ -1271,7 +1272,7 @@ QString XxText::formatClipboard(
break;
}
QString buf;
buf.asprintf( "%d", lineno );
QTextStream(&buf) << lineno;
forline.replace( spos, 2, buf );
pos = spos + buf.length();
}
Expand Down
78 changes: 33 additions & 45 deletions src/util.cpp
Expand Up @@ -145,15 +145,15 @@ void formatPrintFunc(

case 'n': { // - File name
strcat( pformat, "s" );
QString tmp;
tmp.asprintf( pformat, filename.toLocal8Bit().constData() );

QString tmp = QString::asprintf( pformat, filename.toLocal8Bit().constData() );
target.append( tmp );
} break;

case 'N': { // - Quoted File name with dereference if symbolic link
strcat( pformat, "s" );
QString tmp;
tmp.asprintf( pformat, filename.toLocal8Bit().constData() );

QString tmp = QString::asprintf( pformat, filename.toLocal8Bit().constData() );
if ( qfi.isSymLink() ) {
tmp.append( "' -> `" );
tmp.append( qfi.symLinkTarget() );
Expand All @@ -165,102 +165,92 @@ void formatPrintFunc(

case 'a': { // - Access rights in octal
strcat( pformat, "u" );
QString tmp;
tmp.asprintf( pformat, qfiToPerm( qfi ) );
QString tmp = QString::asprintf( pformat, qfiToPerm( qfi ) );
target.append( tmp );
} break;

case 'A': { // - Access rights in human readable form
strcat( pformat, "s" );
QString tmp;
char tbuf[11];
tmp.asprintf( pformat, qfiToHPerm( tbuf,qfi ) );
QString tmp = QString::asprintf( pformat, qfiToHPerm( tbuf,qfi ) );
target.append( tmp );
} break;

case 'F': { // - File type
strcat( pformat, "s" );
QString tmp;
tmp.asprintf( pformat, ( qfi.isFile()?"Regular File":
( qfi.isDir()?"Directory":
( qfi.isSymLink()?"Symbolic Link":
"Unknown" ) ) ) );
QString tmp = QString::asprintf( pformat, ( qfi.isFile()?"Regular File":
( qfi.isDir()?"Directory":
( qfi.isSymLink()?"Symbolic Link":
"Unknown" ) ) ) );
target.append( tmp );
} break;

case 'U': { // - User name of owner
strcat( pformat, "s" );
QString tmp;
tmp.asprintf( pformat,qfi.owner().toLocal8Bit().constData() );
QString tmp = QString::asprintf( pformat,qfi.owner().toLocal8Bit().constData() );
target.append( tmp );
} break;

case 'u': { // - User ID of owner
strcat( pformat, "u" );
QString tmp;
tmp.asprintf( pformat,qfi.ownerId() );
QString tmp = QString::asprintf( pformat,qfi.ownerId() );
target.append( tmp );
} break;

case 'G': { // - Group name of owner
strcat( pformat, "s" );
QString tmp;
tmp.asprintf( pformat,qfi.group().toLocal8Bit().constData() );
QString tmp = QString::asprintf( pformat,qfi.group().toLocal8Bit().constData() );
target.append( tmp );
} break;

case 'g': { // - Group ID of owner
strcat( pformat, "u" );
QString tmp;
tmp.asprintf( pformat,qfi.groupId() );
QString tmp = QString::asprintf( pformat,qfi.groupId() );
target.append( tmp );
} break;

case 's': { // - Total size, in bytes
strcat( pformat, "lu" );
QString tmp;
tmp.asprintf( pformat,qfi.size() );
QString tmp = QString::asprintf( pformat,qfi.size() );
target.append( tmp );
} break;

case 'X': { // - Time of last access as seconds since Epoch
strcat( pformat, "d" );
QString tmp;
tmp.asprintf( pformat, XxUtil::toTime_t( qfi.lastRead() ) );
QString tmp = QString::asprintf( pformat, XxUtil::toTime_t( qfi.lastRead() ) );
target.append( tmp );
} break;

case 'x': { // - Time of last access
strcat( pformat, "s" );
QString tmp;
// It's not the exact same as stat( 2 ) does, but this is ISO 8601
// and stat uses some weird syntax of it's own.
tmp.asprintf( pformat,
qfi.lastRead().toString( DATEFORMAT ).toLocal8Bit().constData() );
QString tmp = QString::asprintf(
pformat,
qfi.lastRead().toString( DATEFORMAT ).toLocal8Bit().constData() );
target.append( tmp );
} break;

case 'Y': { // - Time of last modification as seconds since Epoch
strcat( pformat, "d" );
QString tmp;
tmp.asprintf( pformat, XxUtil::toTime_t( qfi.lastModified() ) );
QString tmp = QString::asprintf( pformat, XxUtil::toTime_t( qfi.lastModified() ) );
target.append( tmp );
} break;

case 'y': { // - Time of last modification
strcat( pformat, "s" );
QString tmp;
// It's not the exact same as stat( 2 ) does, but this is ISO 8601
// and stat uses some weird syntax of it's own.
tmp.asprintf( pformat,
qfi.lastModified().toString( DATEFORMAT ).toLocal8Bit().constData() );
QString tmp = QString::asprintf(
pformat,
qfi.lastModified().toString( DATEFORMAT ).toLocal8Bit().constData() );
target.append( tmp );
} break;

default: {
throw XxUsageError(
XX_EXC_PARAMS,
QString().asprintf( "unknown %%-directive %c\n", m )
QString::asprintf( "unknown %%-directive %c\n", m )
);
}
}
Expand Down Expand Up @@ -326,18 +316,17 @@ void formatPrintFunc(

case 'X': { // - Time of last access as seconds since Epoch
strcat( pformat, "d" );
QString tmp;
tmp.asprintf( pformat,
XxUtil::toTime_t( QDateTime::currentDateTime() ) );
QString tmp = QString::asprintf(
pformat,
XxUtil::toTime_t( QDateTime::currentDateTime() ) );
target.append( tmp );
} break;

case 'x': { // - Time of last access
strcat( pformat, "s" );
QString tmp;
// It's not the exact same as stat( 2 ) does, but this is ISO 8601
// and stat uses some weird syntax of it's own.
tmp.asprintf(
QString tmp = QString::asprintf(
pformat,
( QDateTime::currentDateTime() ).toString( DATEFORMAT ).toLocal8Bit().constData()
);
Expand All @@ -346,18 +335,17 @@ void formatPrintFunc(

case 'Y': { // - Time of last modification as seconds since Epoch
strcat( pformat, "d" );
QString tmp;
tmp.asprintf( pformat,
XxUtil::toTime_t( QDateTime::currentDateTime() ) );
QString tmp = QString::asprintf(
pformat,
XxUtil::toTime_t( QDateTime::currentDateTime() ) );
target.append( tmp );
} break;

case 'y': { // - Time of last modification
strcat( pformat, "s" );
QString tmp;
// It's not the exact same as stat( 2 ) does, but this is ISO 8601
// and stat uses some weird syntax of it's own.
tmp.asprintf(
QString tmp = QString::asprintf(
pformat,
( QDateTime::currentDateTime() ).toString( DATEFORMAT ).toLocal8Bit().constData() );
target.append( tmp );
Expand All @@ -366,7 +354,7 @@ void formatPrintFunc(
default: {
throw XxUsageError(
XX_EXC_PARAMS,
QString().asprintf( "unknown %%-directive %c\n", m )
QString::asprintf( "unknown %%-directive %c\n", m )
);
}

Expand Down

0 comments on commit fe42f80

Please sign in to comment.