1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 // Copyright (C) 2003 John Dallaway
7 // This program is part of the eCos host tools.
9 // This program is free software; you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by the Free
11 // Software Foundation; either version 2 of the License, or (at your option)
14 // This program is distributed in the hope that it will be useful, but WITHOUT
15 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 // You should have received a copy of the GNU General Public License along with
20 // this program; if not, write to the Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // ----------------------------------------------------------------------------
25 //####COPYRIGHTEND####
26 //=================================================================
28 // eCosTestPlatform.cpp
30 // platform information implementation
32 //=================================================================
33 #include "eCosTestPlatform.h"
34 #include "eCosTestUtils.h"
35 #include "eCosTrace.h"
37 std::vector<CeCosTestPlatform> CeCosTestPlatform::arPlatforms;
39 const CeCosTestPlatform *CeCosTestPlatform::Get(LPCTSTR psz)
41 for(int i=0;i<(signed)arPlatforms.size();i++){
42 const CeCosTestPlatform &t=arPlatforms[i];
43 if(0==_tcsicmp(t.Name(),psz)){
50 CeCosTestPlatform::CeCosTestPlatformProperties::CeCosTestPlatformProperties(CeCosTestPlatform *pti)
52 Add(_T("platform"),pti->m_strName);
53 Add(_T("prefix"), pti->m_strPrefix);
54 Add(_T("commands"),pti->m_strCommands);
55 Add(_T("inferior"),pti->m_strInferior);
56 Add(_T("prompt"), pti->m_strPrompt);
57 Add(_T("ServerSideGdb"),pti->m_nServerSideGdb);
60 bool CeCosTestPlatform::LoadFromDir(LPCTSTR pszDir)
63 TRACE(_T("CeCosTestPlatform::LoadFromDir %s\n"),pszDir);
64 // Find all the files in directory pszDir and load from each of them
66 _tgetcwd(szOrigDir,sizeof szOrigDir-1);
67 if(0==_tchdir(pszDir)){
70 for(bool b=CeCosTestUtils::StartSearch(pHandle,strFile);b;b=CeCosTestUtils::NextFile(pHandle,strFile)){
71 if(CeCosTestUtils::IsFile(strFile)){
74 CeCosTestPlatformProperties prop(&t);
75 if(prop.LoadFromFile(strFile)){
78 ERROR(_T("Illegal platform specification in %s%c%s\n"),pszDir,cPathsep,(LPCTSTR)strFile);
83 CeCosTestUtils::EndSearch(pHandle);
85 ERROR(_T("Failed to change to %s from %s\n"),pszDir,szOrigDir);
93 bool CeCosTestPlatform::SaveToRegistry(HKEY hTopKey,LPCTSTR pszKey)
95 // save target info to the registry
97 CProperties::CreateKey(pszKey,hTopKey);
99 bool rc=ERROR_SUCCESS==RegOpenKeyEx (hTopKey, pszKey, 0L, KEY_ALL_ACCESS, &hKey);
101 for(int i=0;i<(signed)arPlatforms.size();i++){
104 const CeCosTestPlatform &ti=arPlatforms[i];
105 rc&=(ERROR_SUCCESS==RegCreateKeyEx(hKey,ti.Name(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey2, &dwDisp));
107 LPCTSTR pszPrefix=ti.Prefix();
108 LPCTSTR pszGdb =ti.GdbCmds();
109 LPCTSTR pszInferior=ti.Inferior();
110 LPCTSTR pszPrompt=ti.Prompt();
111 DWORD dwServerSideGdb=ti.ServerSideGdb()?1l:0l;
112 rc&=(ERROR_SUCCESS==RegSetValueEx(hKey2,_T("Prefix"),0,REG_SZ, (CONST BYTE *)pszPrefix,(1+_tcslen(pszPrefix))*sizeof (TCHAR))) &&
113 (ERROR_SUCCESS==RegSetValueEx(hKey2,_T("Commands"),0,REG_SZ, (CONST BYTE *)pszGdb,(1+_tcslen(pszGdb))*sizeof (TCHAR))) &&
114 (ERROR_SUCCESS==RegSetValueEx(hKey2,_T("Inferior"),0,REG_SZ, (CONST BYTE *)pszInferior,(1+_tcslen(pszInferior))*sizeof (TCHAR))) &&
115 (ERROR_SUCCESS==RegSetValueEx(hKey2,_T("Prompt"),0,REG_SZ, (CONST BYTE *)pszPrompt,(1+_tcslen(pszPrompt))*sizeof (TCHAR))) &&
116 (ERROR_SUCCESS==RegSetValueEx(hKey2,_T("ServerSideGdb"),0,REG_DWORD, (CONST BYTE *)&dwServerSideGdb,sizeof (DWORD)));
125 const String CeCosTestPlatform::GetGreatestSubkey (LPCTSTR pszKey)
130 if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_LOCAL_MACHINE, pszKey, 0L, KEY_READ, &hKey)) {
132 TCHAR pszBuffer [MAX_PATH + 1];
134 while (ERROR_SUCCESS == RegEnumKey (hKey, dwIndex++, (LPTSTR) pszBuffer, sizeof (pszBuffer))) {
135 if (strSubkey.compare (pszBuffer) < 0) {
136 strSubkey = pszBuffer;
143 TRACE (_T("CeCosTestPlatform::GetGreatestSubkey(\"%s\"): %s\n"), pszKey, (LPCTSTR)strSubkey);
148 bool CeCosTestPlatform::Load()
150 TRACE(_T("CeCosTestPlatform::Load\n"));
151 srand( (unsigned)time( NULL ) );
155 // get target info from the registry
156 String strPlatformsKey = _T("Software\\eCos Configuration Tool\\Platforms");
157 // strPlatformsKey += GetGreatestSubkey (_T("Software\\eCos"));
158 // strPlatformsKey += _T("\\Platforms");
161 bool rc=ERROR_SUCCESS==RegOpenKeyEx (HKEY_CURRENT_USER, strPlatformsKey, 0L, KEY_READ, &hKey);
164 // Found the given key.
165 // Subkeys' names are the target image names:
166 // Subkeys's values are:
169 // GdbCmd String [optional]
170 FILETIME ftLastWriteTime;
171 DWORD dwMaxSubKeyLen;
172 if(ERROR_SUCCESS==RegQueryInfoKey(hKey,NULL,NULL,NULL,&dwSubKeys,&dwMaxSubKeyLen,NULL,NULL,NULL,NULL,NULL,NULL)){
173 TCHAR *szName=new TCHAR[1+dwMaxSubKeyLen];
174 DWORD dwSizeName=sizeof(TCHAR)*(1+dwMaxSubKeyLen);
175 for(DWORD dwIndex=0;ERROR_SUCCESS==RegEnumKeyEx(hKey, dwIndex, szName, &dwSizeName, NULL, NULL, NULL, &ftLastWriteTime); dwIndex++){
177 if(t.LoadFromRegistry(hKey,szName)){
179 CeCosTestPlatform::Add(t);
181 dwSizeName=sizeof(TCHAR)*(1+dwMaxSubKeyLen);
188 const String strDir(CeCosTestUtils::HomeFile(_T(".eCosPlatforms")));
190 if(!CeCosTestUtils::Exists(strDir)){
196 ERROR(_T("Failed to initialize any targets\n"));
201 int CeCosTestPlatform::Add(const CeCosTestPlatform &t)
203 for(std::vector<CeCosTestPlatform>::iterator it=arPlatforms.begin();it!=arPlatforms.end();){
204 if(0==_tcsicmp(it->Name(),t.Name())){
205 // Careful - there's already something here with this name:
206 ERROR(_T("Warning: duplicate target info %s\n"),it->Name());
207 it=arPlatforms.erase(it);
212 arPlatforms.push_back(t);
213 return arPlatforms.size()-1;
216 void CeCosTestPlatform::RemoveAllPlatforms()
221 bool CeCosTestPlatform::LoadFromCommandString(LPCTSTR psz)
223 CeCosTestPlatformProperties prop(this);
224 return prop.LoadFromCommandString(psz);
228 bool CeCosTestPlatform::LoadFromRegistry(HKEY hKey,LPCTSTR pszKey)
230 CeCosTestPlatformProperties prop(this);
231 return prop.LoadFromRegistry(hKey,pszKey);
235 bool CeCosTestPlatform::Save()
237 const String strDir(CeCosTestUtils::HomeFile(_T(".eCosPlatforms")));
239 if(!CeCosTestUtils::Exists(strDir)){
240 String strPlatformsKey = _T("Software\\eCos Configuration Tool\\Platforms");
241 // strPlatformsKey += GetGreatestSubkey (_T("Software\\eCos"));
242 // strPlatformsKey += _T("\\Platforms");
244 return SaveToRegistry(HKEY_CURRENT_USER,strPlatformsKey);
247 return SaveToDir(strDir);
250 bool CeCosTestPlatform::SaveToDir (LPCTSTR pszDir)
254 TCHAR szOrigDir[256];
255 _tgetcwd(szOrigDir,sizeof szOrigDir-1);
256 if(0==_tchdir(pszDir)){
257 // Delete all the files under directory "pszDir"
259 for(bool b=CeCosTestUtils::StartSearch(pHandle,strFile);b;b=CeCosTestUtils::NextFile(pHandle,strFile)){
260 if(CeCosTestUtils::IsFile(strFile)){
264 CeCosTestUtils::EndSearch(pHandle);
267 for(int i=0;i<(signed)arPlatforms.size();i++){
268 CeCosTestPlatform &t=arPlatforms[i];
269 CeCosTestPlatformProperties prop(&t);
270 rc&=prop.SaveToFile(t.Name());
273 ERROR(_T("Failed to change to %s from %s\n"),pszDir,szOrigDir);