Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/ChameleonPrefPane/Sources/ShellProcess.h

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

Archive Download this file

Revision: 59