Author: marvin
Date: Sun May 13 04:35:04 2012
New Revision: 1337775
URL: http://svn.apache.org/viewvc?rev=1337775&view=rev
Log:
Add CFCVersion.
Added:
lucy/trunk/clownfish/perl/t/052-version.t (with props)
lucy/trunk/clownfish/src/CFCVersion.c (with props)
lucy/trunk/clownfish/src/CFCVersion.h (with props)
Modified:
lucy/trunk/clownfish/include/CFC.h
lucy/trunk/clownfish/perl/lib/Clownfish/CFC.pm
lucy/trunk/clownfish/perl/lib/Clownfish/CFC.xs
lucy/trunk/clownfish/perl/typemap
Modified: lucy/trunk/clownfish/include/CFC.h
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/include/CFC.h?rev=1337775&r1=1337774&r2=1337775&view=diff
==============================================================================
--- lucy/trunk/clownfish/include/CFC.h (original)
+++ lucy/trunk/clownfish/include/CFC.h Sun May 13 04:35:04 2012
@@ -32,6 +32,7 @@
#include "CFCType.h"
#include "CFCUtil.h"
#include "CFCVariable.h"
+#include "CFCVersion.h"
#include "CFCBindCore.h"
#include "CFCBindAliases.h"
Modified: lucy/trunk/clownfish/perl/lib/Clownfish/CFC.pm
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/perl/lib/Clownfish/CFC.pm?rev=1337775&r1=1337774&r2=1337775&view=diff
==============================================================================
--- lucy/trunk/clownfish/perl/lib/Clownfish/CFC.pm (original)
+++ lucy/trunk/clownfish/perl/lib/Clownfish/CFC.pm Sun May 13 04:35:04 2012
@@ -615,6 +615,24 @@ BEGIN { XSLoader::load( 'Clownfish::CFC'
}
{
+ package Clownfish::CFC::Model::Version;
+ BEGIN { push our @ISA, 'Clownfish::CFC::Base' }
+ use Clownfish::CFC::Util qw( verify_args );
+ use Carp;
+
+ our %new_PARAMS = (
+ vstring => undef,
+ );
+
+ sub new {
+ my ( $either, %args ) = @_;
+ confess "no subclassing allowed" unless $either eq __PACKAGE__;
+ verify_args( \%new_PARAMS, %args ) or confess $@;
+ return _new( $args{vstring} );
+ }
+}
+
+{
package Clownfish::CFC::Binding::Core;
BEGIN { push our @ISA, 'Clownfish::CFC::Base' }
use Clownfish::CFC::Util qw( verify_args );
Modified: lucy/trunk/clownfish/perl/lib/Clownfish/CFC.xs
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/perl/lib/Clownfish/CFC.xs?rev=1337775&r1=1337774&r2=1337775&view=diff
==============================================================================
--- lucy/trunk/clownfish/perl/lib/Clownfish/CFC.xs (original)
+++ lucy/trunk/clownfish/perl/lib/Clownfish/CFC.xs Sun May 13 04:35:04 2012
@@ -1723,6 +1723,45 @@ PPCODE:
END_SET_OR_GET_SWITCH
}
+MODULE = Clownfish::CFC PACKAGE = Clownfish::CFC::Model::Version
+
+SV*
+_new(vstring)
+ const char *vstring;
+CODE:
+ CFCVersion *self = CFCVersion_new(vstring);
+ RETVAL = S_cfcbase_to_perlref(self);
+ CFCBase_decref((CFCBase*)self);
+OUTPUT: RETVAL
+
+void
+_set_or_get(self, ...)
+ CFCVersion *self;
+ALIAS:
+ get_major = 2
+ get_vstring = 4
+PPCODE:
+{
+ START_SET_OR_GET_SWITCH
+ case 2:
+ retval = newSVuv(CFCVersion_get_major(self));
+ break;
+ case 4: {
+ const char *value = CFCVersion_get_vstring(self);
+ retval = newSVpvn(value, strlen(value));
+ }
+ break;
+ END_SET_OR_GET_SWITCH
+}
+
+int
+compare_to(self, other)
+ CFCVersion *self;
+ CFCVersion *other;
+CODE:
+ RETVAL = CFCVersion_compare_to(self, other);
+OUTPUT: RETVAL
+
MODULE = Clownfish::CFC PACKAGE = Clownfish::CFC::Binding::Core
SV*
Added: lucy/trunk/clownfish/perl/t/052-version.t
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/perl/t/052-version.t?rev=1337775&view=auto
==============================================================================
--- lucy/trunk/clownfish/perl/t/052-version.t (added)
+++ lucy/trunk/clownfish/perl/t/052-version.t Sun May 13 04:35:04 2012
@@ -0,0 +1,38 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+use strict;
+use warnings;
+
+use Test::More tests => 9;
+use Clownfish::CFC;
+
+my $v3_2_1 = Clownfish::CFC::Model::Version->new( vstring => 'v3.2.1' );
+my $v3_3 = Clownfish::CFC::Model::Version->new( vstring => 'v3.3' );
+my $v3_2_0 = Clownfish::CFC::Model::Version->new( vstring => 'v3.2.0' );
+my $v3_2_1_0 = Clownfish::CFC::Model::Version->new( vstring => 'v3.2.1.0' );
+my $v90210 = Clownfish::CFC::Model::Version->new( vstring => 'v90210' );
+
+is( $v3_2_1->get_major, 3, 'get_major' );
+is( $v90210->get_major, 90210, 'parse big number' );
+is( $v3_2_1->get_vstring, 'v3.2.1', 'get_vstring' );
+
+is( $v3_2_1->compare_to($v3_2_1_0), 0, "ignore zeroes in compare_to" );
+is( $v3_2_1_0->compare_to($v3_2_1), 0, "ignore zeroes in compare_to" );
+is( $v3_2_1->compare_to($v3_3), -1, "compare_to A < B_fewer_digits" );
+is( $v3_3->compare_to($v3_2_1), 1, "compare_to A_fewer_digits > B" );
+is( $v3_2_1->compare_to($v3_2_0), 1, "compare_to A > B" );
+is( $v3_2_0->compare_to($v3_2_1), -1, "compare_to A < B" );
+
Propchange: lucy/trunk/clownfish/perl/t/052-version.t
------------------------------------------------------------------------------
svn:eol-style = native
Modified: lucy/trunk/clownfish/perl/typemap
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/perl/typemap?rev=1337775&r1=1337774&r2=1337775&view=diff
==============================================================================
--- lucy/trunk/clownfish/perl/typemap (original)
+++ lucy/trunk/clownfish/perl/typemap Sun May 13 04:35:04 2012
@@ -31,6 +31,7 @@ CFCParser* CLOWNFISH_TYPE
CFCSymbol* CLOWNFISH_MODEL
CFCType* CLOWNFISH_MODEL
CFCVariable* CLOWNFISH_MODEL
+CFCVersion* CLOWNFISH_MODEL
CFCBindCore* CLOWNFISH_BINDING_CORE
CFCBindClass* CLOWNFISH_BINDING_CORE_TYPE
CFCPerl* CLOWNFISH_BINDING_PERL
Added: lucy/trunk/clownfish/src/CFCVersion.c
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCVersion.c?rev=1337775&view=auto
==============================================================================
--- lucy/trunk/clownfish/src/CFCVersion.c (added)
+++ lucy/trunk/clownfish/src/CFCVersion.c Sun May 13 04:35:04 2012
@@ -0,0 +1,120 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+#include <ctype.h>
+
+#ifndef true
+ #define true 1
+ #define false 0
+#endif
+
+#define CFC_NEED_BASE_STRUCT_DEF
+#include "CFCBase.h"
+#include "CFCVersion.h"
+#include "CFCUtil.h"
+
+struct CFCVersion {
+ CFCBase base;
+ uint32_t *numbers;
+ size_t num_numbers;
+ char *vstring;
+};
+
+const static CFCMeta CFCVERSION_META = {
+ "Clownfish::CFC::Model::Version",
+ sizeof(CFCVersion),
+ (CFCBase_destroy_t)CFCVersion_destroy
+};
+
+CFCVersion*
+CFCVersion_new(const char *vstring) {
+ CFCVersion *self = (CFCVersion*)CFCBase_allocate(&CFCVERSION_META);
+ return CFCVersion_init(self, vstring);
+}
+
+CFCVersion*
+CFCVersion_init(CFCVersion *self, const char *vstring) {
+ CFCUTIL_NULL_CHECK(vstring);
+ if (*vstring != 'v' || !isdigit(vstring[1])) {
+ CFCBase_decref((CFCBase*)self);
+ CFCUtil_die("Bad version string: '%s'", vstring);
+ }
+ self->vstring = CFCUtil_strdup(vstring);
+ vstring++;
+ uint32_t num = 0;
+ self->num_numbers = 0;
+ self->numbers = (uint32_t*)CALLOCATE(1, sizeof(uint32_t));
+ while (1) {
+ if (!isdigit(*vstring)) {
+ if (*vstring != 0 && *vstring != '.') {
+ CFCBase_decref((CFCBase*)self);
+ CFCUtil_die("Bad version string: '%s'", self->vstring);
+ }
+ size_t size = (self->num_numbers + 1) * sizeof(uint32_t);
+ self->numbers = (uint32_t*)REALLOCATE(self->numbers, size);
+ self->numbers[self->num_numbers++] = num;
+ if (*vstring == 0) {
+ break;
+ }
+ num = 0;
+ }
+ num = num * 10 + *vstring - '0';
+ vstring++;
+ }
+
+ return self;
+}
+
+void
+CFCVersion_destroy(CFCVersion *self) {
+ FREEMEM(self->numbers);
+ FREEMEM(self->vstring);
+ CFCBase_destroy((CFCBase*)self);
+}
+
+int
+CFCVersion_compare_to(CFCVersion *self, CFCVersion *other) {
+ for (size_t i = 0;
+ i < self->num_numbers && i < other->num_numbers;
+ i++
+ ) {
+ int32_t my_number = i >= self->num_numbers
+ ? 0
+ : self->numbers[i];
+ int32_t other_number = i >= other->num_numbers
+ ? 0
+ : other->numbers[i];
+ if (my_number > other_number) {
+ return 1;
+ }
+ else if (other_number > my_number) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+uint32_t
+CFCVersion_get_major(CFCVersion *self) {
+ return self->numbers[0];
+}
+
+const char*
+CFCVersion_get_vstring(CFCVersion *self) {
+ return self->vstring;
+}
+
Propchange: lucy/trunk/clownfish/src/CFCVersion.c
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucy/trunk/clownfish/src/CFCVersion.h
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCVersion.h?rev=1337775&view=auto
==============================================================================
--- lucy/trunk/clownfish/src/CFCVersion.h (added)
+++ lucy/trunk/clownfish/src/CFCVersion.h Sun May 13 04:35:04 2012
@@ -0,0 +1,62 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+/** Clownfish::CFC::Model::Version - Version number.
+ *
+ * A version number, comprised of one or more unsigned integers. Digits
+ * beyond the first are treated as implicit zeros for the purposes of
+ * comparision, so that v1.2 and v1.2.0 are equivalent.
+ */
+#ifndef H_CFCVERSION
+#define H_CFCVERSION
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+
+typedef struct CFCVersion CFCVersion;
+
+/**
+ * @param vstring - A version string consisting of a lower-case 'v' followed
+ * by one or more unsigned integers separated by dots.
+*/
+CFCVersion*
+CFCVersion_new(const char *vstring);
+
+CFCVersion*
+CFCVersion_init(CFCVersion *self, const char *vstring);
+
+void
+CFCVersion_destroy(CFCVersion *self);
+
+int
+CFCVersion_compare_to(CFCVersion *self, CFCVersion *other);
+
+const char*
+CFCVersion_get_vstring(CFCVersion *self);
+
+uint32_t
+CFCVersion_get_major(CFCVersion *self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_CFCVERSION */
+
Propchange: lucy/trunk/clownfish/src/CFCVersion.h
------------------------------------------------------------------------------
svn:eol-style = native