Class: RPM::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
ext/rpm/version.c

Instance Method Summary collapse

Constructor Details

#new(vr, e = nil) ⇒ Version #new(v, r, e = nil) ⇒ Version

Examples:

RPM:: Version.new "1.0.0-3"
RPM:: Version.new "1.04"
RPM:: Version.new "1.0.0-3k", 1
RPM:: Version.new "2.0.3", "5k"

Overloads:

  • #new(vr, e = nil) ⇒ Version

    Creates a version object from a string representation

    Parameters:

    • vr (String)

      version and release in the form “v-r”

    • e (Number) (defaults to: nil)

      epoch

  • #new(v, r, e = nil) ⇒ Version

    Creates a version object from a string representation

    Parameters:

    • v (String)

      version

    • r (String)

      release

    • e (Number) (defaults to: nil)

      epoch



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'ext/rpm/version.c', line 104

static VALUE
version_initialize(int argc, VALUE* argv, VALUE ver)
{
	VALUE v = Qnil;
	VALUE r = Qnil;
	VALUE e = Qnil;

	switch (argc) {
	case 0:
		rb_raise(rb_eArgError, "argument too few(1..3)");

	case 1:
		if (TYPE(argv[0]) != T_STRING) {
			rb_raise(rb_eTypeError, "illegal argument type");
		}
		version_parse(RSTRING_PTR(argv[0]), &v, &r, &e);
		break;

	case 2:
		if (TYPE(argv[0]) != T_STRING) {
			rb_raise(rb_eTypeError, "illegal argument type");
		}
		version_parse(RSTRING_PTR(argv[0]), &v, &r, &e);
		if (e != Qnil) {
			rb_raise(rb_eTypeError, "illegal argument value");
		}
		e = rb_Integer(argv[1]);
		break;

	case 3:
		if (TYPE(argv[0]) != T_STRING
			|| TYPE(argv[1]) != T_STRING) {
			rb_raise(rb_eTypeError, "illegal argument type");
		}
		v = rb_str_new2(RSTRING_PTR(argv[0]));
		r = rb_str_new2(RSTRING_PTR(argv[1]));
		e = rb_Integer(argv[2]);
		break;

	default:
		rb_raise(rb_eArgError, "argument too many(1..3)");
	}

	if (NIL_P(v) || (!NIL_P(e) && NUM2INT(e) < 0)) {
		rb_raise(rb_eArgError, "illegal argument value");
	}

	rb_ivar_set(ver, id_v, v);
	rb_ivar_set(ver, id_r, r);
	rb_ivar_set(ver, id_e, e);

	return ver;
}

Instance Method Details

#<=>(other) ⇒ Number

Comparison between versions

Examples:

v1 = RPM::Version.new("3.0-0",1)
v2 = RPM::Version.new("3.1-0",1)
v1 <=> v2
=> -1

Parameters:

Returns:

  • (Number)

    -1 if other is greater than, 0 if other is equal to, and +1 if other is less than version.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'ext/rpm/version.c', line 217

VALUE
rpm_version_cmp(VALUE ver, VALUE other)
{
	VALUE ve,oe;
	VALUE vr,or;
	VALUE vv,ov;
	int sense = 0;

	if (rb_obj_is_kind_of(other, rpm_cVersion) != Qtrue) {
		rb_raise(rb_eTypeError, "illegal argument type");
	}

	ve = rb_ivar_get(ver,id_e);
	oe = rb_ivar_get(other,id_e);
	if (!NIL_P(ve) && NUM2INT(ve) > 0 && NIL_P(oe))
		return INT2FIX(1);
	else if (NIL_P(ve) && !NIL_P(oe) && NUM2INT(oe) > 0)
		return INT2FIX(-1);
	else if (!NIL_P(ve) && !NIL_P(oe)) {
		if (NUM2INT(ve) < NUM2INT(oe))
			return INT2FIX(-1);
		else if (NUM2INT(ve) > NUM2INT(oe))
			return INT2FIX(1);
	}

	vv = rb_ivar_get(ver,id_v);
	ov = rb_ivar_get(other,id_v);
	if (!NIL_P(vv) && NIL_P(ov))	/* XXX */
		return INT2FIX(1);
	else if (NIL_P(vv) && !NIL_P(ov))	/* XXX */
		return INT2FIX(-1);
	else if (!NIL_P(vv) && !NIL_P(ov)) {
		sense = rpmvercmp(RSTRING_PTR(vv), RSTRING_PTR(ov));
		if (sense)
			return INT2FIX(sense);
	}

	vr = rb_ivar_get(ver,id_r);
	or = rb_ivar_get(other,id_r);
	if (!NIL_P(vr) && NIL_P(or))
		return INT2FIX(1);
	else if (NIL_P(vr) && !NIL_P(or))
		return INT2FIX(-1);
	else if (!NIL_P(vr) && !NIL_P(or)) {
		sense = rpmvercmp(RSTRING_PTR(vr), RSTRING_PTR(or));
	}
	return INT2FIX(sense);
}

