Skip to main content
summaryrefslogtreecommitdiffstats
blob: a5366deef6fee1c63ad5af073bc0b08bb7b6a9c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*******************************************************************************
 * Copyright (c) 2007 Intel Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.dataprovider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.core.settings.model.CMacroEntry;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingPathEntry;
import org.eclipse.cdt.core.settings.model.ICLibraryFileEntry;
import org.eclipse.cdt.core.settings.model.ICSettingEntry;
import org.eclipse.cdt.core.settings.model.util.AbstractEntryStorage;
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
import org.eclipse.cdt.core.settings.model.util.SettingsSet;
import org.eclipse.cdt.core.settings.model.util.SettingsSet.EntryInfo;
import org.eclipse.cdt.core.settings.model.util.SettingsSet.SettingLevel;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IEnvVarBuildPath;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.IOptionPathConverter;
import org.eclipse.cdt.managedbuilder.core.IResourceInfo;
import org.eclipse.cdt.managedbuilder.core.IReverseOptionPathConverter;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.managedbuilder.core.OptionStringValue;
import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentVariableProvider;
import org.eclipse.cdt.managedbuilder.internal.core.Option;
import org.eclipse.cdt.managedbuilder.internal.macros.DefaultMacroContextInfo;
import org.eclipse.cdt.managedbuilder.internal.macros.OptionContextData;
import org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider;
import org.eclipse.cdt.utils.cdtvariables.CdtVariableResolver;
import org.eclipse.cdt.utils.cdtvariables.SupplierBasedCdtVariableSubstitutor;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class BuildEntryStorage extends AbstractEntryStorage {
	private BuildLanguageData fLangData;
	private String fBuildDirName;

	private static class UserEntryInfo {
		private ICLanguageSettingEntry fEntry;
		private OptionStringValue fOriginalValue;
		private OptionStringValue fBsResolvedValue;
		private List fSequense;
		
		UserEntryInfo(ICLanguageSettingEntry entry, OptionStringValue originalValue, OptionStringValue bsResolvedValue, List sequense){
			fEntry = entry;
			fOriginalValue = originalValue;
			fBsResolvedValue = bsResolvedValue;
			fSequense = sequense;
			if(sequense != null)
				sequense.add(this);
		}
	}
	
	private static class EmptyEntryInfo {
		private OptionStringValue fOriginalValue;
		private int fPosition;
		
		EmptyEntryInfo(OptionStringValue value, int position){
			fOriginalValue = value;
			fPosition = position;
		}
	}


	public BuildEntryStorage(int kind, BuildLanguageData lData) {
		super(kind);
		fLangData = lData;
	}

	protected SettingsSet createEmptySettings() {
		SettingsSet settings = new SettingsSet(3);
		SettingLevel levels[] = settings.getLevels();
		
		boolean override = isDiscoveredEntriesOverridable(); 
		int readOnlyFlag = override ? 0 : ICSettingEntry.READONLY;
		levels[0].setFlagsToClear(ICSettingEntry.READONLY | ICSettingEntry.BUILTIN);
		levels[0].setFlagsToSet(0);
		levels[0].setReadOnly(false);
		levels[0].setOverrideSupported(override);

		levels[1].setFlagsToClear(ICSettingEntry.BUILTIN);
		levels[1].setFlagsToSet(readOnlyFlag | ICSettingEntry.RESOLVED);
		levels[1].setReadOnly(true);
		levels[1].setOverrideSupported(false);

		levels[2].setFlagsToClear(0);
		levels[2].setFlagsToSet(readOnlyFlag | ICSettingEntry.BUILTIN | ICSettingEntry.RESOLVED);
		levels[2].setReadOnly(true);
		levels[2].setOverrideSupported(false);

		return settings;
	}
	
	private boolean isDiscoveredEntriesOverridable(){
		return fLangData.getUndefOptionsForKind(getKind()).length != 0;
	}

	private String getBuildDitName(){
		if(fBuildDirName == null){
			fBuildDirName = fLangData.getConfiguration().getName();
		}
		return fBuildDirName;
	}
	
	protected void obtainEntriesFromLevel(int levelNum, SettingLevel level) {
		switch(levelNum){
		case 0:
			if(level == null)
				restoreDefaults();
			else {
				EntryInfo infos[] = level.getInfos();
				UserEntryInfo[] userInfos = new UserEntryInfo[infos.length];
				for(int i = 0; i < infos.length; i++){
					UserEntryInfo uei = (UserEntryInfo)infos[i].getCustomInfo();
					if(uei == null)
						uei = new UserEntryInfo(infos[i].getEntry(), null, null, null);
					userInfos[i] = uei;
				}
				setUserEntries(userInfos, (List)level.getContext());
				setUserUndefinedStringSet(level.containsOverrideInfo() ? level.getOverrideSet() : null);
			}
			break;
		}
	}
	
	private void restoreDefaults(){
		IOption options[] = fLangData.getOptionsForKind(getKind());
		ITool tool = fLangData.getTool();
		for(int i = 0; i < options.length; i++){
			IOption option = options[i];
			if(option.getParent() == tool){
				tool.removeOption(option);
			}
		}
		
		options = fLangData.getUndefOptionsForKind(getKind());
		for(int i = 0; i < options.length; i++){
			IOption option = options[i];
			if(option.getParent() == tool){
				tool.removeOption(option);
			}
		}
	}

	protected void putEntriesToLevel(int levelNum, SettingLevel level) {
		switch(levelNum){
		case 0:
			List emptyEntryInfos = new ArrayList();
			UserEntryInfo[] userEntries = getUserEntries(level.getFlags(0), true, emptyEntryInfos);
			for(int i = 0; i < userEntries.length; i++){
				level.addEntry(userEntries[i].fEntry, userEntries[i]);
			}
			level.addOverrideNameSet(getUserUndefinedStringSet());
			if(emptyEntryInfos.size() != 0)
				level.setContext(emptyEntryInfos);
			break;
		case 1:
			ICLanguageSettingEntry[] envEntries = getEnvEntries(level.getFlags(0));
			level.addEntries(envEntries);
			break;
		case 2:
			ICLanguageSettingEntry[] discoveredEntries = getDiscoveredEntries(level.getFlags(0));
			level.addEntries(discoveredEntries);
			break;
		}
	}
	
	private ICLanguageSettingEntry[] getDiscoveredEntries(int flags){
		ICLanguageSettingEntry[] entries = ProfileInfoProvider.getInstance().getEntryValues(fLangData, getKind(), flags);
		if(entries == null || entries.length == 0){
			UserEntryInfo[] infos = getUserEntries(flags, false, null);
			if(infos.length != 0){
				entries = new ICLanguageSettingEntry[infos.length];
				for(int i = 0; i < entries.length; i++){
					entries[i] = infos[i].fEntry;
				}
			}
		}
		return entries;
	}
	
	private SupplierBasedCdtVariableSubstitutor createSubstitutor(IOption option, boolean bsVarsOnly){
		OptionContextData ocd = new OptionContextData(option, fLangData.getTool());
		DefaultMacroContextInfo ci = new DefaultMacroContextInfo(IBuildMacroProvider.CONTEXT_OPTION, ocd);
		
		return bsVarsOnly ? 
				new BuildSystemSpecificVariableSubstitutor(ci)
				: new SupplierBasedCdtVariableSubstitutor(ci, "", " "); //$NON-NLS-1$ //$NON-NLS-2$
	}
	
	private UserEntryInfo[] getUserEntries(int flags, boolean usr, List emptyValuesInfos){
		IOption options[] = fLangData.getOptionsForKind(getKind());
		if(options.length > 0){
			List entryList = new ArrayList();
			for(int i = 0; i < options.length; i++){
				Option option = (Option)options[i];
				List list = usr ? (List)option.getExactValue() : option.getExactBuiltinsList();
				int size = list != null ? list.size() : 0;
				if(size > 0){
					SupplierBasedCdtVariableSubstitutor subst = createSubstitutor(option, false);
					SupplierBasedCdtVariableSubstitutor bSVarsSubst = createSubstitutor(option, true);
					for(int j = 0; j < size; j++){
						OptionStringValue ve = (OptionStringValue)list.get(j);
						OptionStringValue[] rVes = resolve(ve, option, bSVarsSubst);
						if(rVes.length == 0){
							if(emptyValuesInfos != null){
								emptyValuesInfos.add(new EmptyEntryInfo(ve, j));
							}
						} else {
							boolean isMultiple = rVes.length > 1;
							List sequense = isMultiple ? new ArrayList(rVes.length) : null;
							for(int k = 0; k < rVes.length; k++){
								OptionStringValue rVe = rVes[k];
								ICLanguageSettingEntry entry = createUserEntry(option, rVe, flags, subst);  
								entryList.add(new UserEntryInfo(entry, ve, rVe, sequense));
							}
						}
					}
				}
			}
			
			return (UserEntryInfo[])entryList.toArray(new UserEntryInfo[entryList.size()]);
		}
		return new UserEntryInfo[0];
	}
	
//	private static OptionStringValue stripQuotes(OptionStringValue ov){
//		String value = ov.getValue();
//		value = stripQuotes(value, true);
//		if(value != null){
//			value = value.substring(1, value.length() - 1);
//			ov = substituteValue(ov, value);
//		}
//		return ov;
//	}
	
	private static String stripQuotes(String value, boolean nullIfNone){
		if(value.indexOf('"') == 0 && value.lastIndexOf('"') == value.length() - 1 && value.length() != 1){
			return value.substring(1, value.length() - 1);
		}
		return nullIfNone ? null : value;
		
	}
	
	private static OptionStringValue substituteValue(OptionStringValue ov, String value){
		return new OptionStringValue(value, ov.isBuiltIn(), ov.getSourceAttachmentPath(), ov.getSourceAttachmentRootPath(), ov.getSourceAttachmentPrefixMapping());
	}
	
	private HashSet getUserUndefinedStringSet(){
		HashSet set = null;
		IOption options[] = fLangData.getUndefOptionsForKind(getKind());
		if(options.length > 0){
			for(int i = 0; i < options.length; i++){
				IOption option = options[i];
				List list = (List)option.getValue();
				if(list.size() != 0){
					if(set == null)
						set = new HashSet();
					set.addAll(list);
				}
			}
		}
		return set;
	}
	
	private PathInfo fromBuildToProj(PathInfo info){
		if(info.isAbsolute())
			return info;
		
		Path path = new Path(info.getUnresolvedPath());
		String projPath;
		if(path.segmentCount() != 0 && "..".equals(path.segment(0))){ //$NON-NLS-1$
			projPath = path.removeFirstSegments(1).toString();
		} else {
			StringBuffer buf = new StringBuffer();
			buf.append(getBuildDitName()).append('/').append(info.getUnresolvedPath());
			projPath = buf.toString();
		}
		return new PathInfo(projPath, info.isWorkspacePath(), info.getSubstitutor());
	}

	private PathInfo fromProjToBuild(PathInfo info){
		if(info.isAbsolute())
			return info;
		
		Path path = new Path(info.getUnresolvedPath());
		String projPath;
		if(path.segmentCount() != 0 && getBuildDitName().equals(path.segment(0))){
			projPath = path.removeFirstSegments(1).toString();
		} else {
			StringBuffer buf = new StringBuffer();
			buf.append("../").append(info.getUnresolvedPath()); //$NON-NLS-1$
			projPath = buf.toString();
		}
		return new PathInfo(projPath, info.isWorkspacePath(), info.getSubstitutor());
	}

//	private String[] resolve(String v, IOption option, IPath[] buildLocation){
//		
//	}

	private String[] resolve(String v, IOption option, SupplierBasedCdtVariableSubstitutor sub){
		try {
			return CdtVariableResolver.resolveToStringList(v, sub);
		} catch (CdtVariableException e) {
			ManagedBuilderCorePlugin.log(e);
		}
		return new String[0];
	}
	
	private OptionStringValue[] resolve(OptionStringValue ov, IOption option, SupplierBasedCdtVariableSubstitutor sub){
		String value = ov.getValue();
		value = stripQuotes(value, false);
		String[] rValues = resolve(value, option, sub);
		OptionStringValue[] result = new OptionStringValue[rValues.length];
		for(int i = 0; i < result.length; i++){
			result[i] = substituteValue(ov, stripQuotes(rValues[i], false));
		}
		return result;
	}

	private ICLanguageSettingEntry[] getEnvEntries(int flags){
		String paths[] = null;
		int kind = getKind();
		switch(kind){
		case ICLanguageSettingEntry.INCLUDE_PATH:{
				IEnvironmentVariableProvider provider = ManagedBuildManager.getEnvironmentVariableProvider();
				paths = provider.getBuildPaths(fLangData.getConfiguration(), IEnvVarBuildPath.BUILDPATH_INCLUDE);
			}
			break;
		case ICLanguageSettingEntry.LIBRARY_PATH:{
				IEnvironmentVariableProvider provider = ManagedBuildManager.getEnvironmentVariableProvider();
				paths = provider.getBuildPaths(fLangData.getConfiguration(), IEnvVarBuildPath.BUILDPATH_LIBRARY);
			}
			break;
		}
		
		if(paths != null && paths.length != 0){
			ICLanguageSettingEntry entries[] = new ICLanguageSettingEntry[paths.length];
			for(int i = 0; i < paths.length; i++){
				entries[i] = (ICLanguageSettingEntry)CDataUtil.createEntry(kind, paths[i].toString(), null, null, flags);
			}
			
			return entries;
		}
		return new ICLanguageSettingEntry[0];
	}
	
	private ICLanguageSettingEntry createUserEntry(Option option, OptionStringValue optionValue, int flags, SupplierBasedCdtVariableSubstitutor subst){
//	private ICLanguageSettingEntry createUserEntry(Option option, String optionValue, int flags){
		int kind = getKind();
		
		ICLanguageSettingEntry entry = null;
		
		IPath srcPath = null, srcRootPath = null, srcPrefixMapping = null;
		
		switch (kind){
		case ICLanguageSettingEntry.MACRO:
			String nv[] = macroNameValueFromValue(optionValue.getValue());
//			String nv[] = macroNameValueFromValue(optionValue);
			
			entry = new CMacroEntry(nv[0], nv[1], flags);
			break;
//		case ICLanguageSettingEntry.INCLUDE_PATH:
//		case ICLanguageSettingEntry.INCLUDE_FILE:
//		case ICLanguageSettingEntry.MACRO_FILE:
//		case ICLanguageSettingEntry.LIBRARY_PATH:
//		case ICLanguageSettingEntry.LIBRARY_FILE:
		case ICLanguageSettingEntry.LIBRARY_FILE:
			String tmp = optionValue.getSourceAttachmentPath();
			if(tmp != null)
				srcPath = new Path(tmp);
			tmp = optionValue.getSourceAttachmentRootPath();
			if(tmp != null)
				srcRootPath = new Path(tmp);
			tmp = optionValue.getSourceAttachmentPrefixMapping();
			if(tmp != null)
				srcPrefixMapping = new Path(tmp);
			//do not break
		default:
			IOptionPathConverter optionPathConverter = fLangData.getTool().getOptionPathConverter();
			PathInfo pInfo = optionPathValueToEntry(optionValue.getValue(), subst);
//			Object[] v = optionPathValueToEntry(stripQuotes(optionValue.getValue()));
//			Object[] v = optionPathValueToEntry(optionValue);
			
			if(pInfo.isWorkspacePath()){
				flags |= ICLanguageSettingEntry.VALUE_WORKSPACE_PATH;
			} else if (optionPathConverter != null){
				IPath path = optionPathConverter.convertToPlatformLocation(pInfo.getUnresolvedPath(), option, fLangData.getTool());
				if(path != null){
					pInfo = new PathInfo(path.toString(), false, subst);
				}
			}
			
			pInfo = fromBuildToProj(pInfo);
			
			entry = (ICLanguageSettingEntry)CDataUtil.createEntry(kind, pInfo.getUnresolvedPath(), null, null, flags, srcPath, srcRootPath, srcPrefixMapping);
			break;
		}
		return entry;
	}
	
	private OptionStringValue createOptionValue(IOption option, UserEntryInfo info, SupplierBasedCdtVariableSubstitutor subst){
		if(info.fOriginalValue != null)
			return info.fOriginalValue;
		
		return entryValueToOption(option, info.fEntry, subst);
	}

	private OptionStringValue entryValueToOption(IOption option, ICLanguageSettingEntry entry, SupplierBasedCdtVariableSubstitutor subst){
		String optionValue = entryValueToOptionStringValue(option, entry, subst);
		if(entry.getKind() == ICSettingEntry.LIBRARY_FILE){
			ICLibraryFileEntry libFile = (ICLibraryFileEntry)entry;
			return new OptionStringValue(optionValue, 
					false,
					pathToString(libFile.getSourceAttachmentPath()),
					pathToString(libFile.getSourceAttachmentRootPath()),
					pathToString(libFile.getSourceAttachmentPrefixMapping()));
		}
		return new OptionStringValue(optionValue);
	}
	
	private static String pathToString(IPath path){
		return path != null ? path.toString() : null;
	}

	private String entryValueToOptionStringValue(IOption option, ICLanguageSettingEntry entry, SupplierBasedCdtVariableSubstitutor subst){
		String result;
		boolean checkQuote = true;
		if(entry.getKind() == ICLanguageSettingEntry.MACRO && entry.getValue().length() > 0){
			result = new StringBuffer(entry.getName()).append('=').append(entry.getValue()).toString();
		} else if(entry instanceof ICLanguageSettingPathEntry){
			IOptionPathConverter converter = fLangData.getTool().getOptionPathConverter();
			if(converter instanceof IReverseOptionPathConverter){
				result = ((IReverseOptionPathConverter)converter).convertToOptionValue(entry, option, fLangData.getTool());
				checkQuote = false;
			} else {
				ICLanguageSettingPathEntry pathEntry = (ICLanguageSettingPathEntry)entry;
				result = doConvertToOptionValue(option, pathEntry, subst);
			}
		} else {
			result = entry.getName();
		}
		
		if(checkQuote){
			result = doubleQuotePath(result, false);
		}
		return result;
	}
	
	private String doConvertToOptionValue(IOption option, ICLanguageSettingPathEntry pathEntry, SupplierBasedCdtVariableSubstitutor subst){
		boolean isWsp = pathEntry.isValueWorkspacePath();
		PathInfo pInfo = new PathInfo(pathEntry.getName(), isWsp, subst);
		String result;
		if(isWsp){
			if(!pInfo.isAbsolute()){
				IConfiguration cfg = fLangData.getConfiguration();
				IResource rc = cfg.getOwner();
				if(rc != null){
					IProject proj = rc.getProject();
					String path = pInfo.getUnresolvedPath();
					IPath p = proj.getFullPath().append(path);
					result = p.toString();
				} else {
					result = pathEntry.getName();
				}
			} else {
				result = pathEntry.getName();
			}
			
			result = ManagedBuildManager.fullPathToLocation(result);
		} else {
			pInfo = fromProjToBuild(pInfo);
			result = pInfo.getUnresolvedPath();
		}
		
		return result;
	}
	
	private static String doubleQuotePath(String pathName, boolean nullIfNone)	{
		/* Trim */
		pathName = pathName.trim();
		
		/* Check if path is already double-quoted */
		boolean bStartsWithQuote = pathName.indexOf('"') == 0;
		boolean bEndsWithQuote = pathName.lastIndexOf('"') == pathName.length() - 1;
		
		boolean quoted = false;
		
		/* Check for spaces, backslashes or macros */ 
		int i = pathName.indexOf(' ') + pathName.indexOf('\\') //$NON-NLS-1$ //$NON-NLS-2$
			+ pathName.indexOf("${"); //$NON-NLS-1$
		
		/* If indexof didn't fail all three times, double-quote path */
		if (i != -3) {
			if (!bStartsWithQuote){
				pathName = "\"" + pathName; //$NON-NLS-1$
				quoted = true;
			}
			if (!bEndsWithQuote){
				pathName = pathName + "\""; //$NON-NLS-1$
				quoted = true;
			}
		}
		
		if(quoted)
			return pathName;
		return nullIfNone ? null : pathName;
	}
	
	public static String[] macroNameValueFromValue(String value){
		String nv[] = new String[2];
		int index = value.indexOf('=');
		if(index > 0){
			nv[0] = value.substring(0, index);
			nv[1] = value.substring(index + 1);
		} else {
			nv[0] = value;
			nv[1] = ""; //$NON-NLS-1$
		}
		return nv;
	}
	
//	private static Object[] optionPathValueToEntry(String value){
//		String wspPath = ManagedBuildManager.locationToFullPath(value);
//		if(wspPath != null)
//			return new Object[]{wspPath, Boolean.valueOf(true)};
//		return new Object[]{value, Boolean.valueOf(false)};
//	}
	
	private static PathInfo optionPathValueToEntry(String str, SupplierBasedCdtVariableSubstitutor subst){
		String unresolvedStr = ManagedBuildManager.locationToFullPath(str);
		boolean isWorkspacePath;
		if(unresolvedStr != null){
			isWorkspacePath = true;
		} else {
			unresolvedStr = str;
			isWorkspacePath = false;
		}
		return new PathInfo(unresolvedStr, isWorkspacePath, subst);
	}
	
	private void setUserEntries(UserEntryInfo[] entries, List emptyEntryInfos){
		int kind = getKind();
		IOption options[] = fLangData.getOptionsForKind(kind);
		if(options.length != 0){
			IOption option = options[0];
			OptionStringValue[]  optValue;
			if(entries.length != 0){
				entries = combineSequenses(entries);
				
				entries = addEmptyEntries(entries, emptyEntryInfos);
				
				optValue = new OptionStringValue[entries.length];
				SupplierBasedCdtVariableSubstitutor subst = createSubstitutor(option, false);

				for(int i = 0; i < entries.length; i++){
					optValue[i] = createOptionValue(option, entries[i], subst);
				}
			} else {
				optValue = Option.EMPTY_LV_ARRAY;
			}

			ITool tool = fLangData.getTool();
			IResourceInfo rcInfo = tool.getParentResourceInfo();
			IOption newOption = ManagedBuildManager.setOption(rcInfo, tool, option, optValue);
			options = fLangData.getOptionsForKind(kind);
			for(int i = 0; i < options.length; i++){
				if(options[i] != newOption)
					ManagedBuildManager.setOption(rcInfo, tool, options[i], new String[0]);
			}
		}
	}

	private UserEntryInfo[] addEmptyEntries(UserEntryInfo infos[], List emptyEntryInfos){
		if(emptyEntryInfos == null || emptyEntryInfos.size() == 0)
			return infos;

		LinkedList list = new LinkedList();
		list.addAll(Arrays.asList(infos));
		for(int i = 0; i < emptyEntryInfos.size(); i++){
			EmptyEntryInfo ei = (EmptyEntryInfo)emptyEntryInfos.get(i);
			int index = ei.fPosition;
			if(index > list.size())
				index = list.size();
			
			list.add(index, new UserEntryInfo(null, ei.fOriginalValue, ei.fOriginalValue, null));
		}
		
		return (UserEntryInfo[])list.toArray(new UserEntryInfo[list.size()]);
	}

	private UserEntryInfo[] combineSequenses(UserEntryInfo infos[]){
		if(infos.length == 0)
			return infos;

		List list = new ArrayList(infos.length);
		
		for(int i = 0; i < infos.length; i++){
			UserEntryInfo info = infos[i];
			if(info.fSequense != null) {
				boolean match = true;
				int seqSize = info.fSequense.size();
				if(seqSize > infos.length - i)
					match = false;
				else {
					for(int k = 0; k < seqSize; k++){
						if(info.fSequense.get(k) != infos[i + k]){
							match = false;
							break;
						}
					}
				}
				
				if(match){
					i = i + seqSize - 1; 
				} else {
					infos[i] = createDesecuencedEntry(info);
					for(int k = i + 1; k < infos.length; k++){
						if(infos[k].fSequense == info.fSequense)
							infos[k] = createDesecuencedEntry(infos[k]);
					}
					info = infos[i];
				}
			}
			list.add(info);
		}
		
		return (UserEntryInfo[])list.toArray(new UserEntryInfo[list.size()]);
	}
	
	private static UserEntryInfo createDesecuencedEntry(UserEntryInfo info){
		OptionStringValue resolvedValue = info.fBsResolvedValue;
		if(resolvedValue != null){
			String v = doubleQuotePath(resolvedValue.getValue(), true);
			if(v != null)
				resolvedValue = substituteValue(resolvedValue, v); 
		}
		return new UserEntryInfo(info.fEntry, resolvedValue, resolvedValue, null);
	}
	
	private void setUserUndefinedStringSet(Set set){
		int kind = getKind();
		IOption[] options = fLangData.getUndefOptionsForKind(kind);
		if(options.length != 0){
			if(set != null && set.size() == 0)
				set = null;
			
			String[] optValue = set != null ? (String[])set.toArray(new String[set.size()]) : new String[0];
			IOption option = options[0];
			ITool tool = fLangData.getTool();
			IResourceInfo rcInfo = tool.getParentResourceInfo();
			IOption newOption = ManagedBuildManager.setOption(rcInfo, tool, option, optValue);
			options = fLangData.getUndefOptionsForKind(kind);
			for(int i = 0; i < options.length; i++){
				if(options[i] != newOption)
					ManagedBuildManager.setOption(rcInfo, tool, option, new String[0]);
			}

		}

	}
	
	void optionsChanged(){
	}
}

Back to the top