From f9d42ed3960430ab0d2c605caafb94a28ee77948 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Sun, 2 May 2021 10:43:44 +0400 Subject: [PATCH] Fix MSVC's C4267 warnings --- CMakeLists.txt | 1 - apps/mwiniimporter/importer.cpp | 10 +++++----- apps/opencs/model/doc/blacklist.cpp | 2 +- apps/opencs/model/filter/narynode.cpp | 2 +- apps/opencs/model/tools/mandatoryid.cpp | 2 +- apps/opencs/model/tools/reportmodel.cpp | 6 +++--- apps/opencs/model/world/idtable.cpp | 2 +- apps/opencs/model/world/resources.cpp | 6 +++--- apps/opencs/view/doc/operations.cpp | 4 ++-- apps/opencs/view/widget/scenetoolrun.cpp | 2 +- apps/opencs/view/widget/scenetooltoggle.cpp | 4 ++-- apps/opencs/view/widget/scenetooltoggle2.cpp | 2 +- apps/opencs/view/world/idvalidator.cpp | 2 +- components/bsa/bsa_file.cpp | 16 ++++++++-------- components/bsa/bsa_file.hpp | 4 ++-- components/compiler/controlparser.cpp | 6 +++--- components/compiler/literals.cpp | 8 ++++---- components/crashcatcher/windows_crashcatcher.cpp | 4 ++-- components/esm/esmreader.cpp | 6 +++--- components/esm/esmwriter.cpp | 2 +- components/files/lowlevelfile.cpp | 4 ++-- components/interpreter/runtime.cpp | 2 +- components/widgets/list.cpp | 2 +- extern/osg-ffmpeg-videoplayer/audiodecoder.cpp | 2 +- 24 files changed, 50 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 104140107..fe31951df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -607,7 +607,6 @@ if (WIN32) 4100 # Unreferenced formal parameter (-Wunused-parameter) 4127 # Conditional expression is constant 4244 # Storing value of one type in variable of another (size_t in int, for example) - 4267 # Conversion from 'size_t' to 'int', possible loss of data 4996 # Function was declared deprecated ) diff --git a/apps/mwiniimporter/importer.cpp b/apps/mwiniimporter/importer.cpp index 94b5cf7d0..2763d8ad9 100644 --- a/apps/mwiniimporter/importer.cpp +++ b/apps/mwiniimporter/importer.cpp @@ -676,7 +676,7 @@ MwIniImporter::multistrmap MwIniImporter::loadIniFile(const boost::filesystem::p } if(line[0] == '[') { - int pos = line.find(']'); + int pos = static_cast(line.find(']')); if(pos < 2) { std::cout << "Warning: ini file wrongly formatted (" << line << "). Line ignored." << std::endl; continue; @@ -686,12 +686,12 @@ MwIniImporter::multistrmap MwIniImporter::loadIniFile(const boost::filesystem::p continue; } - int comment_pos = line.find(';'); + int comment_pos = static_cast(line.find(';')); if(comment_pos > 0) { line = line.substr(0,comment_pos); } - int pos = line.find('='); + int pos = static_cast(line.find('=')); if(pos < 1) { continue; } @@ -722,7 +722,7 @@ MwIniImporter::multistrmap MwIniImporter::loadCfgFile(const boost::filesystem::p while (std::getline(file, line)) { // we cant say comment by only looking at first char anymore - int comment_pos = line.find('#'); + int comment_pos = static_cast(line.find('#')); if(comment_pos > 0) { line = line.substr(0,comment_pos); } @@ -731,7 +731,7 @@ MwIniImporter::multistrmap MwIniImporter::loadCfgFile(const boost::filesystem::p continue; } - int pos = line.find('='); + int pos = static_cast(line.find('=')); if(pos < 1) { continue; } diff --git a/apps/opencs/model/doc/blacklist.cpp b/apps/opencs/model/doc/blacklist.cpp index b1d402c69..690d79983 100644 --- a/apps/opencs/model/doc/blacklist.cpp +++ b/apps/opencs/model/doc/blacklist.cpp @@ -21,7 +21,7 @@ void CSMDoc::Blacklist::add (CSMWorld::UniversalId::Type type, { std::vector& list = mIds[type]; - int size = list.size(); + size_t size = list.size(); list.resize (size+ids.size()); diff --git a/apps/opencs/model/filter/narynode.cpp b/apps/opencs/model/filter/narynode.cpp index 2fa9ac6cc..9415f1daf 100644 --- a/apps/opencs/model/filter/narynode.cpp +++ b/apps/opencs/model/filter/narynode.cpp @@ -9,7 +9,7 @@ CSMFilter::NAryNode::NAryNode (const std::vector >& nodes, int CSMFilter::NAryNode::getSize() const { - return mNodes.size(); + return static_cast(mNodes.size()); } const CSMFilter::Node& CSMFilter::NAryNode::operator[] (int index) const diff --git a/apps/opencs/model/tools/mandatoryid.cpp b/apps/opencs/model/tools/mandatoryid.cpp index 23adb9d37..d0d9cc0b9 100644 --- a/apps/opencs/model/tools/mandatoryid.cpp +++ b/apps/opencs/model/tools/mandatoryid.cpp @@ -11,7 +11,7 @@ CSMTools::MandatoryIdStage::MandatoryIdStage (const CSMWorld::CollectionBase& id int CSMTools::MandatoryIdStage::setup() { - return mIds.size(); + return static_cast(mIds.size()); } void CSMTools::MandatoryIdStage::perform (int stage, CSMDoc::Messages& messages) diff --git a/apps/opencs/model/tools/reportmodel.cpp b/apps/opencs/model/tools/reportmodel.cpp index f9a1fdb0c..a2901a663 100644 --- a/apps/opencs/model/tools/reportmodel.cpp +++ b/apps/opencs/model/tools/reportmodel.cpp @@ -24,7 +24,7 @@ int CSMTools::ReportModel::rowCount (const QModelIndex & parent) const if (parent.isValid()) return 0; - return mRows.size(); + return static_cast(mRows.size()); } int CSMTools::ReportModel::columnCount (const QModelIndex & parent) const @@ -140,7 +140,7 @@ bool CSMTools::ReportModel::removeRows (int row, int count, const QModelIndex& p void CSMTools::ReportModel::add (const CSMDoc::Message& message) { - beginInsertRows (QModelIndex(), mRows.size(), mRows.size()); + beginInsertRows (QModelIndex(), static_cast(mRows.size()), static_cast(mRows.size())); mRows.push_back (message); @@ -176,7 +176,7 @@ void CSMTools::ReportModel::clear() { if (!mRows.empty()) { - beginRemoveRows (QModelIndex(), 0, mRows.size()-1); + beginRemoveRows (QModelIndex(), 0, static_cast(mRows.size())-1); mRows.clear(); endRemoveRows(); } diff --git a/apps/opencs/model/world/idtable.cpp b/apps/opencs/model/world/idtable.cpp index 27d60ae98..30fe6f639 100644 --- a/apps/opencs/model/world/idtable.cpp +++ b/apps/opencs/model/world/idtable.cpp @@ -270,7 +270,7 @@ void CSMWorld::IdTable::reorderRows (int baseIndex, const std::vector& newO if (!newOrder.empty()) if (mIdCollection->reorderRows (baseIndex, newOrder)) emit dataChanged (index (baseIndex, 0), - index (baseIndex+newOrder.size()-1, mIdCollection->getColumns()-1)); + index (baseIndex+static_cast(newOrder.size())-1, mIdCollection->getColumns()-1)); } std::pair CSMWorld::IdTable::view (int row) const diff --git a/apps/opencs/model/world/resources.cpp b/apps/opencs/model/world/resources.cpp index b40ab1389..c3eb9762e 100644 --- a/apps/opencs/model/world/resources.cpp +++ b/apps/opencs/model/world/resources.cpp @@ -20,13 +20,13 @@ void CSMWorld::Resources::recreate(const VFS::Manager* vfs, const char * const * mFiles.clear(); mIndex.clear(); - int baseSize = mBaseDirectory.size(); + size_t baseSize = mBaseDirectory.size(); const std::map& index = vfs->getIndex(); for (std::map::const_iterator it = index.begin(); it != index.end(); ++it) { std::string filepath = it->first; - if (static_cast (filepath.size())(mFiles.size()); } std::string CSMWorld::Resources::getId (int index) const diff --git a/apps/opencs/view/doc/operations.cpp b/apps/opencs/view/doc/operations.cpp index 7ee4b8726..9ed77ff9c 100644 --- a/apps/opencs/view/doc/operations.cpp +++ b/apps/opencs/view/doc/operations.cpp @@ -30,7 +30,7 @@ void CSVDoc::Operations::setProgress (int current, int max, int type, int thread return; } - int oldCount = mOperations.size(); + int oldCount = static_cast(mOperations.size()); int newCount = oldCount + 1; Operation *operation = new Operation (type, this); @@ -51,7 +51,7 @@ void CSVDoc::Operations::quitOperation (int type) for (std::vector::iterator iter (mOperations.begin()); iter!=mOperations.end(); ++iter) if ((*iter)->getType()==type) { - int oldCount = mOperations.size(); + int oldCount = static_cast(mOperations.size()); int newCount = oldCount - 1; mLayout->removeItem ((*iter)->getLayout()); diff --git a/apps/opencs/view/widget/scenetoolrun.cpp b/apps/opencs/view/widget/scenetoolrun.cpp index b53282036..24bcf3f13 100644 --- a/apps/opencs/view/widget/scenetoolrun.cpp +++ b/apps/opencs/view/widget/scenetoolrun.cpp @@ -30,7 +30,7 @@ void CSVWidget::SceneToolRun::updateIcon() void CSVWidget::SceneToolRun::updatePanel() { - mTable->setRowCount (mProfiles.size()); + mTable->setRowCount (static_cast(mProfiles.size())); int i = 0; diff --git a/apps/opencs/view/widget/scenetooltoggle.cpp b/apps/opencs/view/widget/scenetooltoggle.cpp index fa0be3155..04ac3322b 100644 --- a/apps/opencs/view/widget/scenetooltoggle.cpp +++ b/apps/opencs/view/widget/scenetooltoggle.cpp @@ -80,7 +80,7 @@ QRect CSVWidget::SceneToolToggle::getIconBox (int index) const int y = index / xMax; int x = index % xMax; - int total = mButtons.size(); + int total = static_cast(mButtons.size()); int actualYIcons = total/xMax; @@ -154,7 +154,7 @@ void CSVWidget::SceneToolToggle::addButton (const std::string& icon, unsigned in desc.mMask = mask; desc.mSmallIcon = smallIcon; desc.mName = name; - desc.mIndex = mButtons.size(); + desc.mIndex = static_cast(mButtons.size()); mButtons.insert (std::make_pair (button, desc)); diff --git a/apps/opencs/view/widget/scenetooltoggle2.cpp b/apps/opencs/view/widget/scenetooltoggle2.cpp index cf9bfe349..363cae570 100644 --- a/apps/opencs/view/widget/scenetooltoggle2.cpp +++ b/apps/opencs/view/widget/scenetooltoggle2.cpp @@ -99,7 +99,7 @@ void CSVWidget::SceneToolToggle2::addButton (unsigned int id, unsigned int mask, desc.mButtonId = id; desc.mMask = mask; desc.mName = name; - desc.mIndex = mButtons.size(); + desc.mIndex = static_cast(mButtons.size()); mButtons.insert (std::make_pair (button, desc)); diff --git a/apps/opencs/view/world/idvalidator.cpp b/apps/opencs/view/world/idvalidator.cpp index 1092d7217..442157ac5 100644 --- a/apps/opencs/view/world/idvalidator.cpp +++ b/apps/opencs/view/world/idvalidator.cpp @@ -42,7 +42,7 @@ QValidator::State CSVWorld::IdValidator::validate (QString& input, int& pos) con if (!mNamespace.empty()) { - std::string namespace_ = input.left (mNamespace.size()).toUtf8().constData(); + std::string namespace_ = input.left (static_cast(mNamespace.size())).toUtf8().constData(); if (Misc::StringUtils::lowerCase (namespace_)!=mNamespace) return QValidator::Invalid; // incorrect namespace diff --git a/components/bsa/bsa_file.cpp b/components/bsa/bsa_file.cpp index ec455ea1f..4758d0a86 100644 --- a/components/bsa/bsa_file.cpp +++ b/components/bsa/bsa_file.cpp @@ -43,7 +43,7 @@ void BSAFile::fail(const std::string &msg) BSAFile::Hash getHash(const std::string& name) { BSAFile::Hash hash; - unsigned l = (name.size() >> 1); + unsigned l = (static_cast(name.size()) >> 1); unsigned sum, off, temp, i, n; for (sum = off = i = 0; i < l; i++) { @@ -163,7 +163,7 @@ void BSAFile::readHeader() { FileStruct &fs = mFiles[i]; fs.fileSize = offsets[i*2]; - fs.offset = offsets[i*2+1] + fileDataOffset; + fs.offset = static_cast(offsets[i*2+1] + fileDataOffset); auto namesOffset = offsets[2*filenum+i]; fs.setNameInfos(namesOffset, &mStringBuf); fs.hash = hashes[i]; @@ -203,11 +203,11 @@ void Bsa::BSAFile::writeHeader() uint32_t head[3]; head[0] = 0x100; auto fileDataOffset = mFiles.empty() ? 12 : mFiles.front().offset; - head[1] = fileDataOffset - 12 - 8*mFiles.size(); + head[1] = static_cast(fileDataOffset - 12 - 8*mFiles.size()); output.seekp(0, std::ios_base::end); - head[2] = mFiles.size(); + head[2] = static_cast(mFiles.size()); output.seekp(0); output.write(reinterpret_cast(head), 12); @@ -239,9 +239,9 @@ int BSAFile::getIndex(const char *str) const if(it == mLookup.end()) return -1; - int res = it->second; - assert(res >= 0 && (size_t)res < mFiles.size()); - return res; + size_t res = it->second; + assert(res >= 0 && res < mFiles.size()); + return static_cast(res); } /// Open an archive file. @@ -300,7 +300,7 @@ void Bsa::BSAFile::addFile(const std::string& filename, std::istream& file) newFile.hash = getHash(filename); if(mFiles.empty()) - newFile.offset = newStartOfDataBuffer; + newFile.offset = static_cast(newStartOfDataBuffer); else { std::vector buffer; diff --git a/components/bsa/bsa_file.hpp b/components/bsa/bsa_file.hpp index fa6e5fc1c..f9b4d4fa3 100644 --- a/components/bsa/bsa_file.hpp +++ b/components/bsa/bsa_file.hpp @@ -58,7 +58,7 @@ public: void setNameInfos(size_t index, std::vector* stringBuf ) { - namesOffset = index; + namesOffset = static_cast(index); namesBuffer = stringBuf; } @@ -102,7 +102,7 @@ protected: the files[] vector above. The iltstr ensures that file name checks are case insensitive. */ - typedef std::map Lookup; + typedef std::map Lookup; Lookup mLookup; /// Error handling diff --git a/components/compiler/controlparser.cpp b/components/compiler/controlparser.cpp index ec69fffa2..a5a68edbd 100644 --- a/components/compiler/controlparser.cpp +++ b/components/compiler/controlparser.cpp @@ -39,14 +39,14 @@ namespace Compiler Codes block; if (iter!=mIfCode.rbegin()) - Generator::jump (iter->second, codes.size()+1); + Generator::jump (iter->second, static_cast(codes.size()+1)); if (!iter->first.empty()) { // if or elseif std::copy (iter->first.begin(), iter->first.end(), std::back_inserter (block)); - Generator::jumpOnZero (block, iter->second.size()+1); + Generator::jumpOnZero (block, static_cast(iter->second.size()+1)); } std::copy (iter->second.begin(), iter->second.end(), @@ -113,7 +113,7 @@ namespace Compiler Codes skip; - Generator::jumpOnZero (skip, mCodeBlock.size()+loop.size()+1); + Generator::jumpOnZero (skip, static_cast (mCodeBlock.size()+loop.size()+1)); std::copy (skip.begin(), skip.end(), std::back_inserter (mCode)); diff --git a/components/compiler/literals.cpp b/components/compiler/literals.cpp index 774ca4ca7..a40ff2a02 100644 --- a/components/compiler/literals.cpp +++ b/components/compiler/literals.cpp @@ -6,12 +6,12 @@ namespace Compiler { int Literals::getIntegerSize() const { - return mIntegers.size() * sizeof (Interpreter::Type_Integer); + return static_cast(mIntegers.size() * sizeof (Interpreter::Type_Integer)); } int Literals::getFloatSize() const { - return mFloats.size() * sizeof (Interpreter::Type_Float); + return static_cast(mFloats.size() * sizeof (Interpreter::Type_Float)); } int Literals::getStringSize() const @@ -41,11 +41,11 @@ namespace Compiler code.resize (size+stringBlockSize/4); - int offset = 0; + size_t offset = 0; for (const auto & mString : mStrings) { - int stringSize = mString.size()+1; + size_t stringSize = mString.size()+1; std::copy (mString.c_str(), mString.c_str()+stringSize, reinterpret_cast (&code[size]) + offset); diff --git a/components/crashcatcher/windows_crashcatcher.cpp b/components/crashcatcher/windows_crashcatcher.cpp index 185eee6aa..39ac86d7b 100644 --- a/components/crashcatcher/windows_crashcatcher.cpp +++ b/components/crashcatcher/windows_crashcatcher.cpp @@ -130,12 +130,12 @@ namespace Crash DWORD copied = 0; do { executablePath.resize(executablePath.size() + MAX_PATH); - copied = GetModuleFileNameW(nullptr, executablePath.data(), executablePath.size()); + copied = GetModuleFileNameW(nullptr, executablePath.data(), static_cast(executablePath.size())); } while (copied >= executablePath.size()); executablePath.resize(copied); memset(mShm->mStartup.mLogFilePath, 0, sizeof(mShm->mStartup.mLogFilePath)); - int length = crashLogPath.length(); + size_t length = crashLogPath.length(); if (length >= MAX_LONG_PATH) length = MAX_LONG_PATH - 1; strncpy(mShm->mStartup.mLogFilePath, crashLogPath.c_str(), length); mShm->mStartup.mLogFilePath[length] = '\0'; diff --git a/components/esm/esmreader.cpp b/components/esm/esmreader.cpp index 30c38d739..b779d7f7f 100644 --- a/components/esm/esmreader.cpp +++ b/components/esm/esmreader.cpp @@ -203,9 +203,9 @@ void ESMReader::getSubName() } // reading the subrecord data anyway. - const size_t subNameSize = mCtx.subName.data_size(); + const int subNameSize = static_cast(mCtx.subName.data_size()); getExact(mCtx.subName.rw_data(), subNameSize); - mCtx.leftRec -= subNameSize; + mCtx.leftRec -= static_cast(subNameSize); } void ESMReader::skipHSub() @@ -327,7 +327,7 @@ std::string ESMReader::getString(int size) char *ptr = mBuffer.data(); getExact(ptr, size); - size = strnlen(ptr, size); + size = static_cast(strnlen(ptr, size)); // Convert to UTF8 and return if (mEncoder) diff --git a/components/esm/esmwriter.cpp b/components/esm/esmwriter.cpp index 09fca4b26..f65340f70 100644 --- a/components/esm/esmwriter.cpp +++ b/components/esm/esmwriter.cpp @@ -217,7 +217,7 @@ namespace ESM if (mCounting && !mRecords.empty()) { for (std::list::iterator it = mRecords.begin(); it != mRecords.end(); ++it) - it->size += size; + it->size += static_cast(size); } mStream->write(data, size); diff --git a/components/files/lowlevelfile.cpp b/components/files/lowlevelfile.cpp index 07915abba..d89ae5838 100644 --- a/components/files/lowlevelfile.cpp +++ b/components/files/lowlevelfile.cpp @@ -326,7 +326,7 @@ void LowLevelFile::seek (size_t position) { assert (mHandle != INVALID_HANDLE_VALUE); - if (SetFilePointer (mHandle, position, nullptr, SEEK_SET) == INVALID_SET_FILE_POINTER) + if (SetFilePointer (mHandle, static_cast(position), nullptr, SEEK_SET) == INVALID_SET_FILE_POINTER) if (GetLastError () != NO_ERROR) throw std::runtime_error ("A seek operation on a file failed."); } @@ -349,7 +349,7 @@ size_t LowLevelFile::read (void * data, size_t size) DWORD read; - if (!ReadFile (mHandle, data, size, &read, nullptr)) + if (!ReadFile (mHandle, data, static_cast(size), &read, nullptr)) throw std::runtime_error ("A read operation on a file failed."); return read; diff --git a/components/interpreter/runtime.cpp b/components/interpreter/runtime.cpp index 35ad93a8a..1cb8f558c 100644 --- a/components/interpreter/runtime.cpp +++ b/components/interpreter/runtime.cpp @@ -45,7 +45,7 @@ namespace Interpreter for (; index; --index) { - offset += std::strlen (literalBlock+offset) + 1; + offset += static_cast(std::strlen (literalBlock+offset)) + 1; if (offset / 4 >= static_cast (mCode[3])) throw std::out_of_range("out of range"); } diff --git a/components/widgets/list.cpp b/components/widgets/list.cpp index 73ca94929..e961b04cb 100644 --- a/components/widgets/list.cpp +++ b/components/widgets/list.cpp @@ -115,7 +115,7 @@ namespace Gui unsigned int MWList::getItemCount() { - return mItems.size(); + return static_cast(mItems.size()); } std::string MWList::getItemNameAt(unsigned int at) diff --git a/extern/osg-ffmpeg-videoplayer/audiodecoder.cpp b/extern/osg-ffmpeg-videoplayer/audiodecoder.cpp index c32794d2a..c78328b2e 100644 --- a/extern/osg-ffmpeg-videoplayer/audiodecoder.cpp +++ b/extern/osg-ffmpeg-videoplayer/audiodecoder.cpp @@ -255,7 +255,7 @@ size_t MovieAudioDecoder::read(char *stream, size_t len) size_t sampleSize = av_get_bytes_per_sample(mOutputSampleFormat); char* data[1]; data[0] = stream; - av_samples_set_silence((uint8_t**)data, 0, len/sampleSize, 1, mOutputSampleFormat); + av_samples_set_silence((uint8_t**)data, 0, static_cast(len/sampleSize), 1, mOutputSampleFormat); return len; }