Class: Pacct::Log
- Inherits:
-
Object
- Object
- Pacct::Log
- Defined in:
- ext/pacct/pacct_c.c
Class Method Summary collapse
-
.new(filename) ⇒ Object
Creates a new Pacct::Log using the given accounting file.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the log file.
-
#each_entry([start]) {|entry, index| ... } ⇒ Object
Yields each entry in the file to the given block.
- #initialize(filename, mode) ⇒ Object constructor
-
#last_entry ⇒ Object
Returns the last entry in the file.
-
#num_entries ⇒ Object
Returns the number of entries in the file.
-
#write_entry(entry) ⇒ Object
Appends the given entry to the file.
Constructor Details
#initialize(filename, mode) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'ext/pacct/pacct_c.c', line 152
static VALUE pacct_log_init(VALUE self, VALUE filename, VALUE mode) {
PacctLog* log;
FILE* acct;
long length;
char* c_filename = StringValueCStr(filename);
size_t c_filename_len;
const char* c_mode = "rb";
if(mode != Qnil) {
int isValidMode = 0;
size_t i;
c_mode = StringValueCStr(mode);
for(i = 0; i < sizeof(validFileModes) / sizeof(char*); ++i) {
if(strcmp(c_mode, validFileModes[i]) == 0) {
isValidMode = 1;
break;
}
}
if(!isValidMode) {
rb_raise(rb_eArgError, "Invalid mode for Pacct::File: '%s'", c_mode);
}
}
acct = fopen(c_filename, c_mode);
if(!acct) {
rb_raise(rb_eIOError, "Unable to open file '%s'", c_filename);
}
Data_Get_Struct(self, PacctLog, log);
log->file = acct;
c_filename_len = strlen(c_filename);
log->filename = malloc(c_filename_len + 1);
ENSURE_ALLOCATED(log->filename);
strncpy(log->filename, c_filename, c_filename_len);
log->filename[c_filename_len] = '\0';
CHECK_CALL(fseek(acct, 0, SEEK_END), 0);
length = ftell(acct);
rewind(acct);
if(length % sizeof(struct acct_v3) != 0) {
rb_raise(rb_eIOError, "Accounting file '%s' appears to be the wrong size.", c_filename);
}
log->num_entries = length / sizeof(struct acct_v3);
return self;
}
|
Class Method Details
.new(filename) ⇒ Object
Creates a new Pacct::Log using the given accounting file
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'ext/pacct/pacct_c.c', line 135
static VALUE pacct_log_new(int argc, VALUE* argv, VALUE class) {
VALUE log;
VALUE init_args[2];
PacctLog* ptr;
init_args[1] = Qnil;
rb_scan_args(argc, argv, "11", init_args, init_args + 1);
log = Data_Make_Struct(class, PacctLog, 0, pacct_log_free, ptr);
ptr->file = NULL;
ptr->num_entries = 0;
rb_obj_call_init(log, 2, init_args);
return log;
}
|
Instance Method Details
#close ⇒ Object
Closes the log file
211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'ext/pacct/pacct_c.c', line 211
static VALUE pacct_log_close(VALUE self) {
PacctLog* log;
Data_Get_Struct(self, PacctLog, log);
if(log->file) {
fclose(log->file);
log->file = NULL;
}
return Qnil;
}
|
#each_entry([start]) {|entry, index| ... } ⇒ Object
Yields each entry in the file to the given block
If start is given, iteration starts at the entry with that index.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'ext/pacct/pacct_c.c', line 254
static VALUE each_entry(int argc, VALUE* argv, VALUE self) {
PacctLog* log;
VALUE start_value;
long start = 0;
int i = 0;
rb_scan_args(argc, argv, "01", &start_value);
if(argc && start_value != Qnil) {
start = NUM2UINT(start_value);
}
Data_Get_Struct(self, PacctLog, log);
pacct_log_check_closed(log);
if(start > log->num_entries) {
rb_raise(rb_eRangeError, "Index %li is out of range", start);
}
CHECK_CALL(fseek(log->file, start * sizeof(struct acct_v3), SEEK_SET), 0);
for(i = start; i < log->num_entries; ++i) {
VALUE entry = pacct_entry_new(log);
rb_yield(entry);
}
return Qnil;
}
|
#last_entry ⇒ Object
Returns the last entry in the file
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'ext/pacct/pacct_c.c', line 286
static VALUE last_entry(VALUE self) {
PacctLog* log;
long pos;
VALUE entry;
Data_Get_Struct(self, PacctLog, log);
pacct_log_check_closed(log);
if(log->num_entries == 0) {
return Qnil;
}
pos = ftell(log->file);
CHECK_CALL(fseek(log->file, -sizeof(struct acct_v3), SEEK_END), 0);
entry = pacct_entry_new(log);
CHECK_CALL(fseek(log->file, pos, SEEK_SET), 0);
return entry;
}
|
#num_entries ⇒ Object
Returns the number of entries in the file
312 313 314 315 316 317 318 |
# File 'ext/pacct/pacct_c.c', line 312
static VALUE get_num_entries(VALUE self) {
PacctLog* log;
Data_Get_Struct(self, PacctLog, log);
return INT2NUM(log->num_entries);
}
|
#write_entry(entry) ⇒ Object
Appends the given entry to the file
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'ext/pacct/pacct_c.c', line 326
static VALUE write_entry(VALUE self, VALUE entry) {
//To do consider: verification?
PacctLog* log;
long pos;
struct acct_v3* acct;
Data_Get_Struct(self, PacctLog, log);
pacct_log_check_closed(log);
Data_Get_Struct(entry, struct acct_v3, acct);
pos = ftell(log->file);
CHECK_CALL(fseek(log->file, 0, SEEK_END), 0);
if(fwrite(acct, sizeof(struct acct_v3), 1, log->file) != 1) {
rb_raise(rb_eIOError, "Unable to write to accounting file '%s'", log->filename);
}
++(log->num_entries);
CHECK_CALL(fseek(log->file, pos, SEEK_SET), 0);
return Qnil;
}
|