You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.7 KiB

5 years ago
#include "header/FileHelper.h"
#include <QFile>
#include <QDir>
#include <QDebug>
bool FileHelper::ReadInputFile(QByteArray& arr, QString path, quint64 *width, quint64 *height)
{
QFile file(path);
file.open(QIODevice::ReadOnly);
char data[32000];
5 years ago
bool first = true;
while (file.readLine(data, 32000))
5 years ago
{
QString str(data);
if(str.length() == 0)
break;
(*height)++;
str = str.remove(str.length() - 1, 1);
auto sl = str.split(",");
if(first)
{
*width = sl.length();
first = false;
}
else if (sl.length() != *width)
{
qDebug() << "Some thing wrong here " << *width << " " << sl.length();
return false;
}
for(int i = 0; i < sl.length(); i++)
{
auto t = sl[i].PARSE;
arr.append(static_cast<char>(t));
arr.append(static_cast<char>((t >> 8)));
arr.append(static_cast<char>((t >> 16)));
arr.append(static_cast<char>((t >> 24)));
#ifdef USE_DBL
arr.append(static_cast<char>((t >> 32)));
arr.append(static_cast<char>((t >> 40)));
arr.append(static_cast<char>((t >> 48)));
arr.append(static_cast<char>((t >> 56)));
#endif
}
}
return true;
}
bool FileHelper::ReadInputFile(myint *arr, QString path, quint64 *width, quint64 *height)
{
QFile file(path);
file.open(QIODevice::ReadOnly);
char data[32000];
5 years ago
bool first = true;
int index = 0;
while (file.readLine(data, 32000))
5 years ago
{
QString str(data);
if(str.length() == 0)
break;
(*height)++;
str = str.remove(str.length() - 1, 1);
auto sl = str.split(",");
if(first)
{
*width = sl.length();
first = false;
}
else if (sl.length() != *width)
{
qDebug() << "Some thing wrong here " << *width << " " << sl.length();
return false;
}
for(int i = 0; i < sl.length(); i++)
{
5 years ago
auto x = sl[i].toInt(Q_NULLPTR,16);
5 years ago
arr[index++] = x;
}
}
return true;
}
void FileHelper::GetFileSIze(QString path, quint64 *width, quint64 *height)
{
QFile file(path);
file.open(QIODevice::ReadOnly);
char data[20000];
bool first = true;
while (file.readLine(data, 20000))
{
QString str(data);
if(str.length() == 0)
break;
(*height)++;
str = str.remove(str.length() - 1, 1);
auto sl = str.split(",");
*width = sl.length();
}
}
5 years ago
bool FileHelper::WriteOutputFile(char* arr, QString path, quint64 width, quint64 height)
{
QFile file(path);
file.open(QIODevice::WriteOnly);
for(int i = 0; i < height;i++)
{
int index = i * sizeof (myflt) * width;
QString str;
for(int j = 0; j < width; j++)
{
#ifdef USE_DBL
quint64 temp = 0;
temp = (((quint64)(arr[index++] & 0xFF))) | (((quint64)(arr[index++] & 0xFF)) << 8)
| (((quint64)(arr[index++] & 0xFF)) << 16) | (((quint64)(arr[index++] & 0xFF)) << 24)
| (((quint64)(arr[index++] & 0xFF)) << 32) | (((quint64)(arr[index++] & 0xFF)) << 40)
| (((quint64)(arr[index++] & 0xFF)) << 48) | (((quint64)(arr[index++] & 0xFF)) << 56);
auto d = *((double*)(&temp));
#else
quint32 temp = 0;
temp = (((quint64)(arr[index++] & 0xFF))) | (((quint64)(arr[index++] & 0xFF)) << 8)
| (((quint64)(arr[index++] & 0xFF)) << 16) | (((quint64)(arr[index++] & 0xFF)) << 24);
auto d = *((float*)(&temp));
#endif
str += QString("%1").arg(temp, 8, 16, QChar('0'));
5 years ago
if(j != width - 1)
str += ",";
}
str += "\n";
file.write(str.toLocal8Bit());
}
return true;
5 years ago
}
bool FileHelper::WriteOutputFile(myflt* arr, QString path, quint64 width, quint64 height)
{
QFile file(path);
file.open(QIODevice::WriteOnly);
for(int i = 0; i < height;i++)
{
int index = i * width;
QString str;
for(int j = 0; j < width; j++)
{
str += QString::number(arr[index + j], 'f', 6);
if(j != width - 1)
str += ",";
}
str += "\n";
file.write(str.toLocal8Bit());
}
return true;
5 years ago
}