Logo ROOT  
Reference Guide
TSystemFile.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Rene Brun 26/06/96
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TSystemFile
13 \ingroup Base
14 
15 A TSystemFile describes an operating system file.
16 The information is used by the browser (see TBrowser).
17 */
18 
19 #include "TSystemFile.h"
20 #include "TBrowser.h"
21 #include "TSystem.h"
22 #include "TEnv.h"
23 
24 
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// TSystemFile default constructor
29 
31 {
32 }
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// TSystemFile normal constructor
36 
37 TSystemFile::TSystemFile(const char *filename, const char *dirname)
38  : TNamed(filename, dirname)
39 {
41 }
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Delete TSystemFile object.
45 
47 {
48 }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Check if object is a directory.
52 
53 Bool_t TSystemFile::IsDirectory(const char *dir) const
54 {
55  Long64_t size;
56  Long_t id, flags, modtime;
57 
58  flags = id = size = modtime = 0;
59  gSystem->GetPathInfo(!dir ? fName.Data() : dir, &id, &size, &flags, &modtime);
60  Int_t isdir = (Int_t)flags & 2;
61 
62  return isdir ? kTRUE : kFALSE;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Execute default action for this system file (action is specified
67 /// in the $HOME/.root.mimes or $ROOTSYS/etc/root.mimes file.
68 
70 {
71  if (b)
72  b->ExecuteDefaultAction(this);
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Invoke text editor on this file
77 
79 {
80 #ifndef _WIN32
81  const char *ed = gEnv->GetValue("Editor", "vi");
82  Int_t nch = strlen(ed)+strlen(GetName()) + 50;
83  Char_t *cmd = new Char_t[nch];
84  if (!strcmp(ed, "vi"))
85  snprintf(cmd,nch, "xterm -e vi %s &", GetName());
86  else
87  snprintf(cmd,nch, "%s %s &", ed, GetName());
88 #else
89  const char *ed = gEnv->GetValue("Editor", "notepad");
90  Int_t nch = strlen(ed)+strlen(GetName()) + 50;
91  Char_t *cmd = new Char_t[nch];
92  snprintf(cmd,nch, "start %s %s", ed, GetName());
93 #endif
94  gSystem->Exec(cmd);
95 
96  delete [] cmd;
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 /// copy this file
101 
102 void TSystemFile::Copy(const char *to)
103 {
104  TString name = to;
105 
106  if (IsDirectory(to)) {
107  if (name.EndsWith("/")) name.Chop();
108  char *s = gSystem->ConcatFileName(name, fName);
109  name = s;
110  delete [] s;
111  }
112 
113  Int_t status = gSystem->CopyFile(fName, name, kFALSE);
114 
115  if (status == -2) {
116  Warning("Copy", "File %s already exists", name.Data());
117  } else if (status == -1) {
118  Warning("Copy", "Failed to move file %s", name.Data());
119  }
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// move this file
124 
125 void TSystemFile::Move(const char *to)
126 {
127  if (!to) {
128  Warning("Move", "No file/dir name specified");
129  return;
130  }
131 
132  TString name = to;
133 
134  if (IsDirectory(to)) {
135  if (name.EndsWith("/")) name.Chop();
136  char *s = gSystem->ConcatFileName(name, fName);
137  name = s;
138  delete [] s;
139  }
140  Int_t status = gSystem->CopyFile(fName, name, kFALSE);
141 
142  if (!status) {
143  gSystem->Unlink(fName);
144  } else if (status == -2) {
145  Warning("Move", "File %s already exists", name.Data());
146  } else if (status == -1) {
147  Warning("Move", "Failed to move file %s", name.Data());
148  }
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// delete this file
153 
155 {
156  gSystem->Unlink(fName);
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// rename this file
161 
162 void TSystemFile::Rename(const char *name)
163 {
165 }
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// inspect this file
169 
171 {
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// dump this file
176 
177 void TSystemFile::Dump() const
178 {
179 }
180 
TSystem::Unlink
virtual int Unlink(const char *name)
Unlink, i.e.
Definition: TSystem.cxx:1379
TSystem::GetPathInfo
int GetPathInfo(const char *path, Long_t *id, Long_t *size, Long_t *flags, Long_t *modtime)
Get info about a file: id, size, flags, modification time.
Definition: TSystem.cxx:1396
TBrowser
Definition: TBrowser.h:37
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
snprintf
#define snprintf
Definition: civetweb.c:1540
TSystemFile::Dump
virtual void Dump() const
dump this file
Definition: TSystemFile.cxx:177
gEnv
R__EXTERN TEnv * gEnv
Definition: TEnv.h:171
TString::Data
const char * Data() const
Definition: TString.h:369
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TSystemFile::~TSystemFile
virtual ~TSystemFile()
Delete TSystemFile object.
Definition: TSystemFile.cxx:46
TSystemFile::Move
virtual void Move(const char *to)
move this file
Definition: TSystemFile.cxx:125
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
Int_t
int Int_t
Definition: RtypesCore.h:45
TBrowser.h
TNamed::fName
TString fName
Definition: TNamed.h:38
TSystemFile::Copy
virtual void Copy(const char *to)
copy this file
Definition: TSystemFile.cxx:102
TEnv::GetValue
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition: TEnv.cxx:492
TSystemFile::IsDirectory
virtual Bool_t IsDirectory(const char *dir=0) const
Check if object is a directory.
Definition: TSystemFile.cxx:53
TEnv.h
TString
Definition: TString.h:136
b
#define b(i)
Definition: RSha256.hxx:118
TSystem::CopyFile
virtual int CopyFile(const char *from, const char *to, Bool_t overwrite=kFALSE)
Copy a file.
Definition: TSystem.cxx:1339
bool
id
XFontStruct * id
Definition: TGX11.cxx:109
TSystemFile::Rename
virtual void Rename(const char *name)
rename this file
Definition: TSystemFile.cxx:162
TSystem.h
TSystemFile
Definition: TSystemFile.h:29
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TNamed
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TSystem::Exec
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition: TSystem.cxx:654
Long_t
long Long_t
Definition: RtypesCore.h:54
TSystem::Rename
virtual int Rename(const char *from, const char *to)
Rename a file.
Definition: TSystem.cxx:1348
TSystemFile::Delete
virtual void Delete()
delete this file
Definition: TSystemFile.cxx:154
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TObject::kCanDelete
@ kCanDelete
if object in a list can be deleted
Definition: TObject.h:58
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TSystemFile::Browse
virtual void Browse(TBrowser *b)
Execute default action for this system file (action is specified in the $HOME/.root....
Definition: TSystemFile.cxx:69
name
char name[80]
Definition: TGX11.cxx:110
TSystem::ConcatFileName
virtual char * ConcatFileName(const char *dir, const char *name)
Concatenate a directory and a file name. User must delete returned string.
Definition: TSystem.cxx:1069
TSystemFile::Edit
virtual void Edit()
Invoke text editor on this file.
Definition: TSystemFile.cxx:78
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
Char_t
char Char_t
Definition: RtypesCore.h:33
TSystemFile::TSystemFile
TSystemFile()
TSystemFile default constructor.
Definition: TSystemFile.cxx:30
TSystemFile::Inspect
virtual void Inspect() const
inspect this file
Definition: TSystemFile.cxx:170
int
TSystemFile.h