forked from GerhardHoffmann/DCLibraries
		
	
		
			
				
	
	
		
			946 lines
		
	
	
		
			25 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			946 lines
		
	
	
		
			25 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
// written by Thomas Sax, Jan.2022
 | 
						|
#include <cstdint>
 | 
						|
#include "datei.h"
 | 
						|
 | 
						|
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
// ------------------------------------------------------ create csv file -------------------------------
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
 | 
						|
QByteArray datei_writeArray, datei_tempArray;
 | 
						|
 | 
						|
void csv_startCreatingFile(void)
 | 
						|
{
 | 
						|
    datei_writeArray.clear();
 | 
						|
    datei_tempArray.clear();
 | 
						|
}
 | 
						|
 | 
						|
void csv_addTextToFile(QString myText)
 | 
						|
{
 | 
						|
    datei_writeArray.append(myText.toLatin1());
 | 
						|
    datei_writeArray.append(FILESEPERATOR);
 | 
						|
}
 | 
						|
 | 
						|
void csv_addIntToFile(int myValue)
 | 
						|
{
 | 
						|
    //qulonglong ullt=12345678901234567890;   // max 1,844 x10^19
 | 
						|
    datei_tempArray.setNum(myValue,10);  // accepted types: short, ushort, int, uint,
 | 
						|
                            // qlonglong, qulonglong, float, double
 | 
						|
                            // numerbase can be 2...36(!),10=dec
 | 
						|
 | 
						|
    datei_writeArray.append(datei_tempArray);
 | 
						|
    datei_writeArray.append(FILESEPERATOR);
 | 
						|
}
 | 
						|
 | 
						|
