Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da6405ed5acd6f5a5f55c119c287f2b78a5cadf6 (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
/*******************************************************************************
 * Copyright (c) 2007, 2016 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.core.settings.model.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.core.settings.model.util.SettingsSet.SettingLevel;

public abstract class AbstractEntryStorage {
	private int fKind;
	
	private static final String EMPTY_STRING = ""; //$NON-NLS-1$

	public AbstractEntryStorage(int kind){
		fKind = kind;
	}
	
	public int getKind(){
		return fKind;
	}
	
	public List<ICLanguageSettingEntry> getEntries(List<ICLanguageSettingEntry> list){
		SettingsSet settings = initCache();
		if(list == null)
			list = new ArrayList<ICLanguageSettingEntry>();
		
		ICLanguageSettingEntry entries[] = settings.getEntries();
		list.addAll(Arrays.asList(entries));
		return list;
	}
	
	protected void resetDefaults(){
		SettingsSet settings = createEmptySettings();
		SettingLevel[] levels = settings.getLevels();
		for(int i = 0; i < levels.length; i++){
			obtainEntriesFromLevel(i, null);
		}
	}
	
	public void setEntries(ICLanguageSettingEntry entries[]){
		if(entries == null){
			resetDefaults();
			return;
		}
		SettingsSet settings = initCache();
		
		settings.applyEntries(entries);
		
		SettingLevel levels[] = settings.getLevels();
		
		for(int i = 0; i < levels.length; i++){
			obtainEntriesFromLevel(i, levels[i]);
		}
	}
	
	protected SettingsSet initCache(){
		SettingsSet settings = createEmptySettings();
		SettingLevel levels[] = settings.getLevels();
		for(int i = 0; i < levels.length; i++){
			putEntriesToLevel(i, levels[i]);
		}
			
		settings.adjustOverrideState();
			
		return settings;
	}
	
	protected abstract void putEntriesToLevel(int levelNum, SettingLevel level);

	protected abstract void obtainEntriesFromLevel(int levelNum, SettingLevel level);

	protected abstract SettingsSet createEmptySettings();
	
	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] = EMPTY_STRING;
		}
		return nv;
	}
}

Back to the top