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}
69int imageIndexFromFs() const;
70protected:
71void removeSpaces(std::string & s);
72
73private:
74int _disk, _part;
75std::string _fsType, _label;
76};
77
78static inline bool isDiskIndexInf(PartitionInfo i, PartitionInfo j)
79{
80return ((i.disk()*100+i.partition()) < (j.disk()*100 + j.partition()) ) ;
81}
82
83//----------------------------------------------------------------
84class PartitionExtractor : public ShellProcess
85{
86public:
87PartitionExtractor() :ShellProcess() { init(); }
88
89const std::vector<PartitionInfo>& extractPartitions(
90const char* szHide=NULL,
91const char* szRenamed=NULL);
92int getListCount() const {return (int) _partList.size();}
93
94const std::vector<PartitionInfo>& partList() const {return _partList;}
95std::vector<PartitionInfo>& editPartList() {return _partList;}
96
97// get the index in the internal partlist of the hd(n,m) partition specified by the input string
98// return -1 if no match, >=0 if a match happens
99int getIndexFromHdStringSpec(const char*);
100
101void hidePartitions(const char* szParts){ _hiddenParts = (szParts ? szParts : "");}
102void renamedPartitions(const char* szParts){ _renamedParts = (szParts ? szParts : "");}
103void sortPartList() {sort(_partList.begin(), _partList.end(), isDiskIndexInf);}
104
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: 21