summaryrefslogtreecommitdiff
path: root/lyt/common.cpp
diff options
context:
space:
mode:
authorTreeki <treeki@gmail.com>2010-10-07 15:56:13 +0200
committerTreeki <treeki@gmail.com>2010-10-07 15:56:13 +0200
commit5f6dad55d75cbe146ff7fefc899a02ccd39078ba (patch)
tree0822df14cc6d2c0329810c9ae7b4a4a638fe82b1 /lyt/common.cpp
downloadLayoutStudio-5f6dad55d75cbe146ff7fefc899a02ccd39078ba.tar.gz
LayoutStudio-5f6dad55d75cbe146ff7fefc899a02ccd39078ba.zip
initial commit -- everything compiles except for material.cpp. the material system still needs quite a bit of work; this will come in due time
Diffstat (limited to 'lyt/common.cpp')
-rw-r--r--lyt/common.cpp92
1 files changed, 92 insertions, 0 deletions
diff --git a/lyt/common.cpp b/lyt/common.cpp
new file mode 100644
index 0000000..f23e68a
--- /dev/null
+++ b/lyt/common.cpp
@@ -0,0 +1,92 @@
+#include "common.h"
+
+QByteArray PadByteArray(QByteArray original, int newLength, char padWith) {
+ QByteArray newArray = original;
+
+ if (original.length() > newLength) {
+ // the original array is longer than the length desired, so truncate it
+ newArray.truncate(newLength);
+
+ } else if (original.length() < newLength) {
+ // the original array is shorter, so pad it
+ int oldLength = original.length();
+ newArray.resize(newLength);
+
+ for (int i = oldLength; i < newLength; i++) {
+ newArray[i] = '\0';
+ }
+ }
+
+ return newArray;
+}
+
+QStringList ReadStringList(QDataStream &in) {
+ QStringList output;
+
+ quint16 count;
+ in >> (quint16&)count;
+ in.skipRawData(2); // padding
+
+ QVector<quint32> stringOffsets(count);
+
+ // save the initial offset so we can get the strings later
+ // string offsets are based on the first offset entry (after the count)
+ // NOT on the section offset
+ qint64 savedPos = in.device()->pos();
+
+ for (int i = 0; i < count; i++) {
+ quint32 offset;
+ in >> (quint32&)offset;
+ in.skipRawData(4); // unused?
+
+ stringOffsets[i] = offset;
+ }
+
+ // ok, now we can get the strings
+ for (int i = 0; i < count; i++) {
+ in.device()->seek(savedPos + stringOffsets[i]);
+
+ // how fun: no length is stored for each string, they're just zero
+ // terminated. so let's try to figure it out!
+ int stringLength = 0;
+ char check;
+
+ in >> (quint8&)check;
+ while (check != 0) {
+ stringLength += 1;
+ in >> (quint8&)check;
+ }
+
+ // now read the string
+ char *buffer = new char[stringLength];
+
+ in.device()->seek(savedPos + stringOffsets[i]);
+ in.readRawData(buffer, stringLength);
+
+ output.append(QString::fromAscii(buffer, stringLength));
+
+ delete[] buffer;
+
+
+ qDebug() << "Read string:" << output.last();
+ }
+
+ return output;
+}
+
+QString ReadFixedLengthASCII(QDataStream &in, int length) {
+ QByteArray readStr(length, '\0');
+ in.readRawData(readStr.data(), readStr.length());
+
+ QString str = QString::fromAscii(readStr.data(), readStr.length());
+ if (str.contains(QChar('\0')))
+ str.truncate(str.indexOf(QChar('\0')));
+
+ return str;
+}
+
+void WriteFixedLengthASCII(QDataStream &out, QString str, int length) {
+ QByteArray paddedStr = PadByteArray(str.toAscii(), length);
+ out.writeRawData(paddedStr.constData(), paddedStr.length());
+}
+