Chameleon Applications

Chameleon Applications Svn Source Tree

Root/branches/ErmaC/ChameleonPrefPane/Sources/PartitionInfoManager.mm

1//
2// PartitionInfoManager.mm
3// ChameleonPrefPane
4//
5// Created by Rekursor on 11-11-13.
6//
7
8#import "PartitionInfoManager.h"
9#include "ShellProcess.h"
10
11// Singleton defintion
12
13static NSMutableArray* sParts = nil;
14
15@implementation PartsInfoMgr
16
17-(id) init
18{
19[super init];
20
21return self;
22}
23
24
25static NSString *currentCompare = nil;
26static BOOL isPartMatchingNameSpec(PartitionInfoElement* pe, NSUInteger idx, BOOL* stop)
27{
28NSString* s = [currentCompare stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @" \t\""]];
29
30if ([ [pe hdString] isEqual:s]
31 || [ [pe bsdName] isEqual:s]
32 || [ [pe vName] isEqual:s]
33 || [ [pe vUUID] isEqual:s]
34|| [ [pe vAliasName] isEqual: s]
35)
36*stop= YES;
37
38return *stop;
39}
40+(PartitionInfoElement*) partWithName: (NSString*) nameSpec
41{
42return [PartsInfoMgr partWithName:nameSpec outIndex:nil];
43}
44/// Retrieve the part that maches the name
45+(PartitionInfoElement*) partWithName: (NSString*) nameSpec outIndex:(NSUInteger*) ind
46{
47if (nameSpec==nil || [nameSpec length]==0) return nil;
48currentCompare = nameSpec;
49NSArray * arr = [PartsInfoMgr parts];
50NSUInteger idx = [arr indexOfObjectPassingTest:
51 ^(id pe, NSUInteger idx, BOOL* stop) {return isPartMatchingNameSpec(pe, idx, stop); } ];
52
53PartitionInfoElement* part = nil;
54
55if (ind!=nil) *ind = idx;
56
57if (idx!= NSNotFound)
58{
59part = [arr objectAtIndex: idx];
60}
61return part;
62}
63
64/// Singleton definition
65+(NSArray*) stringsFromPartSpec: (NSString*) partSpec withCharsFromString:(NSString*) s
66{
67if (partSpec == nil || [partSpec length]==0) return nil;
68NSArray *chunks = [partSpec componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: s] ];
69return chunks;
70}
71
72/// Retrieve all the partitions
73+(NSArray*) parts
74{
75if (sParts==nil)
76{
77//[PartsInfoMgr resetSwapping];
78NSArray* partArr = [PartitionInfoElement createBSDPartitionList];
79sParts = (NSMutableArray*) [PartitionInfoElement extractInfoWithBSDNames: partArr withArray: nil];
80[partArr release];
81}
82return [sParts retain];
83}
84
85+(NSUInteger) count
86{
87return [(NSArray*)[PartsInfoMgr parts] count];
88}
89
90+(PartitionInfoElement*) objectAtIndex:(NSUInteger) index
91{
92
93PartitionInfoElement* elt = [ [[PartsInfoMgr parts] objectAtIndex:index] retain];
94
95
96return elt;
97}
98
99static void hideIterateOn(NSString* partSpec, NSString*filter)
100{
101for (currentCompare in [PartsInfoMgr stringsFromPartSpec:partSpec withCharsFromString: filter])
102{
103PartitionInfoElement * p = (currentCompare!=nil) && [currentCompare length]>0 ? [PartsInfoMgr partWithName: currentCompare] : nil;
104if (p!=nil)
105{
106[p setHidden: true];
107NSLog(@" Found part with name %@ : %@", currentCompare, p);
108}
109if ((currentCompare!=nil) && [currentCompare length]>0 )
110{
111// remove them so that they dont appear in next search
112NSMutableString *mut = [NSMutableString stringWithString:partSpec];
113[ mut replaceOccurrencesOfString: currentCompare withString:@"" options: NSCaseInsensitiveSearch range: NSMakeRange(0, [mut length]) ];
114partSpec = mut;
115}
116}
117}
118
119static NSString *sHideSpec=nil, *sRenSpec=nil;
120
121/// Update partitions alias with partSpec
122+(void) hideParts: (NSString*) hideSpec
123{
124if (hideSpec!=nil) {
125if (sHideSpec!=nil) [sHideSpec release];
126sHideSpec = [hideSpec copy];
127}
128
129// first prioritize strings in double quotes
130if ([sHideSpec rangeOfString:@"\""].location != NSNotFound)
131hideIterateOn(sHideSpec, @"\"");
132hideIterateOn(sHideSpec, @"\" \t\n");
133}
134
135
136static bool renIterateOn(NSString* partSpec, NSString*filter)
137{
138NSString *first=nil, *second=nil;
139
140for (NSString* s in [PartsInfoMgr stringsFromPartSpec:partSpec withCharsFromString: filter])
141{
142if ([s length]==0) continue;
143if (first==nil)
144first = s;
145else if (second==nil)
146{
147second = s;
148break;
149}
150}
151if (first && second)
152{
153PartitionInfoElement * p = (first!=nil) && [first length]>0 ? [PartsInfoMgr partWithName: first] : nil;
154if (p!=nil)
155{
156[p setVAliasName: second];
157NSLog(@" Rename part %@ -> %@", [p vName], second);
158return true;
159}
160}
161return false;
162}
163
164/// Update partitions hidden property with partSpec
165+(void) renameParts: (NSString*) renSpec
166{
167if (renSpec!=nil) {
168if (sRenSpec!=nil) [sRenSpec release];
169sRenSpec = [renSpec copy];
170}
171
172NSArray* sPairs =
173[sRenSpec componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @";"] ];
174for (NSString* sPair in sPairs)
175{
176bool ret=false;
177
178if ([sPair rangeOfString:@"\""].location != NSNotFound)
179ret = renIterateOn(sPair, @"\"");
180if(!ret)
181renIterateOn(sPair, @" \t\n");
182}
183}
184
185+(void) reload
186{
187[PartsInfoMgr reloadWithHideSpec:nil andRenameSpec:nil];
188}
189
190+(void) reloadWithHideSpec: (NSString*) hideSpec andRenameSpec:(NSString*) renSpec
191{
192if (hideSpec!=nil) {
193if (sHideSpec!=nil) [sHideSpec release];
194sHideSpec = [hideSpec copy];
195}
196
197if (renSpec!=nil) {
198if (sRenSpec!=nil) [sRenSpec release];
199sRenSpec = [renSpec copy];
200}
201
202if (sParts!=nil) [sParts release];
203sParts = nil;
204
205// does auto-generate parts first time
206[PartsInfoMgr hideParts:sHideSpec];
207[PartsInfoMgr renameParts:sRenSpec];
208}
209
210+(void)resetSwapping
211{
212for (int i=0; i<MAX_HD; i++) PartitionInfoElement.hdRedirTable[i]=i;
213}
214
215+(void)swapHD: (NSUInteger) src with:(NSUInteger) dst
216{
217if(src > MAX_HD-1 || dst > MAX_HD-1) return;
218PartitionInfoElement.hdRedirTable[src]=dst;
219PartitionInfoElement.hdRedirTable[dst]=src;
220
221}
222
223+(NSString*) getRenameStringFrom:(NSString*) currentStr andPartition: (PartitionInfoElement*) partInfo
224{
225if (partInfo != nil && currentStr!=nil)
226{
227NSMutableString* ms = [[NSMutableString alloc ] initWithCapacity: 512];
228
229NSArray* sPairs =
230 [currentStr componentsSeparatedByCharactersInSet:
231[NSCharacterSet characterSetWithCharactersInString: @";"] ];
232
233if ([sPairs count] > 0 )
234{
235
236for (NSString* s in sPairs)
237{
238if ( s==nil || [s length] == 0) continue;
239
240if ( [ s rangeOfString:[partInfo hdString]].location == NSNotFound )
241{
242if ([ms length] > 0) [ ms appendString:@";" ];
243[ms appendString: [NSString stringWithFormat:@"%@",s ]];
244}
245}
246
247}
248// now filter the case where name eauls alias -> no rename anymore
249if ( ![[partInfo vName] isEqual: [partInfo vAliasName]])
250{
251if ([ms length] > 0) [ ms appendString:@";" ];
252[ms appendString: [NSString stringWithFormat:@"%@ \"%@\"" ,
253 [partInfo hdString], [partInfo vAliasName] ]];
254}
255return ms;
256}
257
258return nil;
259}
260
261@end
262

Archive Download this file

Revision: 396