#eNumber

Access the epoch component

Returns:

  • (Number)

    epoch component or nil if the version does not have an epoch component



312
313
314
315
316
# File 'ext/rpm/version.c', line 312

VALUE
rpm_version_get_e(VALUE ver)
{
	return rb_ivar_get(ver, id_e);
}

#hashString

Hash based on the version content

Returns:

  • (String)


392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'ext/rpm/version.c', line 392

VALUE
rpm_version_hash(VALUE ver)
{
    long h;
    VALUE v, r, e;
	v = rb_ivar_get(ver, id_v);
	r = rb_ivar_get(ver, id_r);
	e = rb_ivar_get(ver, id_e);

    h = NIL_P(e) ? 0 : NUM2INT(e);
    h = (h << 1) ^ NUM2LONG(rb_hash(r));
    h = (h << 1) ^ NUM2LONG(rb_hash(v));
    return LONG2FIX(h);
}

#inspectString

Inspect the version object

Returns:

  • (String)

    object in the form “#<RPM::Version v=V, r=R, e=E>”



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'ext/rpm/version.c', line 369

VALUE
rpm_version_inspect(VALUE ver)
{
	char buf[BUFSIZ];
	VALUE v, r, e;
	v = rb_ivar_get(ver, id_v);
	r = rb_ivar_get(ver, id_r);
	e = rb_ivar_get(ver, id_e);

	if (!NIL_P(e)) {
		snprintf(buf, BUFSIZ, "#<RPM::Version v=%s, r=%s, e=%ld>", RSTRING_PTR(rb_inspect(v)), RSTRING_PTR(rb_inspect(r)), (long) NUM2INT(e));
        } else {
		snprintf(buf, BUFSIZ, "#<RPM::Version v=%s, r=%s>", RSTRING_PTR(rb_inspect(v)), RSTRING_PTR(rb_inspect(r)));
	}

	return rb_str_new2(buf);
}

#newer?(other) ⇒ Boolean

Returns true if the version is newer than other.

Parameters:

  • other (Version)

    Version to compare against

Returns:

  • (Boolean)

    true if the version is newer than other



270
271
272
273
274
# File 'ext/rpm/version.c', line 270

VALUE
rpm_version_is_newer(VALUE ver, VALUE other)
{
	return (NUM2INT(rpm_version_cmp(ver, other)) > 0) ? Qtrue : Qfalse;
}

#older?(other) ⇒ Boolean

Returns true if the version is older than other.

Parameters:

  • other (Version)

    Version to compare against

Returns:

  • (Boolean)

    true if the version is older than other



280
281
282
283
284
# File 'ext/rpm/version.c', line 280

VALUE
rpm_version_is_older(VALUE ver, VALUE other)
{
	return rpm_version_is_newer(ver, other) ? Qfalse : Qtrue;
}

#rString

Access the release component

Returns:

  • (String)

    release component or nil if the version does not have a release component



301
302
303
304
305
# File 'ext/rpm/version.c', line 301

VALUE
rpm_version_get_r(VALUE ver)
{
	return rb_ivar_get(ver, id_r);
}

#to_sString

Note:

The epoch is not included

String representation in the form “v-r”

Returns:

  • (String)


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'ext/rpm/version.c', line 323

VALUE
rpm_version_to_s(VALUE ver)
{
	char buf[BUFSIZ];
	char *p = buf;
	VALUE v, r;
	v = rb_ivar_get(ver, id_v);
	r = rb_ivar_get(ver, id_r);
	strcpy(p, RSTRING_PTR(v));
	if (!NIL_P(r)) {
		strcat(p, "-");
		strcat(p, RSTRING_PTR(r));
	}
	return rb_str_new2(buf);
}

#to_vreString

Note:

The epoch is included

String representation in the form “e:v-r”

Returns:

  • (String)


344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'ext/rpm/version.c', line 344

VALUE
rpm_version_to_vre(VALUE ver)
{
	char buf[BUFSIZ];
	char *p = buf;
	VALUE v, r, e;
	v = rb_ivar_get(ver, id_v);
	r = rb_ivar_get(ver, id_r);
	e = rb_ivar_get(ver, id_e);
	if (!NIL_P(e)) {
		snprintf(buf,BUFSIZ,"%ld:", (long) NUM2INT(e));
		p += strlen(buf);
        }
	strcpy(p, RSTRING_PTR(v));
	if (!NIL_P(r)) {
		strcat(p, "-");
		strcat(p, RSTRING_PTR(r));
	}
	return rb_str_new2(buf);
}

#vString

Access the version component

Returns:

  • (String)

    version component



290
291
292
293
294
# File 'ext/rpm/version.c', line 290

VALUE
rpm_version_get_v(VALUE ver)
{
	return rb_ivar_get(ver, id_v);
}