Skip to content

Commit

Permalink
Merge branch 'array-to-table'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyew committed Oct 24, 2014
1 parent d98804d commit cbec009
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
10 changes: 8 additions & 2 deletions src/PdContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ void PdContext::registerTable(MessageTable *table) {

for (list<TableReceiverInterface *>::iterator it = tableReceiverList.begin();
it != tableReceiverList.end(); it++) {
// in case the table receiver doesn't have the table name yet
if ((*it)->getName() == NULL)
continue;
if (!strcmp((*it)->getName(), table->getName())) (*it)->setTable(table);
}
}
Expand All @@ -417,8 +420,11 @@ MessageTable *PdContext::getTable(const char *name) {
void PdContext::registerTableReceiver(TableReceiverInterface *tableReceiver) {
tableReceiverList.push_back(tableReceiver); // add the new receiver

MessageTable *table = getTable(tableReceiver->getName());
tableReceiver->setTable(table); // set table whether it is NULL or not
// in case the tableread doesnt have the name of the table yet
if (tableReceiver->getName()) {
MessageTable *table = getTable(tableReceiver->getName());
tableReceiver->setTable(table); // set table whether it is NULL or not
}
}

void PdContext::unregisterTableReceiver(TableReceiverInterface *tableReceiver) {
Expand Down
55 changes: 34 additions & 21 deletions src/PdFileParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ string PdFileParser::nextLine() {
// remove trailing '\n' from the line
line = string(stringDesc, pos, newPos-pos);
pos = newPos + 1; // one past the '\n'

size_t commaIndex;
if ((commaIndex = line.find_last_of(",")) != std::string::npos && commaIndex > 0 && line[commaIndex - 1] != '\\') {
line = line.substr(0, commaIndex) + ";";
}
return line;
}
}
Expand All @@ -117,12 +122,14 @@ PdGraph *PdFileParser::execute(PdMessage *initMsg, PdGraph *graph, PdContext *co
PdMessage *initMessage = PD_MESSAGE_ON_STACK(INIT_MESSAGE_MAX_ELEMENTS);

string message;
MessageTable *lastArrayCreated = NULL; // used to know on which table the #A line values have to be set
while (!(message = nextMessage()).empty()) {
// create a non-const copy of message such that strtok can modify it
char line[message.size()+1];
strncpy(line, message.c_str(), sizeof(line));

char *hashType = strtok(line, " ");

if (!strcmp(hashType, "#N")) {
char *objectType = strtok(NULL, " ");
if (!strcmp(objectType, "canvas")) {
Expand Down Expand Up @@ -156,7 +163,7 @@ PdGraph *PdFileParser::execute(PdMessage *initMsg, PdGraph *graph, PdContext *co

// the new graph is pushed onto the stack
graph = newGraph;
} else {
} else {
context->printErr("Unrecognised #N object type: \"%s\".", line);
}
} else if (!strcmp(hashType, "#X")) {
Expand Down Expand Up @@ -272,36 +279,42 @@ PdGraph *PdFileParser::execute(PdMessage *initMsg, PdGraph *graph, PdContext *co
context->printErr("declare \"%s\" flag is not supported.", initMessage->getSymbol(0));
}
} else if (!strcmp(objectType, "array")) {
/*
// creates a new table
// objectInitString should contain both name and buffer length
char *objectInitString = strtok(NULL, ";"); // get the object initialisation string
char resBuffer[RESOLUTION_BUFFER_LENGTH];
initMessage->initWithSARb(4, objectInitString, graph->getArguments(), resBuffer, RESOLUTION_BUFFER_LENGTH);
MessageTable *table = (MessageTable *) context->newObject("table", initMessage, graph);
graph->addObject(0, 0, table);
int bufferLength = 0;
float *buffer = table->getBuffer(&bufferLength);
// next many lines should be elements of that array
// while the next line begins with #A
while (!strcmp(strtok(line = nextMessage(), " ;"), "#A")) {
int index = atoi(strtok(NULL, " ;"));
char *nextNumber = NULL;
// ensure that file does not attempt to write more than stated numbers
while (((nextNumber = strtok(NULL, " ;")) != NULL) && (index < bufferLength)) {
buffer[index++] = atof(nextNumber);
}
}
// ignore the #X coords line
*/
lastArrayCreated = reinterpret_cast<MessageTable *>(
context->newObject("table", initMessage, graph));
graph->addObject(0, 0, lastArrayCreated);
context->printStd("PdFileParser: Replacer array with table, name: '%s'", initMessage->getSymbol(0));
} else if (!strcmp(objectType, "coords")) {
// NOTE(mhroth): not really sure what this object type does, but it doesn't seem to have
// any effect on the function of the patch (i.e. it seems to be purely cosmetic).
context->printErr("WARNING: Unsure what object type #X coords does: \"%s\"\n"
" There is (probably) no reason to worry.", message.c_str());
// context->printErr("WARNING: Unsure what object type #X coords does: \"%s\"\n"
// " There is (probably) no reason to worry.", message.c_str());*/
} else {
context->printErr("Unrecognised #X object type: \"%s\"", message.c_str());
}
} else if (!strcmp(hashType, "#A")) {
if (!lastArrayCreated)
context->printErr("#A line but no array were created");
else {
int bufferLength = 0;
float *buffer = lastArrayCreated->getBuffer(&bufferLength);
char *token;

int index = atoi(strtok(NULL, " ;"));
while ((token = strtok(NULL, " ;")) != NULL) {
if (index >= bufferLength) {
context->printErr("#A trying to add value at index %d \
while buffer length is %d", index, bufferLength);
break;
}
buffer[index] = atof(token);
++index;
}
}
} else {
context->printErr("Unrecognised hash type: \"%s\"", message.c_str());
}
Expand Down

0 comments on commit cbec009

Please sign in to comment.