File indexing completed on 2024-12-22 04:18:20
0001 /*************************************************************************** 0002 * * 0003 * Copyright : (C) 2012 Peter Kümmel * 0004 * email : syntheticpp@gmx.net * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 0013 #include <QtCore> 0014 0015 #include <math.h> 0016 #include <stdio.h> 0017 0018 #ifdef Q_CC_MSVC 0019 #define snprintf _snprintf_s 0020 #endif 0021 0022 0023 void printHelp() 0024 { 0025 printf("Arguments: <filename> <number of columns> <file size in MB>\n"); 0026 } 0027 0028 QByteArray calcLine(double x, int numCols) 0029 { 0030 QString cols; 0031 for (int i = 0; i < numCols; i++) { 0032 cols += QString("%1").arg(sin(x)); 0033 } 0034 cols += "\n"; 0035 return cols.toLatin1(); 0036 } 0037 0038 0039 QByteArray calcLine2(double x, int numCols) 0040 { 0041 QByteArray cols; 0042 for (int i = 0; i < numCols; i++) { 0043 char buffer[50]; 0044 snprintf(buffer, 50, "%.10f ", sin(x)); 0045 cols += QByteArray(buffer); 0046 } 0047 return cols + "\n"; 0048 } 0049 0050 0051 int main(int argc, char *argv[]) 0052 { 0053 QCoreApplication app(argc, argv); 0054 QStringList args = app.arguments(); 0055 if (args.size() != 4) { 0056 printHelp(); 0057 return 1; 0058 } 0059 0060 QString filename = args[1]; 0061 int cols = args[2].toInt(); 0062 qint64 mb = args[3].toInt(); 0063 0064 QFile file(filename); 0065 if (!file.open(QIODevice::WriteOnly)) { 0066 printf("Could not open file '%s'\n", qPrintable(QFileInfo(filename).absoluteFilePath())); 0067 return 1; 0068 } 0069 0070 printf("Writing file %s\n", qPrintable(QFileInfo(filename).absoluteFilePath())); 0071 int progress = 0; 0072 int progStep = mb > 1000 ? 1 : 10; 0073 double dx = 1.0 * M_PI / 100; 0074 double x = 0; 0075 qint64 fileSize = 0; 0076 qint64 maxSize = mb * 1024 * 1024; 0077 while (fileSize < maxSize) { 0078 file.write(calcLine2(x, cols)); 0079 x += dx; 0080 fileSize = file.size(); 0081 int done = 100.0 / progStep * fileSize / maxSize; 0082 if (done != progress) { 0083 printf("%i%%\n", done * progStep); 0084 progress = done; 0085 } 0086 } 0087 0088 return 0; 0089 }