Skip to content

Commit 678f428

Browse files
updating to allow user specified filename
1 parent 90b6a9b commit 678f428

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

Firmata.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,38 +118,40 @@ void FirmataClass::printFirmwareVersion(void)
118118

119119
void FirmataClass::setFirmwareNameAndVersion(const char *name, byte major, byte minor)
120120
{
121-
const char *filename;
122-
char *extension;
121+
const char *firmwareName;
122+
const char *extension;
123123

124124
// parse out ".cpp" and "applet/" that comes from using __FILE__
125125
extension = strstr(name, ".cpp");
126-
127-
if (strrchr(name, '/') != NULL) {
128-
// points to slash, +1 gets to start of filename
129-
filename = strrchr(name, '/') + 1;
130-
} else {
131-
// points to slash, +1 gets to start of filename
132-
filename = strrchr(name, '\\') + 1;
126+
firmwareName = strrchr(name, '/');
127+
128+
if (!firmwareName) {
129+
// windows
130+
firmwareName = strrchr(name, '\\');
133131
}
134-
135-
// add two bytes for version numbers
136-
if(extension && filename) {
137-
firmwareVersionCount = extension - filename + 2;
138-
} else {
139-
firmwareVersionCount = strlen(name) + 2;
140-
filename = name;
132+
if (!firmwareName) {
133+
// user passed firmware name
134+
firmwareName = name;
135+
}
136+
else {
137+
firmwareName ++;
141138
}
142139

140+
if (!extension) {
141+
firmwareVersionCount = strlen(firmwareName) + 2;
142+
}
143+
else {
144+
firmwareVersionCount = extension - firmwareName + 2;
145+
}
146+
147+
// in case anyone every calls setFirmwareNameAndVersion more than once
143148
free(firmwareVersionVector);
144149

145150
firmwareVersionVector = (byte *) malloc(firmwareVersionCount);
146151
firmwareVersionVector[firmwareVersionCount] = 0;
147152
firmwareVersionVector[0] = major;
148153
firmwareVersionVector[1] = minor;
149-
strncpy((char*)firmwareVersionVector + 2, filename, firmwareVersionCount - 2);
150-
// alas, no snprintf on Arduino
151-
// snprintf(firmwareVersionVector, MAX_DATA_BYTES, "%c%c%s",
152-
// (char)major, (char)minor, firmwareVersionVector);
154+
strncpy((char*)firmwareVersionVector + 2, firmwareName, firmwareVersionCount - 2);
153155
}
154156

155157
// this method is only used for unit testing

0 commit comments

Comments
 (0)