Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/ChameleonPrefPane/Sources/process.h

1/*
2 * shell_process.h
3 *
4 * Created by Rekursor on 1/17/2010.
5 *
6 */
7#include <Security/Authorization.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <errno.h>
11#include <sys/stat.h>
12#include <vector>
13#include <string>
14
15//----------------------------------------------------------------
16const int MAX_HD = 10;
17
18//----------------------------------------------------------------
19class ShellProcess
20{
21public:
22// construction / destruction
23ShellProcess() {_fpt= NULL;}
24~ShellProcess() {if (_fpt) close();}
25
26FILE * open(const char *cmd, const char *mode="r");
27int close();
28FILE * desc() const { return _fpt;} // non null if file is open
29char * get_line(char * line, size_t s) const {return _fpt ? fgets(line, s, _fpt) : NULL;}
30
31protected:
32FILE * _fpt;
33};
34
35//----------------------------------------------------------------
36class PartitionInfo
37{
38public:
39PartitionInfo() : _disk(0), _part(0) {}
40PartitionInfo(int disk, int part) {set(disk, part);}
41
42void disk(int disk) { _disk=disk;}
43int disk() const { return _disk;}
44
45void partition(int part) { _part=part;}
46int partition () const { return _part;}
47
48const std::string& label() const { return _label;}
49void label(const char * l) {
50if (l) _label = l;
51removeSpaces(_label);
52}
53
54const std::string& fsType() const { return _fsType;}
55void fsType(const char * fs) {
56if (fs) _fsType = fs;
57removeSpaces(_fsType);
58}
59
60void set(int disk, int part) { _disk =disk; _part = part;}
61bool fromPartitionHdString(const char * inHdStr);
62std::string toHdStr() const
63{
64std::string buf = "hd(n,m)";
65buf[3]= '0'+disk();
66buf[5]='0'+partition();
67return buf;
68}
69
70int imageIndexFromFs() const;
71protected:
72void removeSpaces(std::string & s);
73
74private:
75int _disk, _part;
76std::string _fsType, _label;
77};
78
79static inline bool isDiskIndexInf(PartitionInfo i, PartitionInfo j)
80{
81return ((i.disk()*100+i.partition()) < (j.disk()*100 + j.partition()) ) ;
82}
83
84//----------------------------------------------------------------
85class PartitionExtractor : public ShellProcess
86{
87public:
88PartitionExtractor() :ShellProcess() { init(); }
89
90const std::vector<PartitionInfo>& extractPartitions(
91const char* szHide=NULL,
92const char* szRenamed=NULL);
93int getListCount() const {return (int) _partList.size();}
94
95const std::vector<PartitionInfo>& partList() const
96{
97return _partList;
98}
99// get the index in the internal partlist of the hd(n,m) partition specified by the input string
100// return -1 if no match, >=0 if a match happens
101int getIndexFromHdStringSpec(const char*);
102
103void hidePartitions(const char* szParts){ _hiddenParts = (szParts ? szParts : "");}
104void renamedPartitions(const char* szParts){ _renamedParts = (szParts ? szParts : "");}
105const char * checkForRename(const char * label, const char* szHd);
106
107void swapHD(int src, int dst)
108{
109if(src < 0 || src > MAX_HD-1 || dst < 0 || dst > MAX_HD-1) return;
110_hdRedirTable[src]=dst;
111_hdRedirTable[dst]=src;
112}
113
114void resetSwapping() { init();}
115
116protected:
117void init() {
118for (int i=0; i<MAX_HD; i++) _hdRedirTable[i]=i;
119}
120
121private:
122std::vector<PartitionInfo> _partList;
123int _hdRedirTable[MAX_HD];
124std::string _hiddenParts;
125std::string _renamedParts;
126
127};
128

Archive Download this file

Revision: 14