HenrikBengtsson / R.rsp
1
###########################################################################/**
2
# @RdocClass FileRspResponse
3
#
4
# @title "The FileRspResponse class"
5
#
6
# \description{
7
#  @classhierarchy
8
# }
9
#
10
# @synopsis
11
#
12
# \arguments{
13
#   \item{file}{A filename or a @connection to write responses to.}
14
#   \item{path}{An optional path to the file.}
15
#   \item{overwrite}{If @FALSE, an error is thrown if the output file already
16
#     exists, otherwise not.}
17
#   \item{...}{Not used.}
18
# }
19
#
20
# \section{Fields and Methods}{
21
#  @allmethods
22
# }
23
#
24
# @author
25
#
26
# @keyword internal
27
#*/###########################################################################
28
setConstructorS3("FileRspResponse", function(file=stdout(), path=NULL, overwrite=FALSE, ...) {
29
  # Argument 'file' and 'path':
30 1
  if (is.character(file)) {
31 0
    file <- Arguments$getWritablePathname(file=file, path=path, mustNotExist=!overwrite)
32
    # Empty the file
33 0
    cat(file=file, "")
34 1
  } else if (!inherits(file, "connection")) {
35 0
    throw("Argument 'file' must be a filename or a connection: ",
36 0
                                                          class(file)[1])
37
  }
38

39 1
  extend(RspResponse(), "FileRspResponse",
40 1
    file = file
41
  )
42
})
43

44

45

46
#########################################################################/**
47
# @RdocMethod getOutput
48
#
49
# @title "Gets the output for an RSP response"
50
#
51
# \description{
52
#  @get "title".
53
# }
54
#
55
# @synopsis
56
#
57
# \arguments{
58
#   \item{...}{Not used.}
59
# }
60
#
61
# \value{
62
#  Returns a @connection or a filename.
63
# }
64
#
65
# @author
66
#
67
# \seealso{
68
#   @seeclass
69
# }
70
#
71
# @keyword IO
72
#*/#########################################################################
73
setMethodS3("getOutput", "FileRspResponse", function(this, ...) {
74 0
  this$file
75
})
76

77

78
setMethodS3("writeResponse", "FileRspResponse", function(this, ..., collapse="", sep="") {
79 0
  msg <- paste(..., collapse=collapse, sep=sep)
80 0
  msg <- as.character(GString(msg))
81 0
  out <- getOutput(this)
82 0
  cat(file=out, append=TRUE, msg)
83
})
84

85

86

87
setMethodS3("flush", "FileRspResponse", function(con) {
88
  # To please R CMD check.
89 0
  this <- con
90

91 0
  out <- getOutput(this)
92 0
  flush(out)
93
}, appendVarArgs=FALSE)
94

95

96

97
#########################################################################/**
98
# @RdocMethod getPath
99
#
100
# @title "Gets the path of the directory of the current RSP file"
101
#
102
# \description{
103
#  @get "title".
104
# }
105
#
106
# @synopsis
107
#
108
# \arguments{
109
#   \item{...}{Not used.}
110
# }
111
#
112
# \value{
113
#  Returns a @character string.
114
# }
115
#
116
# @author
117
#
118
# \seealso{
119
#   @seeclass
120
# }
121
#
122
# @keyword IO
123
#*/#########################################################################
124
setMethodS3("getPath", "FileRspResponse", function(this, ...) {
125 0
  getParent(this$file)
126
}, createGeneric=FALSE)
127

128

129

130
#########################################################################/**
131
# @RdocMethod getName
132
#
133
# @title "Gets the (base)name of the current RSP file"
134
#
135
# \description{
136
#  @get "title".
137
# }
138
#
139
# @synopsis
140
#
141
# \arguments{
142
#   \item{...}{Not used.}
143
# }
144
#
145
# \value{
146
#  Returns a @character string.
147
# }
148
#
149
# @author
150
#
151
# \seealso{
152
#   @seeclass
153
# }
154
#
155
# @keyword IO
156
#*/#########################################################################
157
setMethodS3("getName", "FileRspResponse", function(this, ...) {
158 0
  basename(this$file)
159
}, createGeneric=FALSE)
160

161

162

163
#########################################################################/**
164
# @RdocMethod getAbsolutePath
165
#
166
# @title "Gets the absolute pathname to the current RSP file"
167
#
168
# \description{
169
#  @get "title".
170
# }
171
#
172
# @synopsis
173
#
174
# \arguments{
175
#   \item{...}{Not used.}
176
# }
177
#
178
# \value{
179
#  Returns a @character string.
180
# }
181
#
182
# @author
183
#
184
# \seealso{
185
#   @seeclass
186
# }
187
#
188
# @keyword IO
189
#*/#########################################################################
190
setMethodS3("getAbsolutePath", "FileRspResponse", function(this, ...) {
191 0
  getAbsolutePath(this$file)
192
}, createGeneric=FALSE)

Read our documentation on viewing source code .

Loading