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 char * clabel() const { return _label.c_str();}
49const std::string& label() const { return _label;}
50void label(const char * l) {
51if (l) _label = l;
52removeSpaces(_label);
53}
54
55const char * cfsType() const { return _fsType.c_str();}
56const std::string& fsType() const { return _fsType;}
57void fsType(const char * fs) {
58if (fs) _fsType = fs;
59removeSpaces(_fsType);
60}
61
62void set(int disk, int part) { _disk =disk; _part = part;}
63bool fromPartitionHdString(const char * inHdStr);
64std::string toHdStr() const
65{
66std::string buf = "hd(n,m)";
67buf[3]= '0'+disk();
68buf[5]='0'+partition();
69return buf;
70}
71int imageIndexFromFs() const;
72protected:
73void removeSpaces(std::string & s);
74
75private:
76int _disk, _part;
77std::string _fsType, _label;
78};
79
80static inline bool isDiskIndexInf(PartitionInfo i, PartitionInfo j)
81{
82return ((i.disk()*100+i.partition()) < (j.disk()*100 + j.partition()) ) ;
83}
84
85//----------------------------------------------------------------
86class PartitionExtractor : public ShellProcess
87{
88public:
89PartitionExtractor() :ShellProcess() { init(); }
90
91const std::vector<PartitionInfo>& extractPartitions(
92const char* szHide=NULL,
93const char* szRenamed=NULL);
94int getListCount() const {return (int) _partList.size();}
95
96const std::vector<PartitionInfo>& partList() const {return _partList;}
97std::vector<PartitionInfo>& editPartList() {return _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 : "");}
105void sortPartList() {sort(_partList.begin(), _partList.end(), isDiskIndexInf);}
106
107const char * checkForRename(const char * label, const char* szHd);
108
109void swapHD(int src, int dst)
110{
111if(src < 0 || src > MAX_HD-1 || dst < 0 || dst > MAX_HD-1) return;
112_hdRedirTable[src]=dst;
113_hdRedirTable[dst]=src;
114}
115
116void resetSwapping() { init();}
117
118protected:
119void init() {
120for (int i=0; i<MAX_HD; i++) _hdRedirTable[i]=i;
121}
122
123private:
124std::vector<PartitionInfo> _partList;
125int _hdRedirTable[MAX_HD];
126std::string _hiddenParts;
127std::string _renamedParts;
128
129};
130

Archive Download this file

Revision: 27