Encoder-nyuenc / src / main / resultqueue.c
resultqueue.c
Raw
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>

#include "utils.h"

void extractFinalResult() {

    unsigned char *charArray;
    unsigned char lastChar;
    uint8_t *countArray;
    uint8_t lastCount;
    int resultLength;

    for (int i = firstResult; i <= lastResult; i ++) {
        charArray = resultQueue[i] -> charArray;
        countArray = resultQueue[i] -> countArray;
        resultLength = resultQueue[i] -> resultLength;
        if (resultLength > 1) {
            if (i == firstResult) {
                fprintf(stdout, "%c%c", charArray[0], countArray[0]);
            }
            else if (charArray[0] == lastChar) {
                fprintf(stdout, "%c%c", lastChar, lastCount + countArray[0]);
            }
            else {
                fprintf(stdout, "%c%c%c%c", lastChar, lastCount, charArray[0], countArray[0]);
            }
            for (int j = 1; j < resultLength - 1; j ++) {
                fprintf(stdout, "%c%c", charArray[j], countArray[j]);
            }
        }
        lastChar = charArray[resultLength - 1];
        lastCount = countArray[resultLength - 1];
        free(charArray);
        free(countArray);
        free(resultQueue[i]);
    }
    fprintf(stdout, "%c%c", lastChar, lastCount);
    fflush(stdout);

}