OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [ecostest/] [common/] [eCosTestUtils.cpp] - Blame information for rev 790

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
2
// -------------------------------------------                              
3
// This file is part of the eCos host tools.                                
4
// Copyright (C) 1998, 1999, 2000, 2006 Free Software Foundation, Inc.      
5
//
6
// This program is free software; you can redistribute it and/or modify     
7
// it under the terms of the GNU General Public License as published by     
8
// the Free Software Foundation; either version 2 or (at your option) any   
9
// later version.                                                           
10
//
11
// This program is distributed in the hope that it will be useful, but      
12
// WITHOUT ANY WARRANTY; without even the implied warranty of               
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
14
// General Public License for more details.                                 
15
//
16
// You should have received a copy of the GNU General Public License        
17
// along with this program; if not, write to the                            
18
// Free Software Foundation, Inc., 51 Franklin Street,                      
19
// Fifth Floor, Boston, MA  02110-1301, USA.                                
20
// -------------------------------------------                              
21
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
22
//=================================================================
23
//
24
//        eCosTestUtils.cpp
25
//
26
//        Utility functions
27
//
28
//=================================================================
29
//=================================================================
30
//#####DESCRIPTIONBEGIN####
31
//
32
// Author(s):     sdf
33
// Contributors:  sdf
34
// Date:          1999-04-01
35
// Description:   This class contains utility functions for use in the testing infrastructure
36
// Usage:
37
//
38
//####DESCRIPTIONEND####
39
 
40
#include "eCosStd.h"
41
#include "eCosSocket.h"
42
#include "eCosTestUtils.h"
43
#include "eCosThreadUtils.h"
44
#include "eCosTrace.h"
45
#include "TestResource.h"
46
 
47
LPCTSTR  const CeCosTestUtils::Tail(LPCTSTR  const pszFile)
48
{
49
  LPCTSTR pszTail=_tcsrchr(pszFile,_TCHAR('/'));
50
  if(0==pszTail){
51
    pszTail=_tcsrchr(pszFile,_TCHAR('\\'));
52
  }
53
  return (0==pszTail)?pszFile:pszTail+1;
54
}
55
 
