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 {return _partList;}
96std::vector<PartitionInfo>& editPartList() {return _partList;}
97
98// get the index in the internal partlist of the hd(n,m) partition specified by the input string
99// return -1 if no match, >=0 if a match happens
100int getIndexFromHdStringSpec(const char*);
101
102void hidePartitions(const char* szParts){ _hiddenParts = (szParts ? szParts : "");}
103void renamedPartitions(const char* szParts){ _renamedParts = (szParts ? szParts : "");}
104const char * checkForRename(const char * label, const char* szHd);
105
106void swapHD(int src, int dst)
107{
108if(src < 0 || src > MAX_HD-1 || dst < 0 || dst > MAX_HD-1) return;
109_hdRedirTable[src]=dst;
110_hdRedirTable[dst]=src;
111}
112
113void resetSwapping() { init();}
114
115protected:
116void init() {
117for (int i=0; i<MAX_HD; i++) _hdRedirTable[i]=i;
118}
119
120private:
121std::vector<PartitionInfo> _partList;
122int _hdRedirTable[MAX_HD];
123std::string _hiddenParts;
124std::string _renamedParts;
125
126};
127

Archive Download this file

Revision: 20