feisty meow concerns codebase  2.140
filename_list.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : filename_list *
4 * Author : Chris Koeritz *
5 * *
6 *******************************************************************************
7 * Copyright (c) 2005-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
14 
15 #include "filename_list.h"
16 
18 #include <textual/parser_bits.h>
19 
20 using namespace basis;
21 using namespace structures;
22 using namespace textual;
23 
24 namespace filesystem {
25 
26 filename_list::filename_list() : amorph<file_info>() {}
27 
29 {
30  if (this == &to_copy) return *this;
31  reset();
32  for (int i = 0; i < to_copy.elements(); i++) {
33  append(new file_info(*to_copy.get(i)));
34  }
35  return *this;
36 }
37 
38 int filename_list::total_files() const { return elements(); }
39 
41 { return amorph_packed_size(*this); }
42 
43 void filename_list::pack(byte_array &packed_form) const
44 { amorph_pack(packed_form, *this); }
45 
47 { return amorph_unpack(packed_form, *this); }
48 
50 {
51  double to_return = 0;
52  for (int i = 0; i < elements(); i++)
53  to_return += get(i)->_file_size;
54  return to_return;
55 }
56 
58  double current_offset, int &current_file, double &current_size)
59 {
60  current_file = 0;
61  current_size = 0;
62  int posn = locate(file);
63  if (negative(posn)) {
64  if (file.raw().t()) return false; // not a member.
65  // they're at the start of the run.
66  current_file = 1;
67  return true;
68  }
69  current_file = posn + 1; // position zero in array means file number 1.
70  double size_finished = 0;
71  // iterate on all files before the current one.
72  for (int i = 0; i < posn; i++) {
73  size_finished += get(i)->_file_size;
74  }
75  current_size = size_finished + current_offset;
76  return true;
77 }
78 
80 {
81  reset();
82  for (int i = 0; i < to_copy.length(); i++) {
83  append(new file_info(to_copy[i], 0));
84  }
85  return *this;
86 }
87 
89 {
90  to_fill.reset();
91  for (int i = 0; i < elements(); i++) {
92  to_fill += get(i)->raw();
93  }
94 }
95 
96 const file_info *filename_list::find(const filename &to_check) const
97 {
98  for (int i = 0; i < elements(); i++) {
99 #if defined(__WIN32__) || defined(__VMS__)
100  if (to_check.raw().iequals(get(i)->raw())) return get(i);
101 #else
102  if (to_check.raw() == get(i)->raw()) return get(i);
103 #endif
104  }
105  return NULL_POINTER;
106 }
107 
108 int filename_list::locate(const filename &to_find) const
109 {
110  for (int i = 0; i < elements(); i++) {
111 #if defined(__WIN32__) || defined(__VMS__)
112  if (to_find.raw().iequals(get(i)->raw())) return i;
113 #else
114  if (to_find.raw() == get(i)->raw()) return i;
115 #endif
116  }
117  return common::NOT_FOUND;
118 }
119 
120 bool filename_list::member(const filename &to_check) const
121 {
122  for (int i = 0; i < elements(); i++) {
123 #if defined(__WIN32__) || defined(__VMS__)
124  if (to_check.raw().iequals(get(i)->raw())) return true;
125 #else
126  if (to_check.raw() == get(i)->raw()) return true;
127 #endif
128  }
129  return false;
130 }
131 
133 {
134  for (int i = 0; i < elements(); i++) {
135 #if defined(__WIN32__) || defined(__VMS__)
136  if (to_check.raw().iequals(get(i)->raw())) {
137 #else
138  if (to_check.raw() == get(i)->raw()) {
139 #endif
140  // once we have matched a name, the other checks will cause us to
141  // reject any other potential matches after this one if the requested
142  // check fails.
143  if ((how & file_info::EQUAL_FILESIZE) && (to_check._file_size != get(i)->_file_size) )
144  return false;
145  if ((how & file_info::EQUAL_TIMESTAMP) && (to_check._time != get(i)->_time) )
146  return false;
147  if ((how & file_info::EQUAL_CHECKSUM) && (to_check._checksum != get(i)->_checksum) )
148  return false;
149  return true;
150  }
151  }
152  return false;
153 }
154 
155 astring filename_list::text_form(int max_lines) const
156 {
157  astring to_return;
158  for (int i = 0; i < elements(); i++) {
159  to_return += a_sprintf("%d. ", i + 1);
160  if (get(i))
161  to_return += get(i)->text_form();
162  if (i != elements() - 1)
163  to_return += parser_bits::platform_eol_to_chars();
164  }
165  return to_return;
166 }
167 
168 } //namespace.
169 
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition: array.h:349
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
bool t() const
t() is a shortcut for the string being "true", as in non-empty.
Definition: astring.h:97
bool iequals(const astring &that) const
returns true if this is case-insensitively equal to "that".
Definition: astring.cpp:565
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
Encapsulates some measures and calculations based on a file's contents.
Definition: file_info.h:29
double _file_size
the size of the file.
Definition: file_info.h:40
int _checksum
the checksum for the file.
Definition: file_info.h:42
file_time _time
the file's access time.
Definition: file_info.h:41
file_similarity
this enum encapsulates how files may be compared.
Definition: file_info.h:32
bool member(const filename &to_check) const
finds the index for "to_find" or returns a negative number.
bool member_with_state(const file_info &to_check, file_info::file_similarity comparison_method)
returns true if the file "to_check" exists in the list with appropriate equivalence.
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
basis::astring text_form(int max_lines=MAXINT32) const
max_lines is the maximum number of lines to print into the string.
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
int total_files() const
returns the number of files currently held in the list.
const file_info * find(const filename &to_check) const
locates the record of information for the filename "to_check".
double total_size() const
returns the full size of all files listed.
filename_list & operator=(const filename_list &to_copy)
int locate(const filename &to_find) const
bool calculate_progress(const filename &file, double current_offset, int &current_file, double &current_size)
given the last "file" and position, this returns current positioning.
void fill(structures::string_array &to_fill)
stuffs the array "to_fill" with the filesnames from our list.
virtual int packed_size() const
Estimates the space needed for the packed structure.
Provides operations commonly needed on file names.
Definition: filename.h:64
const basis::astring & raw() const
returns the astring that we're holding onto for the path.
Definition: filename.cpp:97
int elements() const
the maximum number of elements currently allowed in this amorph.
Definition: amorph.h:66
basis::outcome append(const file_info *data)
puts "data" on the end of this amorph.
Definition: amorph.h:303
void reset()
cleans out all of the contents.
Definition: amorph.h:81
const contents * get(int field) const
Returns a constant pointer to the information at the index "field".
Definition: amorph.h:312
An array of strings with some additional helpful methods.
Definition: string_array.h:32
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
bool negative(const type &a)
negative returns true if "a" is less than zero.
Definition: functions.h:43
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
void amorph_pack(basis::byte_array &packed_form, const amorph< contents > &to_pack)
support for packing an amorph into an array of bytes.
Definition: amorph.h:479
int amorph_packed_size(const amorph< contents > &to_pack)
reports how large the packed form will be.
Definition: amorph.h:468
bool amorph_unpack(basis::byte_array &packed_form, amorph< contents > &to_unpack)
unpacks the amorph from an array of bytes.
Definition: amorph.h:489