56
// File iterator.  Gets next file in directory, avoiding _T(".") and _T("..")
57
bool CeCosTestUtils::NextFile (void *&pHandle,String &str)
58
{
59
#ifdef _WIN32
60
  WIN32_FIND_DATA fd;
61
  while(FindNextFile((HANDLE)pHandle,&fd)){
62
    LPCTSTR pszName=fd.cFileName;
63
#else // UNIX
64
    struct dirent *d;
65
    while((d=readdir((DIR *)pHandle))){
66
      LPCTSTR pszName=d->d_name;
67
#endif
68
      if(pszName[0]!='.'){
69
        str=pszName;
70
        return true;
71
      }
72
    }
73
    return false;
74
  }
75
 
76
  // Start file iteration and return first file.
77
bool CeCosTestUtils::StartSearch (void *&pHandle,String &str)
78
  {
79
#ifdef _WIN32
80
    WIN32_FIND_DATA fd;
81
    pHandle=(void *)FindFirstFile (_T("*.*"), &fd);
82
    if(INVALID_HANDLE_VALUE==(HANDLE)pHandle){
83
      ERROR(_T("Failed to open dir\n"));
84
      return false;
85
    } else if (fd.cFileName[0]=='.') {
86
      return NextFile(pHandle,str);
87
    } else {
88
                str=String(fd.cFileName);
89
      return true;
90
    }
91
#else // UNIX
92
    pHandle=(void *)opendir(_T("."));
93
    if(0==pHandle){
94
      ERROR(_T("Failed to open dir\n"));
95
      return false;
96
    }
97
    return NextFile(pHandle,str);
98
#endif
99
  }
100
 
101
  // End file iteration
102
void CeCosTestUtils::EndSearch (void *&pHandle)
103
  {
104
#ifdef _WIN32
105
    FindClose((HANDLE)pHandle);
106
#else // UNIX
107
    closedir((DIR *)pHandle);
108
#endif
109
}
110
 
111
 
112
// deal with common command-line  actions
113
bool CeCosTestUtils::CommandLine(int &argc,TCHAR **argv,bool bRequireResourceServer)
114
{
115
  LPCTSTR psz=_tgetenv(_T("RESOURCESERVER"));
116
  if(psz && !CTestResource::SetResourceServer(psz)){
117
    _ftprintf(stderr,_T("Illegal host:port '%s' defined in RESOURCESERVER environment variable\n"),psz);
118
    return false;
119
  }
120
 
121
  for(int i=1;i<argc;i++){
122
    if(_TCHAR('-')==*argv[i]){
123
      if(0==_tcscmp(argv[i],_T("-v"))){
124
        CeCosTrace::EnableTracing((CeCosTrace::TraceLevel)MAX(CeCosTrace::TracingEnabled(),CeCosTrace::TRACE_LEVEL_TRACE));
125
        // Shuffle the command line down to remove that which we have just seen:
126
        for(TCHAR **a=argv+i;(a[0]=a[1]);a++);
127
        argc--;i--; // counteract the increment
128
      } else if(0==_tcscmp(argv[i],_T("-V"))){
129
        CeCosTrace::EnableTracing((CeCosTrace::TraceLevel)MAX(CeCosTrace::TracingEnabled(),CeCosTrace::TRACE_LEVEL_VTRACE));
130
        // Shuffle the command line down to remove that which we have just seen:
131
        for(TCHAR **a=argv+i;(a[0]=a[1]);a++);
132
        argc--;i--; // counteract the increment
133
      } else if(0==_tcscmp(argv[i],_T("-o"))){
134
        if(i+1<argc){
135
          if(!CeCosTrace::SetOutput(argv[i+1])){
136
            _ftprintf(stderr,_T("Failed to direct output to %s\n"),argv[i+1]);
137
          }
138
          // Shuffle the command line down to remove that which we have just seen:
139
          for(TCHAR **a=argv+i;(a[0]=a[2]);a++);
140
          argc-=2;i-=2; // counteract the increment
141
        } else {
142
          return false;
143
        }
144
      } else if(0==_tcscmp(argv[i],_T("-O"))){
145
        if(i+1<argc){
146
          if(!CeCosTrace::SetError(argv[i+1])){
147
            _ftprintf(stderr,_T("Failed to direct error to %s\n"),argv[i+1]);
148
          }
149
          // Shuffle the command line down to remove that which we have just seen:
150
          for(TCHAR **a=argv+i;(a[0]=a[2]);a++);
151
          argc-=2;i-=2; // counteract the increment
152
        } else {
153
          return false;
154
        }
155
      } else if(0==_tcscmp(argv[i],_T("-r"))){
156
        if(i+1<argc){
157
          if(!CTestResource::SetResourceServer(argv[i+1])){
158
            _ftprintf(stderr,_T("Illegal host:port '%s'\n"),argv[i+1]);
159
            return false;
160
          }
161
          // Shuffle the command line down to remove that which we have just seen:
162
          for(TCHAR **a=argv+i;(a[0]=a[2]);a++);
163
          argc-=2;i-=2; // counteract the increment
164
        } else {
165
          return false;
166
        }
167
      } else if(0==_tcscmp(argv[i],_T("-version"))){
168
        const TCHAR *pszTail=_tcsrchr(argv[0],_TCHAR('/'));
169
        if(0==pszTail){
170
          pszTail=_tcsrchr(argv[0],_TCHAR('\\'));
171
        }
172
                        _tprintf (_T("%s %s (%s %s)\n"), (0==pszTail)?argv[0]:pszTail+1,ECOS_VERSION,  __DATE__, __TIME__);
173
        exit(0);
174
      }
175
    }
176
  }
177
 
178
  if(!CeCosSocket::Init() || !CeCosTestPlatform::Load()){
179
    return false;
180
  }
181
 
182
#ifndef _WIN32
183
  sigset_t mask;
184
 
185
  // Clean out all the signals
186
  sigemptyset(&mask);
187
 
188
  // Add our sigpipe
189
  sigaddset(&mask, SIGPIPE);
190
 
191
  sigprocmask(SIG_SETMASK, &mask, NULL);
192
 
193
#endif
194
 
195
  if(CTestResource::ResourceServerSet()){
196
    if(CTestResource::Load()){
197
      _ftprintf(stderr,_T("Connected to resource server %s\n"),(LPCTSTR)CTestResource::GetResourceServer());
198
    } else {
199
      _ftprintf(stderr,_T("Can't load from resource server %s\n"),(LPCTSTR)CTestResource::GetResourceServer());
200
      return false;
201
    }
202
  } else if (bRequireResourceServer) {
203
    _ftprintf(stderr,_T("You must specify a resource server using either the -r switch or by setting the RESOURCESERVER environment variable\n"));
204
    return false;
205
  }
206
 
207
  return true;
208
}
209
 
210
void CeCosTestUtils::UsageMessage(bool bRequireResourceServer)
211
{
212
  _ftprintf(stderr,
213
    _T("        -o file      : send standard output to named file\n")
214
    _T("        -O file      : send standard error  to named file\n"));
215
  if(bRequireResourceServer){
216
    _ftprintf(stderr,
217
    _T("        -r host:port : use this host:port as resourceserver [or set the RESOURCESERVER environment variable]\n")
218
    );
219
  }
220
  _ftprintf(stderr,
221
    _T("        -v           : vebose mode - trace to stderr\n")
222
    _T("        -V           : very verbose mode - trace to stderr\n")
223
    _T("        -version     : print a version string\n")
224
  );
225
}
226
 
227
const String CeCosTestUtils::HomeFile (LPCTSTR pszFile)
228
{
229
  String strFile;
230
#ifdef _WIN32
231
  LPCTSTR psz=_tgetenv(_T("HOMEDRIVE"));
232
  if(psz){
233
    strFile=psz;
234
    psz=_tgetenv(_T("HOMEPATH"));
235
    if(psz){
236
      strFile+=psz;
237
    }
238
    if(_TCHAR('\\')!=strFile[strFile.size()-1]){
239
      strFile+=_TCHAR('\\');
240
    }
241
    strFile+=pszFile;
242
  }
243
#else // UNIX
244
  LPCTSTR psz=_tgetenv(_T("HOME"));
245
  if(psz){
246
    strFile=psz;
247
    strFile+=_TCHAR('/');
248
    strFile+=pszFile;
249
  }
250
#endif
251
  return strFile;
252
}
253
 
254
bool CeCosTestUtils::Exists(LPCTSTR pszFile)
255
{
256
  struct _stat buf;
257
  return (0==_tstat(pszFile,&buf));
258
}
259
 
260
bool CeCosTestUtils::IsFile(LPCTSTR pszFile)
261
{
262
  struct _stat buf;
263
  return 0==_tstat(pszFile,&buf) && 0==(S_IFDIR&buf.st_mode);
264
}
265
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.