Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
25 ClassImp(TSystemFile);
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// TSystemFile default constructor
29 
30 TSystemFile::TSystemFile() : TNamed()
31 {
32 }
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// TSystemFile normal constructor
36 
37 TSystemFile::TSystemFile(const char *filename, const char *dirname)
38  : TNamed(filename, dirname)
39 {
40  SetBit(kCanDelete);
41 }
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Delete TSystemFile object.
45 
46 TSystemFile::~TSystemFile()
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 
69 void TSystemFile::Browse(TBrowser *b)
70 {
71  if (b)
72  b->ExecuteDefaultAction(this);
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Invoke text editor on this file
77 
78 void TSystemFile::Edit()
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 
154 void TSystemFile::Delete()
155 {
156  gSystem->Unlink(fName);
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// rename this file
161 
162 void TSystemFile::Rename(const char *name)
163 {
164  gSystem->Rename(fName, name);
165 }
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// inspect this file
169 
170 void TSystemFile::Inspect() const
171 {
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// dump this file
176 
177 void TSystemFile::Dump() const
178 {
179 }
180