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

Archive Download this file

Revision: 47