1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
class color {
public:
int red;
int green;
int blue;
int transitionTime;
void printObj() {
std::cout << "red: " << red << " - green: " << green << " - blue: " << blue << " - time: " << transitionTime;
}
};
color *patternListObjects;
int patternLength = 0;
void parseTickValues(char *tick, int patternIndex, color *list) {
char *value;
color TickObj;
//red green blue time seperated by ","
value = strtok (tick,",");
for (int i = 0; i < 4; i ++) {
// std::cout << atoi(value) << "\n";
if (i == 0) {
TickObj.red = atoi(value);
} else if (i == 1) {
TickObj.green = atoi(value);
} else if (i == 2) {
TickObj.blue = atoi(value);
} else if (i == 3) {
TickObj.transitionTime = atoi(value);
}
value = strtok (NULL, ",");
}
//add color obj to the list
list[patternIndex] = TickObj;
}
void splitPatternTicks(char *pattern, int ticks) {
char *colorsAtTick;
//im told this is bad? *colorsAtTickArray[ticks];
char *colorsAtTickArray[ticks];
//split pattern on ";" into chars
colorsAtTick = strtok (pattern,";");
while (colorsAtTick != NULL)
{
//save the chars into an array
colorsAtTickArray[patternLength] = colorsAtTick;
patternLength ++;
colorsAtTick = strtok (NULL,";");
}
color list[patternLength];// = {};
for (int i = 0; i < patternLength; i ++) {
//loop over the array of char to further split and save values
//colorsAtTickArray ex. "255,0,0,1000"
parseTickValues(colorsAtTickArray[i], i, list);
}
patternListObjects = list;
//Works here:
std::cout << "\nprinting right after assignment: \n" <<
&patternListObjects[0].red << "\n" <<
&patternListObjects[0].green << "\n" <<
&patternListObjects[0].blue << "\n\n" <<
patternListObjects[0].red << " " <<
patternListObjects[0].green << " " <<
patternListObjects[0].blue << "\n\n";
}
int main() {
//the string I am receiving, it could vary in length
//it represents rgb values with a time in ms to transition to the next color
char string[42] = "0,0,0,1000;25,25,25,1000;150,150,150,1000";
char *stringPointer = string;
splitPatternTicks(stringPointer, 3);
//fails here:
std::cout << "printing through the refernce: \n" <<
&patternListObjects[0].red << "\n" <<
&patternListObjects[0].green << "\n" <<
&patternListObjects[0].blue << "\n\n" <<
patternListObjects[0].red << " " <<
patternListObjects[0].green << " " <<
patternListObjects[0].blue << "\n\n";
}
|