Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Walk in lines and search within the lines for the name of the module
- */
- #include <iostream>
- #include <string>
- using namespace std;
- //pull apart module name ./mod16_*.x86
- string modulo (string line){
- size_t first = line.find ("./");//first part of the string
- size_t last = line.find (" ",first);//Last part of the string
- if (first != string::npos && last != string::npos){
- return line.substr(first,last-first);
- }
- return "";//Not found
- }
- //Search pid in current line
- string pid(string line){
- size_t first = 0;//first part of the string
- //Check if there are spaces at the beginning
- while (line [first++]!=' '){}
- size_t last = line.find (" ",first);//Last part of the string
- if (last != string::npos){
- return line.substr(first,last-1);
- }
- return "";
- }
- int main(int argc, char* argv[])
- {
- string line="";
- //Example from ps -aux| grep ttyUSB
- /*
- string s="root 1194 0.2 0.0 6724 740 ? S 10:06 0:09 ./mod16_video.x64 /dev/ttyUSB1 ul01 videocfgshr8162 9600 N81 \n\
- root 1195 0.1 0.0 6704 724 ? S 10:06 0:06 ./mod16_modbus.x64 /dev/ttyUSB2 9600 N81 ul01 ./cfgmodbushfi.txt 2001 1 1 \n\
- axiome 5356 0.0 0.0 4504 740 pts/0 S+ 11:04 0:00 sh -c ps -aux | grep ttyUSB 2>&1 \n\
- axiome 5358 0.0 0.0 14224 1012 pts/0 S+ 11:04 0:00 grep ttyUSB \n";
- */
- //Example from ps -ax| grep ttyUSB
- string s="\
- 1194 ? S 0:44 ./mod16_video.x64 /dev/ttyUSB1 ul01 videocfgshr8162 9600 N81\n\
- 1195 ? S 0:27 ./mod16_modbus.x64 /dev/ttyUSB2 9600 N81 ul01 ./cfgmodbushfi.txt 2001 1 1\n\
- 18022 pts/0 S+ 0:00 grep --color=auto ttyUSB\n\
- ";
- for (auto c:s){ //Loop over string
- line+=c;
- if (c== '\n' ){//text line ending in \n
- if (line.find("ttyUSB")!= string::npos && line.find("mod16")!= string::npos){
- cout << modulo(line) << " pid = " << pid(line) << endl ;
- }
- line="";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement