1 //
2 // ====================================================================
3 // Copyright (c) 2003-2009 Barry A Scott.  All rights reserved.
4 //
5 // This software is licensed as described in the file LICENSE.txt,
6 // which you should have received as part of this distribution.
7 //
8 // ====================================================================
9 //
10 //
11 //  pysvn_revision.cpp
12 //
13 
14 #if defined( _MSC_VER )
15 // disable warning C4786: symbol greater than 255 character,
16 // nessesary to ignore as <map> causes lots of warning
17 #pragma warning(disable: 4786)
18 #endif
19 
20 #include "pysvn.hpp"
21 #include "pysvn_docs.hpp"
22 
23 
24 //--------------------------------------------------------------------------------
25 
toAprTime(double t_in)26 static apr_time_t toAprTime( double t_in )
27 {
28     return apr_time_t( t_in * 1000000 );
29 }
30 
pysvn_revision(svn_opt_revision_kind kind,double date,int revnum)31 pysvn_revision::pysvn_revision
32     (
33     svn_opt_revision_kind kind,
34     double date,
35     int revnum
36     )
37 {
38     memset( &m_svn_revision, 0, sizeof( m_svn_revision ) );
39 
40     m_svn_revision.kind = kind;
41     if( kind == svn_opt_revision_date )
42         m_svn_revision.value.date = toAprTime( date );
43 
44     if( kind == svn_opt_revision_number )
45         m_svn_revision.value.number = revnum;
46 }
47 
~pysvn_revision()48 pysvn_revision::~pysvn_revision()
49 {
50 }
51 
getSvnRevision() const52 const svn_opt_revision_t &pysvn_revision::getSvnRevision() const
53 {
54     return m_svn_revision;
55 }
56 
repr()57 Py::Object pysvn_revision::repr()
58 {
59     std::string s("<Revision kind=");
60     s += toString( m_svn_revision.kind );
61 
62     switch( m_svn_revision.kind )
63     {
64         case svn_opt_revision_number:
65             {
66             char buf[80];
67             snprintf( buf, sizeof(buf), " %d", int(m_svn_revision.value.number) );
68             s += buf;
69             }
70             break;
71 
72         case svn_opt_revision_date:
73             {
74             char buf[300];
75             snprintf( buf, sizeof(buf), " %f", double( m_svn_revision.value.date )/1000000 );
76             s += buf;
77             }
78             break;
79 
80         default:
81             break;
82     }
83     s += ">";
84 
85     return Py::String( s );
86 }
87 
getattr(const char * _name)88 Py::Object pysvn_revision::getattr( const char *_name )
89 {
90     std::string name( _name );
91 
92     if( name == "__members__" )
93     {
94         Py::List members;
95 
96         members.append( Py::String( "kind" ) );
97         members.append( Py::String( "date" ) );
98         members.append( Py::String( "number" ) );
99 
100         return members;
101     }
102 
103     else if( name == "kind" )
104     {
105         return Py::asObject(
106             new pysvn_enum_value<svn_opt_revision_kind>( m_svn_revision.kind ) );
107     }
108     else if( name == "date" )
109     {
110         if( m_svn_revision.kind == svn_opt_revision_date )
111             return Py::Float( double( m_svn_revision.value.date )/1000000 );
112         else
113             return Py::None();
114     }
115     else if( name == "number" )
116     {
117         if( m_svn_revision.kind == svn_opt_revision_number )
118             return Py::Int( m_svn_revision.value.number );
119         else
120             return Py::None();
121     }
122     return getattr_methods( _name );
123 }
124 
setattr(const char * _name,const Py::Object & value)125 int pysvn_revision::setattr( const char *_name, const Py::Object &value )
126 {
127     std::string name( _name );
128     if( name == "kind" )
129     {
130         Py::ExtensionObject< pysvn_enum_value<svn_opt_revision_kind> > kind( value );
131 
132         m_svn_revision.kind = svn_opt_revision_kind( kind.extensionObject()->m_value );
133     }
134     else if( name == "date" )
135     {
136         Py::Float py_date( value );
137         m_svn_revision.value.date = toAprTime( double( py_date ) );
138     }
139     else if( name == "number" )
140     {
141         Py::Int revnum( value );
142         m_svn_revision.value.number = revnum;
143     }
144     else
145         throw Py::AttributeError( "Unknown revision attribute" );
146 
147     return 0;
148 }
149 
init_type()150 void pysvn_revision::init_type()
151 {
152     behaviors().name("revision");
153     behaviors().doc( pysvn_revision_doc );
154     behaviors().supportGetattr();
155     behaviors().supportRepr();
156 }
157