Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/ChameleonPrefPane/Sources/ShellProcess.h

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//----------------------------------------------------------------
19const int MAX_HD = 32;
20
21//----------------------------------------------------------------
22class ShellProcess
23{
24public:
25// construction / destruction
26ShellProcess() { init(); }
27ShellProcess(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
31FILE * open(const char *cmd, const char *mode="r");
32
33/// Close the file
34int close();
35
36/// Get the output file desc
37FILE * 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
40char * get_line(char * line, size_t s, const char *matching=NULL);
41 std::string get_line(const char * matching=NULL);
42/// encapsulates seek to beginning of file with optional relative offset
43bool seek_start(long offset=0L )
44{
45return (_fpt && fseek(_fpt, offset, SEEK_SET)==0);
46}
47
48/// true if output file desc is valid
49bool is_valid() const { return _fpt!=NULL;}
50
51protected:
52FILE * _fpt;
53
54private:
55void init()
56{
57_fpt= NULL;
58}
59
60void dealloc()
61{
62if (_fpt)
63{
64close();
65_fpt = NULL;
66}
67}
68};
69
70#endif
71

Archive Download this file

Revision: 460