From e456b05ebf0e87c1b3b05939af50a1c8f30a0d87 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 31 Jan 2015 21:44:57 +0000 Subject: [PATCH] String cleanup. --- common/lc_application.cpp | 19 ++++++----- common/lc_basewindow.h | 16 +++++----- common/lc_file.cpp | 16 ---------- common/lc_file.h | 3 -- common/lc_model.h | 1 - common/lc_profile.cpp | 17 ++-------- common/lc_profile.h | 3 +- common/project.cpp | 61 ++++++++++-------------------------- qt/lc_qpovraydialog.cpp | 11 ++----- qt/lc_qpreferencesdialog.cpp | 10 +++--- 10 files changed, 44 insertions(+), 113 deletions(-) diff --git a/common/lc_application.cpp b/common/lc_application.cpp index 9544fe90..b72fa4ce 100644 --- a/common/lc_application.cpp +++ b/common/lc_application.cpp @@ -120,11 +120,10 @@ bool lcApplication::LoadPiecesLibrary(const char* LibPath, const char* LibraryIn return mLibrary->Load(EnvPath, LibraryCachePath); } - char CustomPath[LC_MAXPATH]; - strcpy(CustomPath, lcGetProfileString(LC_PROFILE_PARTS_LIBRARY)); + QString CustomPath = lcGetProfileString(LC_PROFILE_PARTS_LIBRARY); - if (CustomPath[0]) - return mLibrary->Load(CustomPath, LibraryCachePath); + if (!CustomPath.isEmpty()) + return mLibrary->Load(CustomPath.toLatin1().constData(), LibraryCachePath); // todo: qstring if (LibraryInstallPath && LibraryInstallPath[0]) { @@ -425,11 +424,11 @@ void lcApplication::ShowPreferencesDialog() Options.Preferences = mPreferences; - strcpy(Options.DefaultAuthor, lcGetProfileString(LC_PROFILE_DEFAULT_AUTHOR_NAME)); - strcpy(Options.ProjectsPath, lcGetProfileString(LC_PROFILE_PROJECTS_PATH)); - strcpy(Options.LibraryPath, lcGetProfileString(LC_PROFILE_PARTS_LIBRARY)); - strcpy(Options.POVRayPath, lcGetProfileString(LC_PROFILE_POVRAY_PATH)); - strcpy(Options.LGEOPath, lcGetProfileString(LC_PROFILE_POVRAY_LGEO_PATH)); + Options.DefaultAuthor = lcGetProfileString(LC_PROFILE_DEFAULT_AUTHOR_NAME); + Options.ProjectsPath = lcGetProfileString(LC_PROFILE_PROJECTS_PATH); + Options.LibraryPath = lcGetProfileString(LC_PROFILE_PARTS_LIBRARY); + Options.POVRayPath = lcGetProfileString(LC_PROFILE_POVRAY_PATH); + Options.LGEOPath = lcGetProfileString(LC_PROFILE_POVRAY_LGEO_PATH); Options.CheckForUpdates = lcGetProfileInt(LC_PROFILE_CHECK_UPDATES); Options.AASamples = CurrentAASamples; @@ -445,7 +444,7 @@ void lcApplication::ShowPreferencesDialog() if (!gMainWindow->DoDialog(LC_DIALOG_PREFERENCES, &Options)) return; - bool LibraryChanged = strcmp(Options.LibraryPath, lcGetProfileString(LC_PROFILE_PARTS_LIBRARY)); + bool LibraryChanged = Options.LibraryPath != lcGetProfileString(LC_PROFILE_PARTS_LIBRARY); bool AAChanged = CurrentAASamples != Options.AASamples; mPreferences = Options.Preferences; diff --git a/common/lc_basewindow.h b/common/lc_basewindow.h index d1fb0512..b88512fa 100644 --- a/common/lc_basewindow.h +++ b/common/lc_basewindow.h @@ -55,9 +55,9 @@ struct lcHTMLDialogOptions struct lcPOVRayDialogOptions { - char FileName[LC_MAXPATH]; - char POVRayPath[LC_MAXPATH]; - char LGEOPath[LC_MAXPATH]; + QString FileName; + QString POVRayPath; + QString LGEOPath; bool Render; }; @@ -93,11 +93,11 @@ struct lcPreferencesDialogOptions { lcPreferences Preferences; - char DefaultAuthor[101]; - char ProjectsPath[LC_MAXPATH]; - char LibraryPath[LC_MAXPATH]; - char POVRayPath[LC_MAXPATH]; - char LGEOPath[LC_MAXPATH]; + QString DefaultAuthor; + QString ProjectsPath; + QString LibraryPath; + QString POVRayPath; + QString LGEOPath; int CheckForUpdates; int AASamples; diff --git a/common/lc_file.cpp b/common/lc_file.cpp index b6af3561..d212cf14 100644 --- a/common/lc_file.cpp +++ b/common/lc_file.cpp @@ -4,7 +4,6 @@ #include #include #include "lc_file.h" -#include "str.h" // ============================================================================= // lcFile @@ -17,21 +16,6 @@ lcFile::~lcFile() { } -void lcFile::ReadString(String& Value) -{ - lcuint32 Length; - ReadU32(&Length, 1); - ReadBuffer(Value.GetBuffer(Length + 1), Length); - ((char*)Value)[Length] = 0; -} - -void lcFile::WriteString(const String& Value) -{ - lcuint32 Length = Value.GetLength(); - WriteU32(Length); - WriteBuffer((const char*)Value, Length); -} - // ============================================================================= // lcMemFile diff --git a/common/lc_file.h b/common/lc_file.h index 8c8a85c1..300309f0 100644 --- a/common/lc_file.h +++ b/common/lc_file.h @@ -32,9 +32,6 @@ public: WriteBuffer(Buffer, strlen(Buffer)); } - void ReadString(String& Value); - void WriteString(const String& Value); - virtual size_t ReadBuffer(void* Buffer, long Bytes) = 0; virtual size_t WriteBuffer(const void* Buffer, long Bytes) = 0; virtual void CopyFrom(lcMemFile& Source) = 0; diff --git a/common/lc_model.h b/common/lc_model.h index fee8a42b..9628c84f 100644 --- a/common/lc_model.h +++ b/common/lc_model.h @@ -3,7 +3,6 @@ #include "lc_file.h" #include "lc_math.h" -#include "str.h" #include "object.h" #define LC_SEL_NO_PIECES 0x01 // No pieces in model diff --git a/common/lc_profile.cpp b/common/lc_profile.cpp index e40d564e..ce839feb 100644 --- a/common/lc_profile.cpp +++ b/common/lc_profile.cpp @@ -132,17 +132,14 @@ float lcGetProfileFloat(LC_PROFILE_KEY Key) return Settings.value(QString("%1/%2").arg(Entry.mSection, Entry.mKey), Entry.mDefault.FloatValue).toFloat(); } -const char* lcGetProfileString(LC_PROFILE_KEY Key) +QString lcGetProfileString(LC_PROFILE_KEY Key) { lcProfileEntry& Entry = gProfileEntries[Key]; QSettings Settings; - static QByteArray Value; LC_ASSERT(Entry.mType == LC_PROFILE_ENTRY_STRING); - Value = Settings.value(QString("%1/%2").arg(Entry.mSection, Entry.mKey), Entry.mDefault.StringValue).toString().toLocal8Bit(); - - return Value.data(); + return Settings.value(QString("%1/%2").arg(Entry.mSection, Entry.mKey), Entry.mDefault.StringValue).toString(); } void lcGetProfileBuffer(LC_PROFILE_KEY Key, lcMemFile& Buffer) @@ -181,16 +178,6 @@ void lcSetProfileFloat(LC_PROFILE_KEY Key, float Value) Settings.setValue(QString("%1/%2").arg(Entry.mSection, Entry.mKey), Value); } -void lcSetProfileString(LC_PROFILE_KEY Key, const char* Value) -{ - lcProfileEntry& Entry = gProfileEntries[Key]; - QSettings Settings; - - LC_ASSERT(Entry.mType == LC_PROFILE_ENTRY_STRING); - - Settings.setValue(QString("%1/%2").arg(Entry.mSection, Entry.mKey), QString::fromUtf8(Value)); -} - void lcSetProfileString(LC_PROFILE_KEY Key, const QString& Value) { lcProfileEntry& Entry = gProfileEntries[Key]; diff --git a/common/lc_profile.h b/common/lc_profile.h index 5c6bd2bf..09c3e4d9 100644 --- a/common/lc_profile.h +++ b/common/lc_profile.h @@ -99,12 +99,11 @@ void lcRemoveProfileKey(LC_PROFILE_KEY Key); int lcGetProfileInt(LC_PROFILE_KEY Key); float lcGetProfileFloat(LC_PROFILE_KEY Key); -const char* lcGetProfileString(LC_PROFILE_KEY Key); +QString lcGetProfileString(LC_PROFILE_KEY Key); void lcGetProfileBuffer(LC_PROFILE_KEY Key, lcMemFile& Buffer); void lcSetProfileInt(LC_PROFILE_KEY Key, int Value); void lcSetProfileFloat(LC_PROFILE_KEY Key, float Value); -void lcSetProfileString(LC_PROFILE_KEY Key, const char* Value); // todo: qstring void lcSetProfileString(LC_PROFILE_KEY Key, const QString& Value); void lcSetProfileBuffer(LC_PROFILE_KEY Key, const lcMemFile& Buffer); diff --git a/common/project.cpp b/common/project.cpp index ac3d1d81..890c9260 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -1252,9 +1252,8 @@ void Project::ExportPOVRay() lcPOVRayDialogOptions Options; - memset(Options.FileName, 0, sizeof(Options.FileName)); - strcpy(Options.POVRayPath, lcGetProfileString(LC_PROFILE_POVRAY_PATH)); - strcpy(Options.LGEOPath, lcGetProfileString(LC_PROFILE_POVRAY_LGEO_PATH)); + Options.POVRayPath = lcGetProfileString(LC_PROFILE_POVRAY_PATH); + Options.LGEOPath = lcGetProfileString(LC_PROFILE_POVRAY_LGEO_PATH); Options.Render = lcGetProfileInt(LC_PROFILE_POVRAY_RENDER); if (!gMainWindow->DoDialog(LC_DIALOG_EXPORT_POVRAY, &Options)) @@ -1266,7 +1265,7 @@ void Project::ExportPOVRay() lcDiskFile POVFile; - if (!POVFile.Open(Options.FileName, "wt")) + if (!POVFile.Open(Options.FileName.toLatin1().constData(), "wt")) // todo: qstring { QMessageBox::warning(gMainWindow, tr("LeoCAD"), tr("Could not open file '%1' for writing.").arg(Options.FileName)); return; @@ -1302,27 +1301,15 @@ void Project::ExportPOVRay() LGEO_COLOR_GLITTER = 0x40 }; - char LGEOPath[LC_MAXPATH]; - strcpy(LGEOPath, lcGetProfileString(LC_PROFILE_POVRAY_LGEO_PATH)); - - if (LGEOPath[0]) + if (!Options.LGEOPath.isEmpty()) { lcDiskFile TableFile, ColorFile; - char Filename[LC_MAXPATH]; - int Length = strlen(LGEOPath); - - if ((LGEOPath[Length - 1] != '\\') && (LGEOPath[Length - 1] != '/')) - strcat(LGEOPath, "/"); - - strcpy(Filename, LGEOPath); - strcat(Filename, "lg_elements.lst"); - - if (!TableFile.Open(Filename, "rt")) + if (!TableFile.Open(QFileInfo(QDir(Options.LGEOPath), QLatin1String("lg_elements.lst")).absoluteFilePath().toLatin1().constData(), "rt")) { delete[] PieceTable; delete[] PieceFlags; - QMessageBox::information(gMainWindow, tr("LeoCAD"), tr("Could not find LGEO files in folder '%1'.").arg(LGEOPath)); + QMessageBox::information(gMainWindow, tr("LeoCAD"), tr("Could not find LGEO files in folder '%1'.").arg(Options.LGEOPath)); return; } @@ -1360,14 +1347,11 @@ void Project::ExportPOVRay() PieceFlags[Index] |= LGEO_PIECE_SLOPE; } - strcpy(Filename, LGEOPath); - strcat(Filename, "lg_colors.lst"); - - if (!ColorFile.Open(Filename, "rt")) + if (!ColorFile.Open(QFileInfo(QDir(Options.LGEOPath), QLatin1String("lg_colors.lst")).absoluteFilePath().toLatin1().constData(), "rt")) { delete[] PieceTable; delete[] PieceFlags; - QMessageBox::information(gMainWindow, tr("LeoCAD"), tr("Could not find LGEO files in folder '%1'.").arg(LGEOPath)); + QMessageBox::information(gMainWindow, tr("LeoCAD"), tr("Could not find LGEO files in folder '%1'.").arg(Options.LGEOPath)); return; } @@ -1392,7 +1376,7 @@ void Project::ExportPOVRay() const char* OldLocale = setlocale(LC_NUMERIC, "C"); - if (LGEOPath[0]) + if (!Options.LGEOPath.isEmpty()) { POVFile.WriteLine("#include \"lg_defs.inc\"\n#include \"lg_color.inc\"\n\n"); @@ -1522,31 +1506,18 @@ void Project::ExportPOVRay() if (Options.Render) { QStringList Arguments; - char Argument[LC_MAXPATH + 32]; - sprintf(Argument, "+I%s", Options.FileName); - Arguments.append(Argument); + Arguments.append(QString::fromLatin1("+I%1").arg(Options.FileName)); - if (Options.LGEOPath[0]) + if (!Options.LGEOPath.isEmpty()) { - sprintf(Argument, "+L%slg/", Options.LGEOPath); - Arguments.append(Argument); - sprintf(Argument, "+L%sar/", Options.LGEOPath); - Arguments.append(Argument); + Arguments.append(QString::fromLatin1("+L%1lg/").arg(Options.LGEOPath)); + Arguments.append(QString::fromLatin1("+L%1ar/").arg(Options.LGEOPath)); } - sprintf(Argument, "+o%s", Options.FileName); - char* Slash1 = strrchr(Argument, '\\'); - char* Slash2 = strrchr(Argument, '/'); - if (Slash1 || Slash2) - { - if (Slash1 > Slash2) - *(Slash1 + 1) = 0; - else - *(Slash2 + 1) = 0; - - Arguments.append(Argument); - } + QString AbsolutePath = QFileInfo(Options.FileName).absolutePath(); + if (!AbsolutePath.isEmpty()) + Arguments.append(QString::fromLatin1("+o%1").arg(AbsolutePath)); QProcess::execute(Options.POVRayPath, Arguments); } diff --git a/qt/lc_qpovraydialog.cpp b/qt/lc_qpovraydialog.cpp index ae8a95c0..9bf56265 100644 --- a/qt/lc_qpovraydialog.cpp +++ b/qt/lc_qpovraydialog.cpp @@ -32,14 +32,9 @@ void lcQPOVRayDialog::accept() return; } - strcpy(options->FileName, fileName.toLocal8Bit().data()); - - QString povrayPath = ui->povrayEdit->text(); - strcpy(options->POVRayPath, povrayPath.toLocal8Bit().data()); - - QString lgeoPath = ui->lgeoEdit->text(); - strcpy(options->LGEOPath, lgeoPath.toLocal8Bit().data()); - + options->FileName = fileName; + options->POVRayPath = ui->povrayEdit->text(); + options->LGEOPath = ui->lgeoEdit->text(); options->Render = ui->render->isChecked(); QDialog::accept(); diff --git a/qt/lc_qpreferencesdialog.cpp b/qt/lc_qpreferencesdialog.cpp index d6c20272..7dc2fbde 100644 --- a/qt/lc_qpreferencesdialog.cpp +++ b/qt/lc_qpreferencesdialog.cpp @@ -83,11 +83,11 @@ void lcQPreferencesDialog::accept() return; } - strcpy(options->DefaultAuthor, ui->authorName->text().toLocal8Bit().data()); - strcpy(options->ProjectsPath, ui->projectsFolder->text().toLocal8Bit().data()); - strcpy(options->LibraryPath, ui->partsLibrary->text().toLocal8Bit().data()); - strcpy(options->POVRayPath, ui->povrayExecutable->text().toLocal8Bit().data()); - strcpy(options->LGEOPath, ui->lgeoPath->text().toLocal8Bit().data()); + options->DefaultAuthor = ui->authorName->text(); + options->ProjectsPath = ui->projectsFolder->text(); + options->LibraryPath = ui->partsLibrary->text(); + options->POVRayPath = ui->povrayExecutable->text(); + options->LGEOPath = ui->lgeoPath->text(); options->Preferences.mMouseSensitivity = ui->mouseSensitivity->value(); options->CheckForUpdates = ui->checkForUpdates->currentIndex(); options->Preferences.mFixedAxes = ui->fixedDirectionKeys->isChecked();