From 7729d89e15bc68899554b28f480a891c61ef0872 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Tue, 26 Feb 2013 12:51:34 +0200 Subject: [PATCH] QWin32PrintEngine: Fix build on MinGW + avoid dummy allocations Don't use wcscpy_s() which is not available on MinGW but determine the utf-16 string length and pass that value to QString::fromWCharArray() instead. Change-Id: I45d1b1969fe03255fdb6353fa9f52417af530e40 Reviewed-by: Laszlo Papp Reviewed-by: Andy Shaw --- src/printsupport/kernel/qprintengine_win.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/printsupport/kernel/qprintengine_win.cpp b/src/printsupport/kernel/qprintengine_win.cpp index f5690c12f3..7c67fd2845 100644 --- a/src/printsupport/kernel/qprintengine_win.cpp +++ b/src/printsupport/kernel/qprintengine_win.cpp @@ -1596,6 +1596,16 @@ QList QWin32PrintEngine::supportedPaperSizes(const QPrinter return returnList; } +static inline uint qwcsnlen(const wchar_t *str, uint maxlen) +{ + uint length = 0; + if (str) { + while (length < maxlen && *str++) + length++; + } + return length; +} + QList > QWin32PrintEngine::supportedSizesWithNames(const QPrinterInfo &printerInfo) { QList > paperSizes; @@ -1603,22 +1613,20 @@ QList > QWin32PrintEngine::supportedSizesWithNames(const return paperSizes; DWORD size = DeviceCapabilities(reinterpret_cast(printerInfo.printerName().utf16()), NULL, DC_PAPERNAMES, NULL, NULL); - if ((int)size != -1) { + if ((int)size > 0) { wchar_t *papers = new wchar_t[size*64]; size = DeviceCapabilities(reinterpret_cast(printerInfo.printerName().utf16()), NULL, DC_PAPERNAMES, papers, NULL); DWORD size2 = DeviceCapabilities(reinterpret_cast(printerInfo.printerName().utf16()), NULL, DC_PAPERSIZE, NULL, NULL); - if ((int)size2 != -1) { + if ((int)size2 > 0) { POINT *points = new POINT[size2*sizeof(POINT)]; size2 = DeviceCapabilities(reinterpret_cast(printerInfo.printerName().utf16()), NULL, DC_PAPERSIZE, (wchar_t *)points, NULL); - wchar_t copyOfPaper[65]; for (int i=0;i<(int)size;i++) { - wcscpy_s(copyOfPaper, 64, papers + (i * 64)); - copyOfPaper[64] = '\0'; - QString str = QString::fromWCharArray(copyOfPaper); + wchar_t *paper = papers + (i * 64); + QString str = QString::fromWCharArray(paper, qwcsnlen(paper, 64)); paperSizes << qMakePair(str, QSizeF(points[i].x / 10, points[i].y / 10)); } delete [] points;