1 | /*␊ |
2 | * shell_process.h␊ |
3 | *␊ |
4 | * Created by Rekursor on 1/17/2010.␊ |
5 | *␊ |
6 | */␊ |
7 | #ifndef __CHSHELL_PROCESS_H␊ |
8 | #define __CHSHELL_PROCESS_H␊ |
9 | ␊ |
10 | #include <Security/Authorization.h>␊ |
11 | #include <stdio.h>␊ |
12 | #include <stdlib.h>␊ |
13 | #include <errno.h>␊ |
14 | #include <sys/stat.h>␊ |
15 | #include <vector>␊ |
16 | #include <string>␊ |
17 | #include "file_util.h"␊ |
18 | //----------------------------------------------------------------␊ |
19 | const int MAX_HD = 32;␊ |
20 | ␊ |
21 | //----------------------------------------------------------------␊ |
22 | class ShellProcess ␊ |
23 | {␊ |
24 | public:␊ |
25 | ␉// construction / destruction␊ |
26 | ␉ShellProcess() { init(); }␊ |
27 | ␉ShellProcess(const char * cmd, const char * mode = "r") { init(); open(cmd, mode); }␊ |
28 | ␉~ShellProcess() { dealloc(); }␊ |
29 | ␉␊ |
30 | ␉/// Run the cmd and stores output for further parsing␊ |
31 | ␉FILE * open␉(const char *cmd, const char *mode="r");␊ |
32 | ␉␊ |
33 | ␉/// Close the file␊ |
34 | ␉int close();␊ |
35 | ␉␊ |
36 | ␉/// Get the output file desc ␊ |
37 | ␉FILE * desc() const { return _fpt;} // non null if file is open␊ |
38 | ␉␊ |
39 | ␉/// Get the next line in the output, optionally skip lines until matching line is found␊ |
40 | ␉char * get_line(char * line, size_t s, const char *matching=NULL);␊ |
41 | ␉␊ |
42 | ␉/// encapsulates seek to beginning of file with optional relative offset␊ |
43 | ␉bool seek_start(long offset=0L )␊ |
44 | ␉{␊ |
45 | ␉␉return (_fpt && fseek(_fpt, offset, SEEK_SET)==0);␊ |
46 | ␉}␊ |
47 | ␉␊ |
48 | ␉/// true if output file desc is valid␊ |
49 | ␉bool is_valid() const { return _fpt!=NULL;}␊ |
50 | ␉␊ |
51 | protected:␊ |
52 | ␉FILE * _fpt;␊ |
53 | ␉␊ |
54 | private:␊ |
55 | ␉void init()␊ |
56 | ␉{␊ |
57 | ␉␉_fpt= NULL;␊ |
58 | ␉}␊ |
59 | ␉␊ |
60 | ␉void dealloc()␊ |
61 | ␉{␊ |
62 | ␉␉if (_fpt) ␊ |
63 | ␉␉{␊ |
64 | ␉␉␉close();␊ |
65 | ␉␉␉_fpt = NULL;␊ |
66 | ␉␉}␊ |
67 | ␉}␊ |
68 | };␊ |
69 | ␊ |
70 | #endif␊ |
71 | |