Advertisement
AntonioVillanueva

récupérer le nom du module et son pid

Jul 11th, 2022
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. /*
  2.  * Walk in lines and search within the lines for the name of the module
  3.  */
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. //pull apart module name ./mod16_*.x86
  9. string modulo (string line){
  10.    
  11.     size_t first = line.find ("./");//first part of the string
  12.     size_t last = line.find (" ",first);//Last part of the string
  13.    
  14.     if (first != string::npos && last != string::npos){
  15.         return line.substr(first,last-first);
  16.     }
  17.     return "";//Not found
  18. }
  19.  
  20. //Search pid in current line
  21. string pid(string line){
  22.     size_t first = 0;//first part of the string
  23.    
  24.     //Check if there are spaces at the beginning
  25.     while (line [first++]!=' '){}
  26.  
  27.     size_t last = line.find (" ",first);//Last part of the string  
  28.    
  29.     if (last != string::npos){
  30.         return line.substr(first,last-1);
  31.     }
  32.    
  33.     return "";
  34. }
  35.  
  36. int main(int argc, char* argv[])
  37. {
  38.     string line="";
  39.     //Example from  ps -aux| grep ttyUSB
  40. /*
  41.     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\
  42.     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\
  43.     axiome    5356  0.0  0.0   4504   740 pts/0    S+   11:04   0:00 sh -c ps -aux | grep ttyUSB 2>&1 \n\
  44.     axiome    5358  0.0  0.0  14224  1012 pts/0    S+   11:04   0:00 grep ttyUSB \n";
  45. */
  46.  
  47.     //Example from  ps -ax| grep ttyUSB
  48.     string s="\
  49.      1194 ?        S      0:44 ./mod16_video.x64 /dev/ttyUSB1 ul01 videocfgshr8162 9600 N81\n\
  50. 1195 ?        S      0:27 ./mod16_modbus.x64 /dev/ttyUSB2 9600 N81 ul01 ./cfgmodbushfi.txt 2001 1 1\n\
  51. 18022 pts/0    S+     0:00 grep --color=auto ttyUSB\n\
  52.     ";
  53.  
  54.  
  55.     for (auto c:s){ //Loop over string
  56.         line+=c;
  57.         if (c== '\n' ){//text line ending in \n
  58.            
  59.             if (line.find("ttyUSB")!= string::npos && line.find("mod16")!= string::npos){
  60.                 cout << modulo(line) << " pid = " << pid(line) << endl ;
  61.  
  62.             }
  63.            
  64.             line="";
  65.         }      
  66.     }
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement