Skip to content

Commit cbda993

Browse files
committed
feat(linter) Added 'cc' to lint the C++ code on 'npm lint'
Make linter happier. Make linter happier.
1 parent 093c85d commit cbda993

File tree

9 files changed

+89
-64
lines changed

9 files changed

+89
-64
lines changed

package.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
},
6868
"devDependencies": {
6969
"bluebird": "^3.5.0",
70+
"cc": "^1.0.1",
7071
"chai": "^4.0.2",
7172
"chai-subset": "^1.5.0",
7273
"conventional-changelog-cli": "^1.3.2",
@@ -97,7 +98,7 @@
9798
"arduino-test": "TEST_PORT=$(./bin/find-arduino.js) npm test",
9899
"changelog": "conventional-changelog -i CHANGELOG.md -s",
99100
"docs": "jsdoc -c ./.jsdoc.json ",
100-
"lint": "eslint lib test bin examples",
101+
"lint": "eslint lib test bin examples && cc",
101102
"rebuild-all": "npm rebuild && node-gyp rebuild",
102103
"repl": "node bin/repl.js",
103104
"terminal": "node bin/terminal.js",
@@ -111,5 +112,16 @@
111112
"prebuild": "prebuild --all --strip --verbose",
112113
"prebuild-upload": "prebuild --all --strip --verbose"
113114
},
114-
"gypfile": true
115+
"gypfile": true,
116+
"cc": {
117+
"filter": [
118+
"legal/copyright",
119+
"build/include"
120+
],
121+
"files": [
122+
"src/*.cpp",
123+
"src/*.h"
124+
],
125+
"linelength": "120"
126+
}
115127
}

src/darwin_list.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <IOKit/serial/ioss.h>
1111
#endif
1212

13+
#include <string>
14+
#include <list>
15+
1316
uv_mutex_t list_mutex;
1417
Boolean lockInitialised = FALSE;
1518

@@ -21,7 +24,7 @@ NAN_METHOD(List) {
2124
}
2225

2326
ListBaton* baton = new ListBaton();
24-
strcpy(baton->errorString, "");
27+
snprintf(baton->errorString, sizeof(baton->errorString), "");
2528
baton->callback.Reset(info[0].As<v8::Function>());
2629

2730
uv_work_t* req = new uv_work_t();
@@ -99,19 +102,19 @@ static void ExtractUsbInformation(stSerialDevice *serialDevice, IOUSBDeviceInter
99102
UInt32 locationID;
100103
kernResult = (*deviceInterface)->GetLocationID(deviceInterface, &locationID);
101104
if (KERN_SUCCESS == kernResult) {
102-
snprintf(serialDevice->locationId, 11, "%08x", locationID);
105+
snprintf(serialDevice->locationId, sizeof(serialDevice->locationId), "%08x", locationID);
103106
}
104107

105108
UInt16 vendorID;
106109
kernResult = (*deviceInterface)->GetDeviceVendor(deviceInterface, &vendorID);
107110
if (KERN_SUCCESS == kernResult) {
108-
snprintf(serialDevice->vendorId, 7, "%04x", vendorID);
111+
snprintf(serialDevice->vendorId, sizeof(serialDevice->vendorId), "%04x", vendorID);
109112
}
110113

111114
UInt16 productID;
112115
kernResult = (*deviceInterface)->GetDeviceProduct(deviceInterface, &productID);
113116
if (KERN_SUCCESS == kernResult) {
114-
snprintf(serialDevice->productId, 7, "%04x", productID);
117+
snprintf(serialDevice->productId, sizeof(serialDevice->productId), "%04x", productID);
115118
}
116119
}
117120

@@ -152,9 +155,9 @@ static stDeviceListItem* GetSerialDevices() {
152155
CFRelease(bsdPathAsCFString);
153156

154157
if (result) {
155-
stDeviceListItem *deviceListItem = (stDeviceListItem*) malloc(sizeof(stDeviceListItem));
158+
stDeviceListItem *deviceListItem = reinterpret_cast<stDeviceListItem*>( malloc(sizeof(stDeviceListItem)));
156159
stSerialDevice *serialDevice = &(deviceListItem->value);
157-
strcpy(serialDevice->port, bsdPath);
160+
snprintf(serialDevice->port, sizeof(serialDevice->port), bsdPath);
158161
memset(serialDevice->locationId, 0, sizeof(serialDevice->locationId));
159162
memset(serialDevice->vendorId, 0, sizeof(serialDevice->vendorId));
160163
memset(serialDevice->productId, 0, sizeof(serialDevice->productId));
@@ -196,7 +199,7 @@ static stDeviceListItem* GetSerialDevices() {
196199
kCFStringEncodingUTF8);
197200

198201
if (result) {
199-
strcpy(serialDevice->manufacturer, manufacturer);
202+
snprintf(serialDevice->manufacturer, sizeof(serialDevice->manufacturer), manufacturer);
200203
}
201204

202205
CFRelease(manufacturerAsCFString);
@@ -219,7 +222,7 @@ static stDeviceListItem* GetSerialDevices() {
219222
kCFStringEncodingUTF8);
220223

221224
if (result) {
222-
strcpy(serialDevice->serialNumber, serialNumber);
225+
snprintf(serialDevice->serialNumber, sizeof(serialDevice->serialNumber), serialNumber);
223226
}
224227

225228
CFRelease(serialNumberAsCFString);
@@ -240,7 +243,7 @@ static stDeviceListItem* GetSerialDevices() {
240243

241244
// Use the plugin interface to retrieve the device interface.
242245
res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
243-
(LPVOID*) &deviceInterface);
246+
reinterpret_cast<LPVOID*> (&deviceInterface));
244247

245248
// Now done with the plugin interface.
246249
(*plugInInterface)->Release(plugInInterface);

src/darwin_list.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
#ifndef SRC_SERIALPORT_DARWIN_LIST_H_
2-
#define SRC_SERIALPORT_DARWIN_LIST_H_
1+
#ifndef SRC_DARWIN_LIST_H_
2+
#define SRC_DARWIN_LIST_H_
3+
#include <sys/param.h> // For MAXPATHLEN
34
#include <nan.h>
45
#include <list>
5-
#include <sys/param.h> // For MAXPATHLEN
6+
#include <string>
67

78
#define ERROR_STRING_SIZE 1024
89

@@ -41,4 +42,4 @@ typedef struct DeviceListItem {
4142
int* length;
4243
} stDeviceListItem;
4344

44-
#endif // SRC_SERIALPORT_DARWIN_LIST_H_
45+
#endif // SRC_DARWIN_LIST_H_

src/poller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Poller::~Poller() {
1919
// if we call uv_poll_stop after uv_poll_init failed we segfault
2020
if (uv_poll_init_success) {
2121
uv_poll_stop(poll_handle);
22-
uv_close((uv_handle_t*) poll_handle, Poller::onClose);
22+
uv_close(reinterpret_cast<uv_handle_t*> (poll_handle), Poller::onClose);
2323
} else {
2424
delete poll_handle;
2525
}

src/serialport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ NAN_METHOD(Open) {
5454
}
5555

5656
OpenBaton* baton = new OpenBaton();
57-
strcpy(baton->path, *path);
57+
snprintf(baton->path, sizeof(baton->path), *path);
5858
baton->baudRate = getIntFromObject(options, "baudRate");
5959
baton->dataBits = getIntFromObject(options, "dataBits");
6060
baton->parity = ToParityEnum(getStringFromObj(options, "parity"));

src/serialport_linux.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate) {
99
struct termios2 t;
1010

11-
if(ioctl(fd, TCGETS2, &t)) {
11+
if (ioctl(fd, TCGETS2, &t)) {
1212
return -1;
1313
}
1414

1515
t.c_cflag &= ~CBAUD;
1616
t.c_cflag |= BOTHER;
1717
t.c_ospeed = t.c_ispeed = baudrate;
1818

19-
if(ioctl(fd, TCSETS2, &t)) {
19+
if (ioctl(fd, TCSETS2, &t)) {
2020
return -2;
2121
}
2222

@@ -27,11 +27,11 @@ int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate) {
2727
int linuxGetSystemBaudRate(const int fd, int* const outbaud) {
2828
struct termios2 t;
2929

30-
if(ioctl(fd, TCGETS2, &t)) {
30+
if (ioctl(fd, TCGETS2, &t)) {
3131
return -1;
3232
}
3333

34-
*outbaud = (int)t.c_ospeed;
34+
*outbaud = static_cast<int>(t.c_ospeed);
3535

3636
return 0;
3737
}

src/serialport_linux.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#ifndef CUSTOM_BAUDRATE_H
2-
#define CUSTOM_BAUDRATE_H
1+
#ifndef SRC_SERIALPORT_LINUX_H_
2+
#define SRC_SERIALPORT_LINUX_H_
33

44
int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate);
55
int linuxGetSystemBaudRate(const int fd, int* const outbaud);
66

7-
#endif
7+
#endif // SRC_SERIALPORT_LINUX_H_
88

src/serialport_unix.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "./serialport_unix.h"
2-
#include "./serialport.h"
1+
#include "serialport_unix.h"
2+
#include "serialport.h"
33

44
#include <sys/file.h>
55
#include <unistd.h>
@@ -15,13 +15,11 @@
1515
#if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
1616
#include <sys/ioctl.h>
1717
#include <IOKit/serial/ioss.h>
18-
#endif
1918

20-
#if defined(__OpenBSD__)
19+
#elif defined(__OpenBSD__)
2120
#include <sys/ioctl.h>
22-
#endif
2321

24-
#if defined(__linux__)
22+
#elif defined(__linux__)
2523
#include <sys/ioctl.h>
2624
#include <linux/serial.h>
2725
#include "serialport_linux.h"
@@ -105,7 +103,8 @@ int setBaudRate(ConnectionOptionsBaton *data) {
105103
// get port options
106104
struct termios options;
107105
if (-1 == tcgetattr(fd, &options)) {
108-
snprintf(data->errorString, sizeof(data->errorString), "Error: %s setting custom baud rate of %d", strerror(errno), data->baudRate);
106+
snprintf(data->errorString, sizeof(data->errorString),
107+
"Error: %s setting custom baud rate of %d", strerror(errno), data->baudRate);
109108
return -1;
110109
}
111110

@@ -115,10 +114,12 @@ int setBaudRate(ConnectionOptionsBaton *data) {
115114
int err = linuxSetCustomBaudRate(fd, data->baudRate);
116115

117116
if (err == -1) {
118-
snprintf(data->errorString, sizeof(data->errorString), "Error: %s || while retrieving termios2 info", strerror(errno));
117+
snprintf(data->errorString, sizeof(data->errorString),
118+
"Error: %s || while retrieving termios2 info", strerror(errno));
119119
return -1;
120-
} else if(err == -2) {
121-
snprintf(data->errorString, sizeof(data->errorString), "Error: %s || while setting custom baud rate of %d", strerror(errno), data->baudRate);
120+
} else if (err == -2) {
121+
snprintf(data->errorString, sizeof(data->errorString),
122+
"Error: %s || while setting custom baud rate of %d", strerror(errno), data->baudRate);
122123
return -1;
123124
}
124125

@@ -131,7 +132,8 @@ int setBaudRate(ConnectionOptionsBaton *data) {
131132
if (-1 == baudRate) {
132133
speed_t speed = data->baudRate;
133134
if (-1 == ioctl(fd, IOSSIOSPEED, &speed)) {
134-
snprintf(data->errorString, sizeof(data->errorString), "Error: %s calling ioctl(.., IOSSIOSPEED, %ld )", strerror(errno), speed );
135+
snprintf(data->errorString, sizeof(data->errorString),
136+
"Error: %s calling ioctl(.., IOSSIOSPEED, %ld )", strerror(errno), speed);
135137
return -1;
136138
} else {
137139
tcflush(fd, TCIOFLUSH);
@@ -141,7 +143,8 @@ int setBaudRate(ConnectionOptionsBaton *data) {
141143
#endif
142144

143145
if (-1 == baudRate) {
144-
snprintf(data->errorString, sizeof(data->errorString), "Error baud rate of %d is not supported on your platform", data->baudRate);
146+
snprintf(data->errorString, sizeof(data->errorString),
147+
"Error baud rate of %d is not supported on your platform", data->baudRate);
145148
return -1;
146149
}
147150

@@ -163,7 +166,8 @@ void EIO_Update(uv_work_t* req) {
163166
int setup(int fd, OpenBaton *data) {
164167
int dataBits = ToDataBitsConstant(data->dataBits);
165168
if (-1 == dataBits) {
166-
snprintf(data->errorString, sizeof(data->errorString), "Invalid data bits setting %d", data->dataBits);
169+
snprintf(data->errorString, sizeof(data->errorString),
170+
"Invalid data bits setting %d", data->dataBits);
167171
return -1;
168172
}
169173

@@ -299,7 +303,8 @@ void EIO_Close(uv_work_t* req) {
299303
VoidBaton* data = static_cast<VoidBaton*>(req->data);
300304

301305
if (-1 == close(data->fd)) {
302-
snprintf(data->errorString, sizeof(data->errorString), "Error: %s, unable to close fd %d", strerror(errno), data->fd);
306+
snprintf(data->errorString, sizeof(data->errorString),
307+
"Error: %s, unable to close fd %d", strerror(errno), data->fd);
303308
}
304309
}
305310

@@ -370,7 +375,7 @@ void EIO_GetBaudRate(uv_work_t* req) {
370375
}
371376
#endif
372377

373-
// TODO implement on mac
378+
// TODO(Fumon) implement on mac
374379
#if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
375380
snprintf(data->errorString, sizeof(data->errorString), "Error: System baud rate check not implemented on darwin");
376381
return;

0 commit comments

Comments
 (0)