@@ -118,38 +118,40 @@ void FirmataClass::printFirmwareVersion(void)
118118
119119void 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