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//----------------------------------------------------------------
19
20/**
21 * Check whether file exists:
22 */
23inline bool fileExists(const char * str)
24{
25struct stat stFileInfo;
26if((stat(str,&stFileInfo)) == 0) return true;
27else return false;
28}
29
30inline bool fileExists(std::string str) {return fileExists(str.c_str());}
31
32//----------------------------------------------------------------
33class ShellProcess
34{
35public:
36// construction / destruction
37ShellProcess() {_fpt= NULL;}
38~ShellProcess() {if (_fpt) close();}
39
40FILE * open(const char *cmd, const char *mode="r");
41int close();
42FILE * desc() const { return _fpt;} // non null if file is open
43char * get_line(char * line, size_t s) const {return _fpt ? fgets(line, s, _fpt) : NULL;}
44
45protected:
46FILE * _fpt;
47};
48
49//----------------------------------------------------------------
50class PartitionInfo
51{
52public:
53PartitionInfo() : _disk(0), _part(0) {}
54PartitionInfo(int disk, int part) {set(disk, part);}
55
56void disk(int disk) { _disk=disk;}
57int disk() const { return _disk;}
58
59void partition(int part) { _part=part;}
60int partition () const { return _part;}
61
62const char * clabel() const { return _label.c_str();}
63const std::string& label() const { return _label;}
64void label(const char * l) {
65if (l) _label = l;
66removeSpaces(_label);
67}
68
69const char * cfsType() const { return _fsType.c_str();}
70const std::string& fsType() const { return _fsType;}
71void fsType(const char * fs) {
72if (fs) _fsType = fs;
73removeSpaces(_fsType);
74}
75
76void set(int disk, int part) { _disk =disk; _part = part;}
77bool fromPartitionHdString(const char * inHdStr);
78std::string toHdStr() const
79{
80std::string buf = "hd(n,m)";
81buf[3]= '0'+disk();
82buf[5]='0'+partition();
83return buf;
84}
85int imageIndexFromFs() const;
86protected:
87void removeSpaces(std::string & s);
88
89private:
90int _disk, _part;
91std::string _fsType, _label;
92};
93
94static inline bool isDiskIndexInf(PartitionInfo i, PartitionInfo j)
95{
96return ((i.disk()*100+i.partition()) < (j.disk()*100 + j.partition()) ) ;
97}
98
99//----------------------------------------------------------------
100class PartitionExtractor : public ShellProcess
101{
102public:
103PartitionExtractor() :ShellProcess() { init(); }
104
105const std::vector<PartitionInfo>& extractPartitions(
106const char* szHide=NULL,
107const char* szRenamed=NULL);
108int getListCount() const {return (int) _partList.size();}
109
110const std::vector<PartitionInfo>& partList() const {return _partList;}
111std::vector<PartitionInfo>& editPartList() {return _partList;}
112
113// get the index in the internal partlist of the hd(n,m) partition specified by the input string
114// return -1 if no match, >=0 if a match happens
115int getIndexFromHdStringSpec(const char*);
116
117void hidePartitions(const char* szParts){ _hiddenParts = (szParts ? szParts : "");}
118void renamedPartitions(const char* szParts){ _renamedParts = (szParts ? szParts : "");}
119void sortPartList() {sort(_partList.begin(), _partList.end(), isDiskIndexInf);}
120
121const char * checkForRename(const char * label, const char* szHd);
122
123void swapHD(int src, int dst)
124{
125if(src < 0 || src > MAX_HD-1 || dst < 0 || dst > MAX_HD-1) return;
126_hdRedirTable[src]=dst;
127_hdRedirTable[dst]=src;
128}
129
130void resetSwapping() { init();}
131
132protected:
133void init() {
134for (int i=0; i<MAX_HD; i++) _hdRedirTable[i]=i;
135}
136
137private:
138std::vector<PartitionInfo> _partList;
139int _hdRedirTable[MAX_HD];
140std::string _hiddenParts;
141std::string _renamedParts;
142
143};
144

Archive Download this file

Revision: 43