void csv_addUintToFile(uint myValue)
 | 
						|
{
 | 
						|
    datei_tempArray.setNum(myValue,10);
 | 
						|
    datei_writeArray.append(datei_tempArray);
 | 
						|
    datei_writeArray.append(FILESEPERATOR);
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
void csv_addLongvalToFile(qlonglong myValue)
 | 
						|
{
 | 
						|
    datei_tempArray.setNum(myValue,10);
 | 
						|
    datei_writeArray.append(datei_tempArray);
 | 
						|
    datei_writeArray.append(FILESEPERATOR);
 | 
						|
}
 | 
						|
 | 
						|
void csv_addUlongvalToFile(qulonglong myValue)
 | 
						|
{
 | 
						|
    datei_tempArray.setNum(myValue,10);
 | 
						|
    datei_writeArray.append(datei_tempArray);
 | 
						|
    datei_writeArray.append(FILESEPERATOR);
 | 
						|
}
 | 
						|
 | 
						|
/*
 | 
						|
void csv_addCurrentTimeToFile(void)
 | 
						|
{
 | 
						|
    uint8_t hour, minute, sec, ui8buf[20];
 | 
						|
    char buf[20];
 | 
						|
 | 
						|
    config_getSysTime(&hour, &minute, &sec);
 | 
						|
    GetTimeString(hour, minute, sec, 0, 1, ui8buf);
 | 
						|
    for (uint8_t nn=0; nn<20; nn++)
 | 
						|
        buf[nn]=char(ui8buf[nn]);
 | 
						|
    datei_writeArray.append(buf,8);             // time string
 | 
						|
    datei_writeArray.append(FILESEPERATOR);
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
void csv_addCurrentDateToFile(void)
 | 
						|
{
 | 
						|
    uint16_t year;
 | 
						|
    uint8_t month, day, ui8buf[20];
 | 
						|
    char buf[20];
 | 
						|
 | 
						|
    config_getSystemDate(&year, &month, &day);
 | 
						|
    //qDebug()<<"date year: "<<year;
 | 
						|
    GetDateString(day, month, 0x20, uint8_t(year%100), 0, 0, ui8buf);
 | 
						|
    for (uint8_t nn=0; nn<20; nn++)
 | 
						|
        buf[nn]=char(ui8buf[nn]);
 | 
						|
    datei_writeArray.append(buf, 10);            // date string
 | 
						|
    datei_writeArray.append(NEWLINEINFILE);
 | 
						|
 | 
						|
}
 | 
						|
*/
 | 
						|
 | 
						|
void csv_addNewlineToFile(void)
 | 
						|
{
 | 
						|
    datei_writeArray.chop(1);      // Komma weg
 | 
						|
    datei_writeArray.append(NEWLINEINFILE);
 | 
						|
}
 | 
						|
 | 
						|
QByteArray csv_readbackArray(void)
 | 
						|
{
 | 
						|
    return datei_writeArray;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
/*
 | 
						|
QByteArray csv_writeContent_testValues(void)
 | 
						|
{
 | 
						|
    QByteArray myBA, tmpBA;
 | 
						|
    uint8_t modCount=5, modAddr=23;
 | 
						|
    uint8_t hour, minute, sec, month, day, ui8buf[20];
 | 
						|
    uint16_t year, modType=45678;
 | 
						|
    char buf[20];
 | 
						|
    uint32_t modNrDIs=1234567890;
 | 
						|
    uint8_t  modNrAIs=4, modNrCtr=2, modNrDOs=8;
 | 
						|
    int       modNrAOs=-2;
 | 
						|
       
 | 
						|
    myBA.clear();
 | 
						|
    tmpBA.clear();
 | 
						|
    myBA.append("scan time");
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    
 | 
						|
    datei_getSysTime(&hour, &minute, &sec);
 | 
						|
    GetTimeString(hour, minute, sec, 0, 1, ui8buf);        
 | 
						|
    for (uint8_t nn=0; nn<20; nn++)
 | 
						|
        buf[nn]=char(ui8buf[nn]);
 | 
						|
    myBA.append(buf,8);             // time string
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    
 | 
						|
    datei_getSystemDate(&year, &month, &day);
 | 
						|
    //qDebug()<<"date year: "<<year;
 | 
						|
    GetDateString(day, month, 0x20, uint8_t(year%100), 0, 0, ui8buf);        
 | 
						|
    for (uint8_t nn=0; nn<20; nn++)
 | 
						|
        buf[nn]=char(ui8buf[nn]);          
 | 
						|
    myBA.append(buf, 10);            // date string
 | 
						|
    myBA.append(NEWLINEINFILE);
 | 
						|
    
 | 
						|
    myBA.append("number of modules");
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    tmpBA.setNum(modCount,10);   //2nd para = number base 2, 8, 10 or 16 (bin, oct, dec, hex)
 | 
						|
    myBA.append(tmpBA);
 | 
						|
    myBA.append(NEWLINEINFILE);
 | 
						|
 | 
						|
    myBA.append("busaddr");
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    myBA.append("type");
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    myBA.append("NrOfDI");
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    myBA.append("NrOfAI");
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    myBA.append("NrOfCtrIn");
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    myBA.append("NrOfDO");
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    myBA.append("NrOfAO");
 | 
						|
    myBA.append(NEWLINEINFILE);
 | 
						|
 | 
						|
    tmpBA.setNum(modAddr,10);
 | 
						|
    myBA.append(tmpBA);
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    tmpBA.setNum(modType,10);
 | 
						|
    myBA.append(tmpBA);
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    tmpBA.setNum(modNrDIs,10);
 | 
						|
    myBA.append(tmpBA);
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    tmpBA.setNum(modNrAIs,10);
 | 
						|
    myBA.append(tmpBA);
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    tmpBA.setNum(modNrCtr,10);
 | 
						|
    myBA.append(tmpBA);
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    tmpBA.setNum(modNrDOs,10);
 | 
						|
    myBA.append(tmpBA);
 | 
						|
    myBA.append(FILESEPERATOR);
 | 
						|
    tmpBA.setNum(modNrAOs,10);
 | 
						|
    myBA.append(tmpBA);
 | 
						|
    myBA.append(NEWLINEINFILE);
 | 
						|
    return myBA;
 | 
						|
}
 | 
						|
*/
 | 
						|
 | 
						|
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
// ------------------------------------------------------ parse csv file -------------------------------
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
 | 
						|
// first: QByteArray datei_readFromFile(QString filename);
 | 
						|
 | 
						|
 | 
						|
uint32_t csv_nrOfEntriesInFile(QByteArray readFromFile)
 | 
						|
{
 | 
						|
    // count sequences between FILESEPERATOR and NEWLINEINFILE    
 | 
						|
    uint32_t   filSize=0, pp=0;
 | 
						|
    char       oneByt=0;
 | 
						|
    
 | 
						|
    int  filLen=readFromFile.size();
 | 
						|
    if(filLen>1)
 | 
						|
        filSize=uint32_t(filLen);
 | 
						|
    else
 | 
						|
        return 0;
 | 
						|
        
 | 
						|
    // 1) find position of seperators
 | 
						|
    for (uint32_t ii=0; ii<filSize; ii++)
 | 
						|
    {
 | 
						|
        oneByt=readFromFile[ii];
 | 
						|
        if (oneByt==FILESEP1 || oneByt==FILESEP2 || oneByt==NEWLINEINFILE)
 | 
						|
            pp++;
 | 
						|
    }
 | 
						|
    // now: pp = number of seperators
 | 
						|
    // oneByt = last byte in file. If it's not a seperator then 
 | 
						|
    // there's one more entry (last entry without termination)
 | 
						|
    if (oneByt !=FILESEP1 && oneByt !=FILESEP2 && oneByt !=NEWLINEINFILE)
 | 
						|
        pp++;
 | 
						|
    //qDebug()<<"csv: nr of sequences="<< pp;
 | 
						|
    return pp;
 | 
						|
}
 | 
						|
 | 
						|
QByteArray csv_getOneFileSequence(QByteArray sourceFile, uint32_t sequNr)
 | 
						|
{
 | 
						|
    // seperate file content in single sequences between FILESEPERATOR and NEWLINEINFILE
 | 
						|
    // and return "entryNr" - entry 
 | 
						|
    // for this first step leave data type QByteArray
 | 
						|
    // 2nd step can change in numbers and strings
 | 
						|
    QByteArray sequence;
 | 
						|
    uint32_t   sepPos[MAXNUMBEROFSEQUENCES];
 | 
						|
    uint32_t   filSize=0, pp=0, ii, start=0, ende=0;
 | 
						|
    char       oneByt;
 | 
						|
    int        filLen, mm; 
 | 
						|
    
 | 
						|
    filLen=sourceFile.size();
 | 
						|
    //qDebug()<<"fillen="<< filLen;
 | 
						|
    if(filLen<10)
 | 
						|
        return "";
 | 
						|
    filSize=uint32_t(filLen);
 | 
						|
 | 
						|
    if (sequNr>MAXNUMBEROFSEQUENCES)
 | 
						|
        return "";    
 | 
						|
 | 
						|
    // 1) find position of seperators
 | 
						|
    for (ii=0; ii<filSize; ii++)
 | 
						|
    {
 | 
						|
        oneByt=sourceFile[ii];
 | 
						|
        if (oneByt==FILESEP1 || oneByt==FILESEP2 || oneByt==NEWLINEINFILE)
 | 
						|
        {
 | 
						|
            sepPos[pp++]=ii;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    // now: pp = number of entries
 | 
						|
    //qDebug()<<"nr of seperators="<< pp;
 | 
						|
 | 
						|
    if (sequNr>=pp)
 | 
						|
        return "";
 | 
						|
 | 
						|
    // 2) get sequence
 | 
						|
    if (sequNr==0)
 | 
						|
    {
 | 
						|
        start=0;
 | 
						|
        ende=sepPos[sequNr];        
 | 
						|
    } else
 | 
						|
    if (sequNr>0)
 | 
						|
    {
 | 
						|
        start=sepPos[sequNr-1]+1;
 | 
						|
        ende=sepPos[sequNr];    
 | 
						|
    }    
 | 
						|
 | 
						|
    //qDebug()<<"datei getOneFileSequence start/ende: "<<start << " " << ende;
 | 
						|
    if (start>=ende)
 | 
						|
        return "";
 | 
						|
        //return "-err3-";
 | 
						|
    sequence.clear();
 | 
						|
    //batmp.clear();
 | 
						|
    pp=0;
 | 
						|
    for (ii=start; ii<ende; ii++)
 | 
						|
    {
 | 
						|
        mm=int(ii);
 | 
						|
        if (mm>=int(filSize))
 | 
						|
            mm=0;
 | 
						|
        oneByt=sourceFile.at(mm);
 | 
						|
        sequence.append(oneByt);
 | 
						|
    }    
 | 
						|
    return sequence;    
 | 
						|
}
 | 
						|
 | 
						|
int csv_getEntryAsInt(QByteArray sourceFile, uint32_t sequNr)
 | 
						|
{
 | 
						|
    QByteArray myBA, myVA;
 | 
						|
    int        entry=0;
 | 
						|
    bool ok;
 | 
						|
 | 
						|
    myVA.clear();
 | 
						|
    myBA = csv_getOneFileSequence(sourceFile, sequNr);
 | 
						|
    //qDebug()<<"datei getEntryAsInt, sequence: " << myBA;
 | 
						|
 | 
						|
    entry=myBA.toInt(&ok,16);
 | 
						|
    if (ok)
 | 
						|
    {
 | 
						|
        //qDebug()<<"datei getEntryAsInt, number: " << entry;
 | 
						|
        return entry;
 | 
						|
    }
 | 
						|
    //qDebug()<<"datei getEntryAsInt, error " << myBA;
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 | 
						|
int32_t csv_getEntryAsLong(QByteArray sourceFile, uint32_t sequNr)
 | 
						|
{
 | 
						|
    QByteArray myBA = csv_getOneFileSequence(sourceFile, sequNr);
 | 
						|
    long        entry=0;
 | 
						|
    bool ok;
 | 
						|
    
 | 
						|
    entry=myBA.toLong(&ok,10);
 | 
						|
    if (ok)
 | 
						|
        return entry;
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 | 
						|
uint8_t csv_getEntryAsUshort(QByteArray sourceFile, uint32_t sequNr)
 | 
						|
{
 | 
						|
    QByteArray myBA = csv_getOneFileSequence(sourceFile, sequNr);
 | 
						|
    uint8_t        entry=0;
 | 
						|
    bool ok;
 | 
						|
    
 | 
						|
    entry=uint8_t(myBA.toUShort(&ok,10));
 | 
						|
    if (ok)
 | 
						|
        return entry;
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 | 
						|
uint16_t csv_getEntryAsUint(QByteArray sourceFile, uint32_t sequNr)
 | 
						|
{
 | 
						|
    QByteArray myBA = csv_getOneFileSequence(sourceFile, sequNr);
 | 
						|
    uint16_t        entry=0;
 | 
						|
    bool ok;
 | 
						|
    
 | 
						|
    entry=uint16_t(myBA.toUInt(&ok,10));
 | 
						|
    if (ok)
 | 
						|
        return entry;
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 | 
						|
uint32_t csv_getEntryAsUlong(QByteArray sourceFile, uint32_t sequNr)
 | 
						|
{
 | 
						|
    QByteArray myBA = csv_getOneFileSequence(sourceFile, sequNr);
 | 
						|
    uint32_t        entry=0;
 | 
						|
    bool ok;
 | 
						|
    
 | 
						|
    entry=myBA.toULong(&ok,10);
 | 
						|
    if (ok)
 | 
						|
        return entry;
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 | 
						|
uint64_t csv_getEntryAs2Ulong(QByteArray sourceFile, uint32_t sequNr)
 | 
						|
{
 | 
						|
    QByteArray myBA = csv_getOneFileSequence(sourceFile, sequNr);
 | 
						|
    uint64_t        entry=0;
 | 
						|
    bool ok;
 | 
						|
    
 | 
						|
    entry=myBA.toULongLong(&ok,10);
 | 
						|
    if (ok)
 | 
						|
        return entry;
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
QString csv_getEntryAsString(QByteArray sourceFile, uint32_t sequNr)
 | 
						|
{
 | 
						|
    QByteArray myBA = csv_getOneFileSequence(sourceFile, sequNr);
 | 
						|
    QString     entry;
 | 
						|
 | 
						|
    //qDebug()<<"datei getEntryAsString, sequence: " << myBA;
 | 
						|
    entry=myBA.toStdString().c_str();
 | 
						|
    return entry;
 | 
						|
}
 | 
						|
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
// ------------------------------------------------------ create Json file -------------------------------
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
 | 
						|
/*
 | 
						|
 example
 | 
						|
QString str = "{"
 | 
						|
                  "  \"Herausgeber\": \"Xema\","
 | 
						|
                  "  \"Nummer\": \"1234-5678-9012-3456\","
 | 
						|
                  "  \"Deckung\": 2e+6,"
 | 
						|
                  "  \"Währung\": \"EURO\","
 | 
						|
                  "  \"Inhaber\": {"
 | 
						|
                  "    \"Name\": \"Mustermann\","
 | 
						|
                  "    \"Vorname\": \"Max\","
 | 
						|
                  "    \"männlich\": true,"
 | 
						|
                  "    \"Hobbys\": [ \"Reiten\", \"Golfen\", \"Lesen\" ],"
 | 
						|
                  "    \"Alter\": 42,"
 | 
						|
                  "    \"Kinder\": [],"
 | 
						|
                  "    \"Partner\": null"
 | 
						|
                  "  }"
 | 
						|
                  "}";
 | 
						|
 | 
						|
*/
 | 
						|
 | 
						|
QString myJsonCon;
 | 
						|
QString tmpStr;
 | 
						|
 | 
						|
void json_startRecord(void)
 | 
						|
{
 | 
						|
    myJsonCon.clear();
 | 
						|
    tmpStr.clear();
 | 
						|
    myJsonCon.append('{');
 | 
						|
}
 | 
						|
 | 
						|
void json_enterIntToRecord(QString attribute, ulong i_value)
 | 
						|
{
 | 
						|
    tmpStr.clear();
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(attribute);
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(':');
 | 
						|
    tmpStr.setNum(i_value);
 | 
						|
    myJsonCon.append(tmpStr);
 | 
						|
    myJsonCon.append(',');
 | 
						|
    myJsonCon.append(NEWLINEINFILE);
 | 
						|
}
 | 
						|
 | 
						|
void json_enterTextToRecord(QString attribute, QString txt_value)
 | 
						|
{
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(attribute);
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(':');
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(txt_value);
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(',');
 | 
						|
    myJsonCon.append(NEWLINEINFILE);
 | 
						|
}
 | 
						|
/*
 | 
						|
void json_addCurrentTimeToRecord(QString attribute)
 | 
						|
{
 | 
						|
    uint8_t hour, minute, sec, ui8buf[20];
 | 
						|
    //char buf[20];
 | 
						|
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(attribute);
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(':');
 | 
						|
    myJsonCon.append('"');
 | 
						|
 | 
						|
    datei_getSysTime(&hour, &minute, &sec);
 | 
						|
    GetTimeString(hour, minute, sec, 0, 1, ui8buf);
 | 
						|
    for (uint8_t nn=0; nn<8; nn++)
 | 
						|
        myJsonCon.append(ui8buf[nn]);
 | 
						|
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(',');
 | 
						|
    myJsonCon.append(NEWLINEINFILE);
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
void json_addCurrentDateToRecord(QString attribute)
 | 
						|
{
 | 
						|
    uint16_t year;
 | 
						|
    uint8_t month, day, ui8buf[20];
 | 
						|
    //char buf[20];
 | 
						|
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(attribute);
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(':');
 | 
						|
    myJsonCon.append('"');
 | 
						|
 | 
						|
    datei_getSystemDate(&year, &month, &day);
 | 
						|
    GetDateString(day, month, 0x20, uint8_t(year%100), 0, 0, ui8buf);
 | 
						|
    for (uint8_t nn=0; nn<10; nn++)
 | 
						|
        myJsonCon.append(ui8buf[nn]);
 | 
						|
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(',');
 | 
						|
    myJsonCon.append(NEWLINEINFILE);
 | 
						|
 | 
						|
}
 | 
						|
*/
 | 
						|
 | 
						|
void json_enterArrayToRecord(QString attribute, uint8_t *buf, ulong nrofVals)
 | 
						|
{
 | 
						|
    // add array of numbers with "nrofVals" elements
 | 
						|
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(attribute);
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(':');
 | 
						|
    myJsonCon.append('[');  // eckig!!!
 | 
						|
    for (ulong ul=0; ul<nrofVals; ul++)
 | 
						|
    {
 | 
						|
        tmpStr.setNum(buf[ul]);
 | 
						|
        myJsonCon.append(tmpStr);
 | 
						|
        myJsonCon.append(',');
 | 
						|
    }
 | 
						|
    myJsonCon.chop(1);      // Komma weg
 | 
						|
    myJsonCon.append(']');  // eckig!!!
 | 
						|
    myJsonCon.append(',');
 | 
						|
    myJsonCon.append(NEWLINEINFILE);
 | 
						|
}
 | 
						|
 | 
						|
void json_enterStructToRecord(QString attribute)
 | 
						|
{
 | 
						|
    // every call must be concluded with "json_finishFile()"
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(attribute);
 | 
						|
    myJsonCon.append('"');
 | 
						|
    myJsonCon.append(':');
 | 
						|
    myJsonCon.append('{');  // geschweift!!
 | 
						|
    myJsonCon.append(NEWLINEINFILE);
 | 
						|
}
 | 
						|
 | 
						|
void json_finishStruct(void)
 | 
						|
{
 | 
						|
    myJsonCon.chop(2);  // remove , and \r from the end
 | 
						|
    myJsonCon.append('}');
 | 
						|
    myJsonCon.append(',');
 | 
						|
    myJsonCon.append(NEWLINEINFILE);
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
void json_finishRecord(void)
 | 
						|
{
 | 
						|
    myJsonCon.chop(2);  // remove , and \r from the end
 | 
						|
    myJsonCon.append(NEWLINEINFILE);
 | 
						|
    myJsonCon.append('}');
 | 
						|
    myJsonCon.append(NEWLINEINFILE);
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
QString json_readbackRecordStr(void)
 | 
						|
{
 | 
						|
    return myJsonCon;
 | 
						|
}
 | 
						|
 | 
						|
QByteArray json_readbackRecordBa(void)
 | 
						|
{            
 | 
						|
    return myJsonCon.toLatin1();
 | 
						|
}
 | 
						|
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
// ------------------------------------------------------ parse Json file -------------------------------
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
 | 
						|
/*
 | 
						|
example Json File:
 | 
						|
{"temperature":28,
 | 
						|
"snow":"no",
 | 
						|
"Zeit":"16_21_45",
 | 
						|
"sunny":"12h",
 | 
						|
"humidity":75,
 | 
						|
"wann ":"24.01.2022",
 | 
						|
"unterstruktur":{
 | 
						|
"day of week":"tuesday",
 | 
						|
"year":22,
 | 
						|
"month":1,
 | 
						|
"day":24},
 | 
						|
"fast am":"Ende",
 | 
						|
"Puffer":[8,3,9,2,10]
 | 
						|
}
 | 
						|
 */
 | 
						|
// first: QByteArray datei_readFromFile(QString filename);
 | 
						|
 | 
						|
 | 
						|
int json_nrOfPairsInFile(QByteArray filename)
 | 
						|
{
 | 
						|
    QJsonDocument jdoc = QJsonDocument::fromJson(filename);
 | 
						|
    QJsonObject jobj = jdoc.object();
 | 
						|
 | 
						|
    // key value pair consisting of key (unique string) and value (QJsonValue)
 | 
						|
    int nrOfPairs=jobj.size();
 | 
						|
 | 
						|
    qDebug() << "my Json file has got: " << nrOfPairs<< "pairs";
 | 
						|
    return nrOfPairs;
 | 
						|
}
 | 
						|
 | 
						|
bool json_exists(QByteArray filename, QString searchForKey)
 | 
						|
{
 | 
						|
    // look for "searchForKey" =name of the pair (left of : )
 | 
						|
    QJsonDocument jdoc = QJsonDocument::fromJson(filename);
 | 
						|
    QJsonObject jobj = jdoc.object();
 | 
						|
 | 
						|
    if (jobj.contains(searchForKey))
 | 
						|
        return true;
 | 
						|
    return false;
 | 
						|
}
 | 
						|
 | 
						|
bool json_remove(QByteArray filename, QString searchFor)
 | 
						|
{
 | 
						|
    // look for "searchFor" =name of the pair (left of : ) and remove the record from file
 | 
						|
    QJsonDocument jdoc = QJsonDocument::fromJson(filename);
 | 
						|
    QJsonObject jobj = jdoc.object();
 | 
						|
 | 
						|
    if (jobj.contains(searchFor))
 | 
						|
    {
 | 
						|
        jobj.remove(searchFor);
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
    return false;
 | 
						|
}
 | 
						|
 | 
						|
QString json_searchForStringInFile(QByteArray filename, QString searchFor)
 | 
						|
{
 | 
						|
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String
 | 
						|
    QJsonDocument jdoc = QJsonDocument::fromJson(filename);
 | 
						|
    QJsonObject jobj = jdoc.object();
 | 
						|
 | 
						|
    if (jobj.contains(searchFor))
 | 
						|
    {
 | 
						|
        return jobj[searchFor].toString();    // toObject(); toArray();
 | 
						|
    } else
 | 
						|
    {
 | 
						|
        //qDebug() << "pairname not found in Json file";
 | 
						|
        return "";
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
int json_searchForIntInFile(QByteArray filename, QString searchFor)
 | 
						|
{
 | 
						|
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String
 | 
						|
    QJsonDocument jdoc = QJsonDocument::fromJson(filename);
 | 
						|
    QJsonObject jobj = jdoc.object();
 | 
						|
 | 
						|
    if (jobj.contains(searchFor))
 | 
						|
    {
 | 
						|
        return jobj[searchFor].toInt();    // toObject(); toArray();
 | 
						|
    } else
 | 
						|
    {
 | 
						|
        //qDebug() << "number not found in Json file";
 | 
						|
        return 0;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
bool json_searchForObjectInFile(QByteArray filename, QString searchFor, QJsonObject *oneObject)
 | 
						|
{
 | 
						|
    // return an object from the json file
 | 
						|
    QJsonDocument jdoc = QJsonDocument::fromJson(filename);
 | 
						|
    QJsonObject jobj = jdoc.object();
 | 
						|
 | 
						|
    if (jobj.contains(searchFor))
 | 
						|
    {
 | 
						|
        *oneObject = jobj[searchFor].toObject();
 | 
						|
        return true;
 | 
						|
    } else
 | 
						|
    {
 | 
						|
        //qDebug() << "Object not found in Json file";
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
int json_nrOfPairsInObject(QJsonObject objname)
 | 
						|
{
 | 
						|
    int nrOfPairs=objname.size();
 | 
						|
    qDebug() << "my Json Object has got: " << nrOfPairs<< "pairs";
 | 
						|
    return nrOfPairs;
 | 
						|
}
 | 
						|
 | 
						|
QString json_searchForStringInObject(QJsonObject objname, QString searchFor)
 | 
						|
{
 | 
						|
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String
 | 
						|
 | 
						|
    if (objname.contains(searchFor))
 | 
						|
    {
 | 
						|
        return objname[searchFor].toString();
 | 
						|
    } else
 | 
						|
    {
 | 
						|
        //qDebug() << "string not found in Json object";
 | 
						|
        return "";
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
int json_searchForIntInObject(QJsonObject objname, QString searchFor)
 | 
						|
{
 | 
						|
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String
 | 
						|
 | 
						|
    if (objname.contains(searchFor))
 | 
						|
    {
 | 
						|
        return objname[searchFor].toInt();
 | 
						|
    } else
 | 
						|
    {
 | 
						|
        //qDebug() << "number not found in Json file";
 | 
						|
        return 0;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
bool json_searchForArrayInFile(QByteArray filename, QString searchFor, QJsonArray *oneArray)
 | 
						|
{
 | 
						|
    // look for "searchFor" =name of the pair (left of : ) and return value of this pair (right of : ) as String
 | 
						|
    QJsonDocument jdoc = QJsonDocument::fromJson(filename);
 | 
						|
    QJsonObject jobj = jdoc.object();
 | 
						|
 | 
						|
    if (jobj.contains(searchFor))
 | 
						|
    {
 | 
						|
        *oneArray = jobj[searchFor].toArray();
 | 
						|
        return true;
 | 
						|
    } else
 | 
						|
    {
 | 
						|
        //qDebug() << "Array not found in Json file";
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
int json_nrOfValuesInArray(QJsonArray arrayname)
 | 
						|
{
 | 
						|
    int nrOfPairs=arrayname.size();
 | 
						|
    qDebug() << "my Json Array has got: " << nrOfPairs<< "values";
 | 
						|
    return nrOfPairs;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
bool json_getValuesOfArray(QJsonArray arrayname, int *buf, int MaxBufferSize)
 | 
						|
{
 | 
						|
    // assuming that the array consists of integers
 | 
						|
 | 
						|
    /* copy to local buffer:
 | 
						|
    #define MAXNROFARRAYVALUES      100
 | 
						|
    int buf[MAXNROFARRAYVALUES], ii;
 | 
						|
    int nrOfPairs=arrayname.size();
 | 
						|
 | 
						|
    if (nrOfPairs>MAXNROFARRAYVALUES)
 | 
						|
        nrOfPairs=MAXNROFARRAYVALUES;
 | 
						|
 | 
						|
    for (ii=0; ii<nrOfPairs; ii++)
 | 
						|
        buf[ii]=arrayname[ii].toInt();
 | 
						|
    */
 | 
						|
    // copy to host buffer:
 | 
						|
    bool ok=true;
 | 
						|
    int nrOfPairs=arrayname.size();
 | 
						|
 | 
						|
    if (nrOfPairs>MaxBufferSize)
 | 
						|
    {
 | 
						|
        ok=false;       // got not all
 | 
						|
        nrOfPairs=MaxBufferSize;
 | 
						|
    }
 | 
						|
    for (int ii=0; ii<nrOfPairs; ii++)
 | 
						|
        buf[ii]=arrayname[ii].toInt();
 | 
						|
 | 
						|
    return ok;
 | 
						|
}
 | 
						|
 | 
						|
/*
 | 
						|
void datei_json_readTestFile(QString filename)
 | 
						|
{
 | 
						|
    QByteArray my2Ba;
 | 
						|
    QString    my2Str;
 | 
						|
 | 
						|
    my2Str.clear();
 | 
						|
    my2Ba=datei_readFromFile(filename);
 | 
						|
 | 
						|
    QJsonDocument jdoc = QJsonDocument::fromJson(my2Ba);
 | 
						|
    QJsonObject jobj = jdoc.object();
 | 
						|
    //QJsonParseError jerror;
 | 
						|
    QJsonObject myObj;
 | 
						|
    QJsonArray myArray;
 | 
						|
 | 
						|
        // key value pair consisting of key (unique string) and value (QJsonValue)
 | 
						|
    int nrOfPairs=jobj.size();
 | 
						|
    qDebug() << "my Json file has got: " << nrOfPairs<< "pairs";
 | 
						|
 | 
						|
    if (jobj.contains("Zeit"))
 | 
						|
        qDebug() << "my Json file: " << jobj["Zeit"].toString();    // toObject(); toArray();
 | 
						|
    else
 | 
						|
        qDebug() << "my Json file contains no Zeit";
 | 
						|
 | 
						|
    if (jobj.contains("Humidity"))
 | 
						|
        qDebug() << "my Json file: " << jobj["humidity"].toInt();
 | 
						|
    else
 | 
						|
        qDebug() << "my Json file contains no Humidity";
 | 
						|
 | 
						|
    if (jobj.contains("month"))
 | 
						|
        qDebug() << "my Json file: " << jobj["month"].toObject();   // anzeige QJsonObject()
 | 
						|
    else
 | 
						|
        qDebug() << "my Json file contains no month";
 | 
						|
 | 
						|
 | 
						|
    myObj=jobj["unterstruktur"].toObject();
 | 
						|
    qDebug() << "my unterstruktur: " << myObj["month"].toInt();
 | 
						|
    qDebug() << "my unterstruktur: " << myObj["day of week"].toString();
 | 
						|
    //if (jerror.error == QJsonParseError::NoError)
 | 
						|
    //    qDebug() << "no error";
 | 
						|
 | 
						|
    qDebug() << "my Month: " << myObj["Month"].toInt();
 | 
						|
    //if (myObj["Month"] == QJsonValue::Undefined)
 | 
						|
    //    qDebug() << "no found";     geht nicht
 | 
						|
 | 
						|
    //if (jerror.error != QJsonParseError::NoError)
 | 
						|
      //  qDebug() << "no found";
 | 
						|
 | 
						|
    myArray=jobj["Puffer"].toArray();
 | 
						|
    qDebug() << "my array " <<myArray[2].toInt();
 | 
						|
    //if (jerror.error != QJsonParseError::NoError)
 | 
						|
     //   qDebug() << "no found";
 | 
						|
 | 
						|
    if ( !jobj.contains("Puffer"))
 | 
						|
        qDebug() << "no Puffer found";
 | 
						|
 | 
						|
    if ( !myArray.contains(20))
 | 
						|
        qDebug() << "no entry found";
 | 
						|
} */
 | 
						|
 | 
						|
 | 
						|
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
// ------------------------------------------------------ read, write, copy files -------------------
 | 
						|
// -------------------------------------------------------------------------------------------------
 | 
						|
 | 
						|
void datei_closeFile(QString filename)
 | 
						|
{
 | 
						|
    QFile file(filename);
 | 
						|
    file.close();
 | 
						|
}
 | 
						|
 | 
						|
QByteArray datei_readFromFile(QString filename)
 | 
						|
{
 | 
						|
    //QFile file("/own/H2B/dc2.hex");
 | 
						|
    //QFile file(FILENAME_STRUCTURE);
 | 
						|
    QFile file;
 | 
						|
 | 
						|
    file.setFileName(filename);
 | 
						|
    QByteArray myBA;
 | 
						|
 | 
						|
    myBA.clear();
 | 
						|
    if (!file.exists())
 | 
						|
    {
 | 
						|
        qDebug()<<"file not exists";
 | 
						|
        return myBA;
 | 
						|
    } else
 | 
						|
    {
 | 
						|
        if (!file.open(QIODevice::ReadOnly)  )
 | 
						|
        {
 | 
						|
            qDebug()<<"cannot open";
 | 
						|
 | 
						|
        } else
 | 
						|
        {
 | 
						|
            //qDebug()<<"loading file with " << file.size() <<"byte";
 | 
						|
            myBA = file.readAll();
 | 
						|
            //qDebug()<<"datei read: " << myBA;
 | 
						|
            file.close();
 | 
						|
        }
 | 
						|
    }
 | 
						|
    return myBA;
 | 
						|
}
 | 
						|
 | 
						|
bool datei_ifFileExists(QString filename)
 | 
						|
{
 | 
						|
 | 
						|
    QFile file;
 | 
						|
    file.setFileName(filename);
 | 
						|
 | 
						|
    if (file.exists())
 | 
						|
        return true;
 | 
						|
    return false;
 | 
						|
}
 | 
						|
 | 
						|
char datei_writeToFile(QString filename, QByteArray content)
 | 
						|
{
 | 
						|
    // retval=0 if successful  1: no write access allowed 2:cannot open to append 3:cannot create new file
 | 
						|
    QFile file(filename);
 | 
						|
    QFileInfo myFI(filename);
 | 
						|
 | 
						|
    //if (!myFI.isWritable())   //geht nur bei NTFS, weg.
 | 
						|
    //{
 | 
						|
        //file.setPermissions(filename, QFile::WriteOther); geht nicht :(
 | 
						|
    //    qDebug()<<"datei_writeToFile: writing not allowed. set attributes first!";
 | 
						|
    //    return 1;
 | 
						|
    //}
 | 
						|
 | 
						|
    if (file.exists())
 | 
						|
    {
 | 
						|
        if (!file.open(QIODevice::Append))
 | 
						|
        {
 | 
						|
            qDebug()<<"datei_writeToFile cannot open to append";
 | 
						|
            return 2;
 | 
						|
        } else
 | 
						|
        {
 | 
						|
            // add new object to the end of the file
 | 
						|
            file.write(content);
 | 
						|
            file.close();
 | 
						|
            return 0;   // OK
 | 
						|
        }
 | 
						|
    } else
 | 
						|
    {
 | 
						|
        if (!file.open(QIODevice::WriteOnly))
 | 
						|
        {
 | 
						|
            qDebug()<<"datei_writeToFile cannot open new";
 | 
						|
            return 3;
 | 
						|
        } else
 | 
						|
        {
 | 
						|
            qDebug()<<"create new file";
 | 
						|
            // write first lines into file
 | 
						|
            file.write(content);
 | 
						|
            file.close();
 | 
						|
            return 0;   // OK
 | 
						|
        }
 | 
						|
    }
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 | 
						|
bool datei_copyFile(QString currentFileName, QString newFileName)
 | 
						|
{
 | 
						|
    // retval=true if successful
 | 
						|
    QFile file;
 | 
						|
    file.setFileName(currentFileName);
 | 
						|
    return file.copy(newFileName);
 | 
						|
}
 | 
						|
 | 
						|
bool datei_clearFile(QString filename)
 | 
						|
{
 | 
						|
    // retval=true if successful
 | 
						|
    QFile file;
 | 
						|
    file.setFileName(filename);
 | 
						|
 | 
						|
    file.remove();  // 3.2.22 erst ganz löschen wegen Schreibrechten
 | 
						|
    if (!file.open(QIODevice::WriteOnly))
 | 
						|
    {
 | 
						|
        qDebug()<<"datei_clearFile  cannot open file to delete";
 | 
						|
        return false;
 | 
						|
    } else
 | 
						|
    {
 | 
						|
        file.write(0);
 | 
						|
        file.close();
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 |