Regular commit
[backups/.git] / filedata.cpp
1 #include <string>
2 #include <vector>
3 #include <iostream>
4
5 #include "filedata.hpp"
6
7 using namespace std;
8
9 FileData::FileData( char               _type,
10                     string             _permissions,
11                     string             _user,
12                     string             _group,
13                     unsigned long long _size,
14                     unsigned long long _modified_date,
15                     unsigned long long _last_backup,
16                     string             _name )
17 : filetype( _type ),
18   permissions( _permissions ),
19   username( _user ),
20   groupname( _group ),
21   filesize( _size ),
22   modified_date( _modified_date ),
23   last_backup_date( _last_backup ),
24   filename( _name )
25 {}
26
27 vector<string> split( const string &line, char c, int limit = -1 ) {
28   string::size_type start = 0, end = 0;
29
30   vector<string> out;
31   while( 0 != limit-- && end != line.size() ) {
32     if( 0 == limit ) {
33       end = line.size();
34     } else {
35       end = line.find( c, start );
36       if( end == string::npos ) {
37         end = line.size();
38       }
39     }
40     out.push_back( line.substr( start, end-start ) );
41     start = end + 1;
42   }
43   return out;
44 }
45
46 ostream &operator<<( const FileData &d, ostream &o ) {
47   o << d.getFileType()       << ' ';
48   o << d.getPermissions()    << ' ';
49   o << d.getUserName()       << ' ';
50   o << d.getGroupName()      << ' ';
51   o << d.getFileSize()       << ' ';
52   o << d.getModifiedDate()   << ' ';
53   o << d.getLastBackupDate() << ' ';
54   o << d.getFileName()       << '\0';
55
56   return o;
57 }
58
59 istream &operator>>( istream &i, FileData &d ) {
60   string file_string;
61
62   for( int c = i.get(); 0 != c && ! i.eof(); c = i.get() ) {
63     file_string.push_back( c );
64   }
65
66   if( 0 != file_string.size() ) {
67     // Example entry
68     // type perms user group size datemodified name (8 total)
69     // f 0600 cnb cnb 424 20051015205340 0 ./.git/index
70     vector<string> vals = split( file_string, ' ', 8 );
71     d.setFileType(       vals[0][0] );
72     d.setPermissions(    vals[1] );
73     d.setUserName(       vals[2] );
74     d.setGroupName(      vals[3] );
75     d.setFileSize(       atoi( vals[4].c_str() ) );
76     d.setModifiedDate(   atoi( vals[5].c_str() ) );
77     d.setLastBackupDate( atoi( vals[6].c_str() ) );
78     d.setFileName(       vals[7] );
79   }
80
81   return i;
82 }