source: trunk/third/rep-gtk/rep-types.c @ 18404

Revision 18404, 10.1 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18403, which included commits to RCS files with non-trunk default branches.
RevLine 
[15285]1/* Copyright (C) 1997, 1998, 1999 Marius Vollmer
2 * Copyright (C) 1999-2000 John Harper <john@dcs.warwick.ac.uk>
3 *
[18403]4 * $Id: rep-types.c,v 1.1.1.2 2003-01-05 00:30:07 ghudson Exp $
[15285]5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this software; see the file COPYING.  If not, write to
18 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <config.h>
22#include <assert.h>
23#include <glib.h>
24#include "rep-gtk.h"
25#include <string.h>
26#include <limits.h>
27
28
29
30/* Hacking the basic types --jsh */
31
32static inline int
33valid_int_type (repv obj)
34{
35    return rep_INTEGERP (obj) || rep_LONG_INTP (obj);
36}
37
38int
39sgtk_valid_int (repv obj)
40{
41    return valid_int_type (obj);
42}
43
44int
45sgtk_valid_uint (repv obj)
46{
47    return valid_int_type (obj);
48}
49
50int
51sgtk_valid_long (repv obj)
52{
53    return valid_int_type (obj);
54}
55
56int
57sgtk_valid_ulong (repv obj)
58{
59    return valid_int_type (obj);
60}
61
62int
63sgtk_valid_char (repv obj)
64{
65    return rep_INTP (obj);
66}
67
68repv
69sgtk_uint_to_rep (u_long x)
70{
71    return rep_make_long_uint (x);
72}
73
74repv
75sgtk_int_to_rep (long x)
76{
77    return rep_make_long_int (x);
78}
79
80repv
81sgtk_long_to_rep (long x)
82{
83    return rep_make_long_int (x);
84}
85
86repv
87sgtk_ulong_to_rep (u_long x)
88{
89    return rep_make_long_uint (x);
90}
91
92guint
93sgtk_rep_to_uint (repv obj)
94{
95    return rep_get_long_uint (obj);
96}
97
98gint
99sgtk_rep_to_int (repv obj)
100{
101    return rep_get_long_int (obj);
102}
103
104gulong
105sgtk_rep_to_ulong (repv obj)
106{
107    return rep_get_long_uint (obj);
108}
109
110glong
111sgtk_rep_to_long (repv obj)
112{
113    return rep_get_long_int (obj);
114}
115
116gchar
117sgtk_rep_to_char (repv obj)
118{
119    return rep_INT (obj);
120}
121
122repv
123sgtk_char_to_rep (gchar c)
124{
125    return rep_MAKE_INT (c);
126}
127
128char *
129sgtk_rep_to_string (repv obj)
130{
131    return rep_STRINGP (obj) ? rep_STR (obj) : (u_char *)"";
132}
133
134repv
135sgtk_string_to_rep (char *x)
136{
137    repv obj;
138    if (x == 0)
139        return Qnil;
140    obj = rep_string_dup (x);
141    g_free (x);
142    return obj;
143}
144
145repv
146sgtk_static_string_to_rep (const char *x)
147{
148    repv obj;
149    if (x == 0)
150        return Qnil;
151    obj = rep_string_dup (x);
152    return obj;
153}
154
155int
156sgtk_valid_string (repv obj)
157{
158    return rep_STRINGP (obj);
159}
160
161repv
162sgtk_bool_to_rep (int x)
163{
164    return x ? Qt : Qnil;
165}
166
167int
168sgtk_rep_to_bool (repv obj)
169{
170    return obj != Qnil;
171}
172
173int
174sgtk_valid_function (repv obj)
175{
176    return Ffunctionp (obj) != Qnil;
177}
178
179int
180sgtk_valid_fd (repv obj)
181{
182    return rep_FILEP (obj) && rep_LOCAL_FILE_P (obj);
183}
184
185int
186sgtk_rep_to_fd (repv obj)
187{
188    return fileno (rep_FILE(obj)->file.fh);
189}
190
191repv
192sgtk_fd_to_rep (int fd)
193{
194    if (fd < 0)
195        return Qnil;
196    else
197        return rep_file_fdopen (fd, "w+");
198}
199
200int
201sgtk_valid_pointer (repv obj)
202{
203    return obj == Qnil || rep_INTEGERP (obj) || rep_LONG_INTP (obj);
204}
205
206void *
207sgtk_rep_to_pointer (repv obj)
208{
209    if (obj == Qnil)
210        return NULL;
211    else
212        return (void *) rep_get_long_uint (obj);
213}
214
215repv
216sgtk_pointer_to_rep (void *ptr)
217{
218    repv data = rep_VAL (ptr);
219    if (data == 0)
220        return Qnil;
221    else if (data > rep_LISP_MAX_INT)
222        /* could use a bignum, but cons is more efficient */
223        return rep_MAKE_LONG_INT (data);
224    else
225        return rep_MAKE_INT (data);
226}
227
228static int
229list_length (repv list)
230{
231    repv len = Flength (list);
232    return (len && rep_INTP (len)) ? rep_INT (len) : 0;
233}
234
[18403]235/* namespace fuckage. needed so we can represent GObject base class */
236GType
237gobject_get_type (void)
238{
239  return G_TYPE_OBJECT;
240}
241
[15285]242
243/* Floats. */
244
245int
246sgtk_valid_float (repv obj)
247{
248  return rep_NUMERICP (obj);
249}
250
251gfloat
252sgtk_rep_to_float (repv obj)
253{
254  return rep_get_float (obj);
255}
256
257repv
258sgtk_float_to_rep (gfloat f)
259{
260  return rep_make_float (f, rep_FALSE);
261}
262
263int
264sgtk_valid_double (repv obj)
265{
266  return rep_NUMERICP (obj);
267}
268
269double
270sgtk_rep_to_double (repv obj)
271{
272  return rep_get_float (obj);
273}
274
275repv
276sgtk_double_to_rep (double f)
277{
278  return rep_make_float (f, rep_FALSE);
279}
280
281
282
283/* Composites. */
284
285int
286sgtk_valid_composite (repv obj, int (*predicate)(repv))
287{
288  return sgtk_valid_complen (obj, predicate, -1);
289}
290
291int
292sgtk_valid_complen (repv obj, int (*predicate)(repv), int len)
293{
294  int actual_len;
295
296  if (rep_LISTP(obj))
297    {
298      actual_len = list_length (obj);
299
300      if (len >= 0 && len != actual_len)
301        return 0;
302
303      if (predicate)
304        {
305          while (rep_CONSP(obj))
306            {
307              if (!predicate (rep_CAR(obj)))
308                return 0;
309              obj = rep_CDR(obj);
310            }
311        }
312      return 1;
313    }
314  else if (rep_VECTORP(obj))
315    {
316      int i;
317      repv *elts;
318
319      actual_len = rep_VECT_LEN (obj);
320      if (len >= 0 && len != actual_len)
321        return 0;
322
323      if (predicate)
324        {
325          elts = rep_VECT(obj)->array;
326          for (i = 0; i < len; i++)
327            if (!predicate(elts[i]))
328              return 0;
329        }
330      return 1;
331    }
332  else
333    return 0;
334}
335
336repv
337sgtk_slist_to_rep (GSList *list, repv (*toscm)(void*))
338{
339  repv res, *tail = &res;
340  while (list)
341    {
342      *tail = Fcons (toscm (&list->data), *tail);
343      tail = rep_CDRLOC (*tail);
344      list = list->next;
345    }
346  *tail = Qnil;
347  return res;
348}
349
350GSList*
351sgtk_rep_to_slist (repv obj, void (*fromscm)(repv, void*))
352{
353  GSList *res, **tail = &res;
354
355  if (obj == Qnil || (rep_CONSP(obj)))
356    {
357      while (rep_CONSP(obj))
358        {
359          *tail = g_slist_alloc ();
360          if (fromscm)
361            fromscm (rep_CAR (obj), &(*tail)->data);
362          else
363            (*tail)->data = NULL;
364          obj = rep_CDR(obj);
365          tail = &(*tail)->next;
366        }
367    }
368  else if (rep_VECTORP(obj))
369    {
370      int len = rep_VECT_LEN (obj), i;
371      repv *elts = rep_VECT(obj)->array;
372      for (i = 0; i < len; i++)
373        {
374          *tail = g_slist_alloc ();
375          if (fromscm)
376            fromscm (elts[i], &(*tail)->data);
377          else
378            (*tail)->data = NULL;
379          tail = &(*tail)->next;
380        }
381    }
382  *tail = NULL;
383  return res;
384}
385
386void
387sgtk_slist_finish (GSList *list, repv obj, repv (*toscm)(void*))
388{
389  if (toscm)
390    {
391      if (obj == Qnil || (rep_CONSP(obj)))
392        {
393          while (rep_CONSP(obj) && list)
394            {
395              rep_CAR(obj) = toscm (list->data);
396              obj = rep_CDR(obj);
397              list = list->next;
398            }
399        }
400      else if (rep_VECTORP(obj))
401        {
402          int len = rep_VECT_LEN (obj), i;
403          repv *elts = rep_VECT(obj)->array;
404          for (i = 0; i < len && list; i++)
405            {
406              elts[i] = toscm (list->data);
407              list = list->next;
408            }
409        }
410    }
411
412  g_slist_free (list);
413}
414
415repv
416sgtk_list_to_rep (GList *list, repv (*toscm)(void*))
417{
418  repv res, *tail = &res;
419  while (list)
420    {
421      *tail = Fcons (toscm (&list->data), *tail);
422      tail = rep_CDRLOC (*tail);
423      list = list->next;
424    }
425  *tail = Qnil;
426  return res;
427}
428
429GList*
430sgtk_rep_to_list (repv obj, void (*fromscm)(repv, void*))
431{
432  GList *res = NULL, *tail;
433 
434  if (obj == Qnil || (rep_CONSP(obj)))
435    {
436      while (rep_CONSP(obj))
437      {
438        GList *n = g_list_alloc ();
439        if (res == NULL)
440          res = tail = n;
441        else
442          {
443            g_list_concat (tail, n);
444            tail = n;
445          }
446        if (fromscm)
447          fromscm (rep_CAR (obj), &(n->data));
448        else
449          n->data = NULL;
450        obj = rep_CDR(obj);
451      }
452    }
453  else if (rep_VECTORP(obj))
454    {
455      int len = rep_VECT_LEN (obj), i;
456      repv *elts = rep_VECT(obj)->array;
457      for (i = 0; i < len; i++)
458        {
459          GList *n = g_list_alloc ();
460          if (res == NULL)
461            res = tail = n;
462          else
463            {
464              g_list_concat (tail, n);
465              tail = n;
466            }
467          if (fromscm)
468            fromscm (elts[i], &(n->data));
469          else
470            n->data = NULL;
471        }
472    }
473  return res;
474}
475
476void
477sgtk_list_finish (GList *list, repv obj, repv (*toscm)(void*))
478{
479  if (toscm)
480    {
481      if (obj == Qnil || (rep_CONSP(obj)))
482        {
483          while (rep_CONSP(obj) && list)
484            {
485              rep_CAR (obj) = toscm (list->data);
486              obj = rep_CDR(obj);
487              list = list->next;
488            }
489        }
490      else if (rep_VECTORP(obj))
491        {
492          int len = rep_VECT_LEN (obj), i;
493          repv *elts = rep_VECT(obj)->array;
494          for (i = 0; i < len && list; i++)
495            {
496              elts[i] = toscm (list->data);
497              list = list->next;
498            }
499        }
500    }
501 
502  g_list_free (list);
503}
504
505sgtk_cvec
506sgtk_rep_to_cvec (repv obj, void (*fromscm)(repv, void*), size_t sz)
507{
508  sgtk_cvec res;
509  int i;
510  char *ptr;
511
512  if (rep_LISTP(obj))
513    {
514      res.count = list_length (obj);
515      res.vec = rep_alloc ((res.count + 1) * sz);
516      if (fromscm)
517        {
518          for (i = 0, ptr = res.vec; i < res.count; i++, ptr += sz)
519            {
520              fromscm (rep_CAR (obj), ptr);
521              obj = rep_CDR(obj);
522            }
523        }
524      else
525        memset (res.vec, 0, res.count * sz);
526    }
527  else if (rep_VECTORP(obj))
528    {
529      repv *elts = rep_VECT(obj)->array;
530      res.count = rep_VECT_LEN (obj);
531      res.vec = rep_alloc ((res.count + 1) * sz);
532      if (fromscm)
533        {
534          for (i = 0, ptr = res.vec; i < res.count; i++, ptr += sz)
535            fromscm (elts[i], ptr);
536        }
537      else
538        memset (res.vec, 0, res.count * sz);
539    }
540  /* make all vectors zero terminated, makes `tvec' easier to implement */
541  memset (((char *)res.vec) + res.count * sz, 0, sz);
542  return res;
543}
544
545void
546sgtk_cvec_finish (sgtk_cvec *cvec, repv obj, repv (*toscm)(void *), size_t sz)
547{
548  if (toscm)
549    {
550      if (obj == Qnil || (rep_CONSP(obj)))
551        {
552          int i, len = cvec->count;
553          char *ptr;
554
555          for (i = 0, ptr = cvec->vec;
556               i < len && rep_CONSP(obj);
557               i++, ptr += sz, obj = rep_CDR (obj))
558            {
559              rep_CAR (obj) = toscm (ptr);
560            }
561        }
562      else if (rep_VECTORP(obj))
563        {
564          repv *elts = rep_VECT(obj)->array;
565          int len1 = rep_VECT_LEN (obj), len2 = cvec->count, i;
566          char *ptr;
567
568          for (i = 0, ptr = cvec->vec; i < len1 && i < len2; i++, ptr += sz)
569            elts[i] = toscm (ptr);
570        }
571    }
572
573  rep_free (cvec->vec);
574}
575
576repv
577sgtk_cvec_to_rep (sgtk_cvec *cvec, repv (*toscm)(void *), size_t sz)
578{
579    int len = cvec->count, i;
580    repv obj = Fmake_vector (rep_MAKE_INT(len), Qnil);
581    repv *elts = rep_VECT (obj)->array;
582    char *ptr;
583
584    for (i = 0, ptr = cvec->vec; i < len; i++, ptr += sz)
585        elts[i] = toscm (ptr);
586
587    g_free (cvec->vec);
588    return obj;
589}
590
591
592/* dl hooks */
593
594repv
595rep_dl_init (void)
596{
[18403]597    repv tem = rep_push_structure ("gui.gtk-2.types");
[15285]598    return rep_pop_structure (tem);
599}
Note: See TracBrowser for help on using the repository browser.