Skip to content
Browse files

Allow bang to be described as "!" in a message string.

  • Loading branch information...
1 parent d8c3206 commit b303d475b236aead8b80fc1606a077dcd2bf7d1e @mhroth committed Feb 16, 2012
Showing with 16 additions and 32 deletions.
  1. +1 −1 src/MessageMessageBox.cpp
  2. +14 −30 src/PdMessage.cpp
  3. +1 −1 src/PdMessage.h
View
2 src/MessageMessageBox.cpp
@@ -116,7 +116,7 @@ void MessageMessageBox::processMessage(int inletIndex, PdMessage *message) {
char *buffer = (char *) alloca(RES_BUFFER_LENGTH * sizeof(char));
// TODO(mhroth): resolve string, but may be in stack buffer
PdMessage::resolveString(messageTemplate->getSymbol(i), message, 1, buffer, RES_BUFFER_LENGTH);
- outgoingMessage->setFloatOrSymbol(i, buffer); // buffer is resolved to float or string
+ outgoingMessage->parseAndSetMessageElement(i, buffer); // buffer is resolved to float or string
}
}
sendMessage(0, outgoingMessage);
View
44 src/PdMessage.cpp
@@ -36,20 +36,22 @@ void PdMessage::initWithString(double ts, unsigned int maxElements, char *initSt
if (token == NULL || strlen(initString) == 0) {
initWithTimestampAndBang(0.0); // just in case, there is always at least one element in a message
} else {
- int i = 0;
+ unsigned int i = 0;
do {
- setFloatOrSymbol(i++, token);
+ parseAndSetMessageElement(i++, token);
} while (((token = strtok(NULL, " ;")) != NULL) && (i < maxElements));
numElements = i;
}
}
-void PdMessage::setFloatOrSymbol(unsigned int index, char *initString) {
- if (StaticUtils::isNumeric(initString)) {
- setFloat(index, atof(initString));
+void PdMessage::parseAndSetMessageElement(unsigned int index, char *token) {
+ if (StaticUtils::isNumeric(token)) {
+ setFloat(index, atof(token)); // element is a float
+ } else if (!strcmp("!", token)) {
+ setBang(index); // element is a bang
} else {
- setSymbol(index, initString); // element is symbolic
+ setSymbol(index, token); // element is symbolic
}
}
@@ -332,18 +334,9 @@ char *PdMessage::toString() {
for (int i = 0; i < numElements; i++) {
lengths[i] = 0;
switch (getType(i)) {
- case FLOAT: {
- lengths[i] = snprintf(NULL, 0, "%g", getFloat(i));
- break;
- }
- case BANG: {
- lengths[i] = snprintf(NULL, 0, "%s", "bang");
- break;
- }
- case SYMBOL: {
- lengths[i] = snprintf(NULL, 0, "%s", getSymbol(i));
- break;
- }
+ case FLOAT: lengths[i] = snprintf(NULL, 0, "%g", getFloat(i)); break;
+ case BANG: lengths[i] = snprintf(NULL, 0, "%s", "bang"); break;
+ case SYMBOL:lengths[i] = snprintf(NULL, 0, "%s", getSymbol(i)); break;
default: break;
}
// total length of our string is each atom plus a space, or \0 on the end
@@ -360,18 +353,9 @@ char *PdMessage::toString() {
}
// put a string representation of each atom into the final string
switch (getType(i)) {
- case FLOAT: {
- snprintf(&finalString[pos], lengths[i] + 1, "%g", getFloat(i));
- break;
- }
- case BANG: {
- snprintf(&finalString[pos], lengths[i] + 1, "%s", "bang");
- break;
- }
- case SYMBOL: {
- snprintf(&finalString[pos], lengths[i] + 1, "%s", getSymbol(i));
- break;
- }
+ case FLOAT: snprintf(&finalString[pos], lengths[i] + 1, "%g", getFloat(i)); break;
+ case BANG: snprintf(&finalString[pos], lengths[i] + 1, "%s", "bang"); break;
+ case SYMBOL: snprintf(&finalString[pos], lengths[i] + 1, "%s", getSymbol(i)); break;
default: break;
}
pos += lengths[i];
View
2 src/PdMessage.h
@@ -86,7 +86,7 @@ class PdMessage {
void initWithString(double timestamp, unsigned int maxElements, char *initString);
/** Sets the given message element to a FLOAT or SYMBOL depending on contents of string. */
- void setFloatOrSymbol(unsigned int index, char *initString);
+ void parseAndSetMessageElement(unsigned int index, char *initString);
MessageAtom *getElement(unsigned int index);

0 comments on commit b303d47

Please sign in to comment.
Something went wrong with that request. Please try again.