Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/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{
19self = [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}
81return [sParts retain];
82}
83
84+(NSUInteger) count
85{
86return [(NSArray*)[PartsInfoMgr parts] count];
87}
88
89+(PartitionInfoElement*) objectAtIndex:(NSUInteger) index
90{
91
92PartitionInfoElement* elt = [[PartsInfoMgr parts] objectAtIndex:index];
93
94
95return elt;
96}
97
98static void hideIterateOn(NSString* partSpec, NSString*filter)
99{
100for (currentCompare in [PartsInfoMgr stringsFromPartSpec:partSpec withCharsFromString: filter])
101{
102PartitionInfoElement * p = (currentCompare!=nil) && [currentCompare length]>0 ? [PartsInfoMgr partWithName: currentCompare] : nil;
103if (p!=nil)
104{
105[p setHidden: true];
106NSLog(@" Found part with name %@ : %@", currentCompare, p);
107}
108if ((currentCompare!=nil) && [currentCompare length]>0 )
109{
110// remove them so that they dont appear in next search
111NSMutableString *mut = [NSMutableString stringWithString:partSpec];
112[ mut replaceOccurrencesOfString: currentCompare withString:@"" options: NSCaseInsensitiveSearch range: NSMakeRange(0, [mut length]) ];
113partSpec = mut;
114}
115}
116}
117
118static NSString *sHideSpec=nil, *sRenSpec=nil;
119
120/// Update partitions alias with partSpec
121+(void) hideParts: (NSString*) hideSpec
122{
123if (hideSpec!=nil) {
124if (sHideSpec!=nil) [sHideSpec release];
125sHideSpec = [hideSpec copy];
126}
127
128// first prioritize strings in double quotes
129if ([sHideSpec rangeOfString:@"\""].location != NSNotFound)
130hideIterateOn(sHideSpec, @"\"");
131hideIterateOn(sHideSpec, @"\" \t\n");
132}
133
134
135static bool renIterateOn(NSString* partSpec, NSString*filter)
136{
137NSString *first=nil, *second=nil;
138
139for (NSString* s in [PartsInfoMgr stringsFromPartSpec:partSpec withCharsFromString: filter])
140{
141if ([s length]==0) continue;
142if (first==nil)
143first = s;
144else if (second==nil)
145{
146second = s;
147break;
148}
149}
150if (first && second)
151{
152PartitionInfoElement * p = (first!=nil) && [first length]>0 ? [PartsInfoMgr partWithName: first] : nil;
153if (p!=nil)
154{
155[p setVAliasName: second];
156NSLog(@" Rename part %@ -> %@", [p vName], second);
157return true;
158}
159}
160return false;
161}
162
163/// Update partitions hidden property with partSpec
164+(void) renameParts: (NSString*) renSpec
165{
166if (renSpec!=nil) {
167if (sRenSpec!=nil) [sRenSpec release];
168sRenSpec = [renSpec copy];
169}
170
171NSArray* sPairs =
172[sRenSpec componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @";"] ];
173for (NSString* sPair in sPairs)
174{
175bool ret=false;
176
177if ([sPair rangeOfString:@"\""].location != NSNotFound)
178ret = renIterateOn(sPair, @"\"");
179if(!ret)
180renIterateOn(sPair, @" \t\n");
181}
182}
183
184+(void) reload
185{
186[PartsInfoMgr reloadWithHideSpec:nil andRenameSpec:nil];
187}
188
189+(void) reloadWithHideSpec: (NSString*) hideSpec andRenameSpec:(NSString*) renSpec
190{
191if (hideSpec!=nil) {
192if (sHideSpec!=nil) [sHideSpec release];
193sHideSpec = [hideSpec copy];
194}
195
196if (renSpec!=nil) {
197if (sRenSpec!=nil) [sRenSpec release];
198sRenSpec = [renSpec copy];
199}
200
201if (sParts!=nil) [sParts release];
202sParts = nil;
203
204// does auto-generate parts first time
205[PartsInfoMgr hideParts:sHideSpec];
206[PartsInfoMgr renameParts:sRenSpec];
207}
208
209+(void)resetSwapping
210{
211for (int i=0; i<MAX_HD; i++) PartitionInfoElement.hdRedirTable[i]=i;
212}
213
214+(void)swapHD: (NSUInteger) src with:(NSUInteger) dst
215{
216if(src > MAX_HD-1 || dst > MAX_HD-1) return;
217PartitionInfoElement.hdRedirTable[src]=dst;
218PartitionInfoElement.hdRedirTable[dst]=src;
219
220}
221
222+(NSString*) getRenameStringFrom:(NSString*) currentStr andPartition: (PartitionInfoElement*) partInfo
223{
224if (partInfo != nil && currentStr!=nil)
225{
226NSMutableString* ms = [[[NSMutableString alloc ] initWithCapacity: 512] autorelease];
227
228NSArray* sPairs =
229 [currentStr componentsSeparatedByCharactersInSet:
230[NSCharacterSet characterSetWithCharactersInString: @";"] ];
231
232if ([sPairs count] > 0 )
233{
234
235for (NSString* s in sPairs)
236{
237if ( s==nil || [s length] == 0) continue;
238
239if ( [ s rangeOfString:[partInfo hdString]].location == NSNotFound )
240{
241if ([ms length] > 0) [ ms appendString:@";" ];
242[ms appendString: [NSString stringWithFormat:@"%@",s ]];
243}
244}
245
246}
247// now filter the case where name eauls alias -> no rename anymore
248if ( ![[partInfo vName] isEqual: [partInfo vAliasName]])
249{
250if ([ms length] > 0) [ ms appendString:@";" ];
251[ms appendString: [NSString stringWithFormat:@"%@ \"%@\"" ,
252 [partInfo hdString], [partInfo vAliasName] ]];
253}
254return ms;
255}
256
257return nil;
258}
259
260@end
261

Archive Download this file

Revision: 451