File: fd_file.c

package info (click to toggle)
libforms 1.0.93sp1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,548 kB
  • ctags: 9,107
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (896 lines) | stat: -rw-r--r-- 22,601 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/*
 * This file is part of XForms.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file fd_file.c
 *
*  This file is part of XForms package
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *
 * This file is part of the Form Designer.
 *
 * It contains the routines to save and load forms in the internal
 * format used by the form designer. This is readable ASCII.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "include/forms.h"
#include "flinternal.h"
#include "fd_main.h"
#include "fd_spec.h"

#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* Writes an obj description to a file. Note externally the coordinate
 * system always starts from lower-left corner of the screen */

#if 0
#define OBJ_Y(ob)   ob->form->h - ob->y - ob->h
#else
#define OBJ_Y(ob)   obj->y
#endif


/***************************************
 ***************************************/

static void
save_object( FILE      * fl,
             FL_OBJECT * obj )
{
    char name[ MAX_VAR_LEN ],
         cbname[ MAX_VAR_LEN ],
         argname[ MAX_VAR_LEN ];
    char *label;
    double sc = get_conversion_factor( );
    FL_OBJECT *defobj,
              fake_obj;

    if ( obj->parent )
        return;

    defobj = find_class_default( obj->objclass, obj->type );

    get_object_name( obj, name, cbname, argname );

    fprintf( fl, "\n--------------------\n" );
    fprintf( fl, "class: %s\n", class_name( obj->objclass ) );
    fprintf( fl, "type: %s\n", find_type_name( obj->objclass, obj->type ) );

    fake_obj.x = obj->x;
    fake_obj.y = obj->y;
    fake_obj.w = obj->w;
    fake_obj.h = obj->h;
    fl_scale_object( &fake_obj, sc, sc );
    fprintf( fl, "box: %d %d %d %d\n", fake_obj.x, fake_obj.y,
             fake_obj.w, fake_obj.h );

    fprintf( fl, "boxtype: %s\n", boxtype_name( obj->boxtype ) );
    fprintf( fl, "colors: %s %s\n", fli_query_colorname( obj->col1 ),
             fli_query_colorname( obj->col2 ) );
    fprintf( fl, "alignment: %s\n", align_name( obj->align, 0 ) );
    fprintf( fl, "style: %s\n", style_name( obj->lstyle ) );
    fprintf( fl, "size: %s\n", lsize_name( obj->lsize ) );
    fprintf( fl, "lcol: %s\n", fli_query_colorname( obj->lcol ) );
    label = get_label( obj, 0 );
    fprintf( fl, "label: %s\n", label );
    fl_free( label );
    fprintf( fl, "shortcut: %s\n", get_shortcut_string( obj ) );
    fprintf( fl, "resize: %s\n", resize_name( obj->resize ) );
    fprintf( fl, "gravity: %s %s\n",
             gravity_name( obj->nwgravity ),
             gravity_name( obj->segravity ) );
    fprintf( fl, "name: %s\n", name );
    fprintf( fl, "callback: %s\n", cbname );
    fprintf( fl, "argument: %s\n", argname );
    if ( ! defobj || obj->how_return != defobj->how_return )
        fprintf( fl, "return: %s\n",
                 get_how_return_name( obj->how_return, 0 ) );

    save_objclass_spec_info( fl, obj );
}


/*
 * X version changed color systemtically, need to do a translation
 * from old fd files on the fly
 */

typedef struct {
    int oldval,
        newval;
} Trantable;

static Trantable tcolor[ ] =
{
    {  0, FL_BLACK },
    {  1, FL_RED },
    {  2, FL_GREEN },
    {  3, FL_YELLOW },
    {  4, FL_BLUE },
    {  5, FL_MAGENTA },
    {  6, FL_CYAN },
    {  7, FL_WHITE },
    {  8, FL_BOTTOM_BCOL }, /* approx */

    {  9, FL_INDIANRED },
    { 10, FL_PALEGREEN },
    { 11, FL_PALEGREEN },
    { 12, FL_SLATEBLUE },

    { 35, FL_RIGHT_BCOL },
    { 36, FL_RIGHT_BCOL },  /* approx */
    { 37, FL_RIGHT_BCOL },  /* approx  */
    { 40, FL_BOTTOM_BCOL },
    { 47, FL_COL1 },
    { 49, FL_MCOL },
    { 51, FL_TOP_BCOL },
    { 55, FL_LEFT_BCOL }
};

static Trantable tclass[ ] =
{
    {     1, FL_BOX },
    {     2, FL_TEXT },
    {     3, FL_BITMAP },
    {     4, FL_CHART },
    {    11, FL_BUTTON },
    {    12, FL_LIGHTBUTTON },
    {    13, FL_ROUNDBUTTON },
    {    21, FL_SLIDER },
    {    22, FL_DIAL },
    {    23, FL_POSITIONER },
    {    24, FL_VALSLIDER },
    {    25, FL_COUNTER },
    {    31, FL_INPUT },
    {    41, FL_MENU },
    {    42, FL_CHOICE },
    {    61, FL_CLOCK },
    {    62, FL_TIMER },
    {    71, FL_BROWSER },
    {   101, FL_FREE },
    { 10000, FL_BEGIN_GROUP },
    { 20000, FL_END_GROUP }
};

static Trantable talign[] =
{
    { 0, FL_ALIGN_TOP },
    { 1, FL_ALIGN_BOTTOM },
    { 2, FL_ALIGN_LEFT },
    { 3, FL_ALIGN_RIGHT },
    { 4, FL_ALIGN_CENTER }
};

static Trantable tbtype[ ] =
{
    { 0, FL_NO_BOX },
    { 1, FL_UP_BOX },
    { 2, FL_DOWN_BOX },
    { 3, FL_FLAT_BOX },
    { 4, FL_BORDER_BOX },
    { 5, FL_SHADOW_BOX },
    { 6, FL_FRAME_BOX },
    { 7, FL_ROUNDED_BOX },
    { 8, FL_RFLAT_BOX },
    { 9, FL_RSHADOW_BOX }
};


/***************************************
 ***************************************/

static int
do_trans( Trantable * tab,
          int         n,
          int         old )
{
    Trantable *p = tab, *q;

    for ( q = p + n; p < q; p++ )
        if ( p->oldval == old )
            return p->newval;
    return old;
}


#define new_class( o ) do_trans( tclass, sizeof tclass / sizeof *tclass, ( o ) )
#define new_color( o ) do_trans( tcolor, sizeof tcolor / sizeof *tcolor, ( o ) )
#define new_align( o ) do_trans( talign, sizeof talign / sizeof *talign, ( o ) )
#define new_btype( o ) do_trans( tbtype, sizeof tbtype / sizeof *tbtype, ( o ) )


/***************************************
 ***************************************/

static int
ff_read_boxtype( FL_OBJECT * obj )
{
    int r;

    if ( ( r = ff_read( "%b", &obj->boxtype ) ) < 0 )
        return ff_err( "Can't read expected object boxtype" );

    if ( r == 0 )
        return ff_err( "\"boxtype\" key without value" );

    if ( fd_magic == MAGIC2 )
        obj->boxtype = new_btype( obj->boxtype );

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_colors( FL_OBJECT * obj )
{
    int r;

    if ( ( r = ff_read( "%c%c", &obj->col1, &obj->col2 ) ) < 0 )
        return ff_err( "Can't read expected object colors" );

    if ( r != 2 )
        return ff_err( "\"colors\" key without two object colors" );

    if ( fd_magic == MAGIC2 )
    {
        obj->col1 = new_color( obj->col1 );
        obj->col2 = new_color( obj->col2 );
    }

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_alignment( FL_OBJECT * obj )
{
    int r;

    if ( ( r = ff_read( "%a", &obj->align ) ) < 0 )
        return ff_err( "Can't read expected object alignment" );

    if ( r == 0 )
        return ff_err( "\"alignment\" key without or invalid value" );

    if ( fd_magic == MAGIC2 )
        obj->align = new_align( obj->align );

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_lstyle( FL_OBJECT * obj )
{
    int r;

    if ( ( r = ff_read( "%p", &obj->lstyle ) ) < 0 )
        return ff_err( "Can't read expected object label style" );

    if ( r == 0 )
        return ff_err( "\"style\" key without or invalid value" );

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_lsize( FL_OBJECT * obj )
{
    int r;

    if ( ( r = ff_read( "%q", &obj->lsize ) ) < 0 )
        return ff_err( "Can't read expected object label size" );

    if ( r == 0 )
        return ff_err( "\"size\" key without or invalid value" );

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_lcol( FL_OBJECT * obj )
{
    int r;

    if ( ( r = ff_read( "%c", &obj->lcol ) ) < 0 )
        return ff_err( "Can't read expected object label color" );

    if ( r == 0 )
        return ff_err( "\"lcol\" key without or invalid value" );

    if ( fd_magic == MAGIC2 )
        obj->lcol = new_color( obj->lcol );

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_resize( FL_OBJECT * obj )
{
    int r;

    if ( ( r = ff_read( "%r", &obj->resize ) ) < 0 )
        return ff_err( "Can't read expected object resize value" );

    if ( r == 0 )
        return ff_err( "\"resize\" key without or invalid value" );

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_label( FL_OBJECT * obj )
{
    int r;
    char *label;

    if ( ( r = ff_read( "%S", &label ) ) < 0 )
        return ff_err( "Can't read expected object label" );

    if ( r == 1 )
    {
        set_label( obj, label );
        fl_safe_free( label );
    }

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_shortcut( FL_OBJECT * obj )
{
    int r;
    char *shortcut;

    if ( ( r = ff_read( "%s", &shortcut ) ) < 0 )
        return ff_err( "Can't read expected object shortcut" );

    if ( r == 1 )
    {
        set_shortcut( obj, shortcut );
        fl_safe_free( shortcut );
    }

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_callback( FL_OBJECT * obj )
{
    int r;
    char *cbname;

    if ( ( r = ff_read( "%v", &cbname ) ) < 0 )
        return ff_err( "Can't read expected object callback" );

    if ( r == 1 )
    {
        set_object_name( obj, NULL, cbname, NULL );
        fl_safe_free( cbname );
    }

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_name( FL_OBJECT * obj )
{
    int r;
    char *name;

    if ( ( r = ff_read( "%s", &name ) ) < 0 )
        return ff_err( "Can't read expected object name" );

    if ( r == 1 )
    {
        set_object_name( obj, name, NULL, NULL );
        fl_safe_free( name );
    }

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_gravity( FL_OBJECT * obj )
{
    int r;

    if ( ( r = ff_read( "%g%g", &obj->nwgravity, &obj->segravity ) ) < 0 )
        return ff_err( "Can't read expected object gravity values" );

    if ( r != 2 )
    {
        if ( r == 0 )
            ff_err( "\"gravity\" key without valid values" );
        if ( r == 1 )
            ff_err( "\"gravity\" key with only one valid value" );
        return FF_READ_FAILURE;
    }

    return 0;
}


/***************************************
 ***************************************/

static int
ff_read_argument( FL_OBJECT * obj )

{
    int r;
    char *arg;

    if ( ( r = ff_read( "%s", &arg ) ) < 0 )
        return ff_err( "Can't read expected object callback argument" );

    if ( r == 1 )
    {
        set_object_name( obj, NULL, NULL, arg );
        fl_safe_free( arg );
    }

    return 0;
}


/***************************************
 ***************************************/

static int 
ff_read_return( FL_OBJECT * obj )
{
    int r;
    char *return_name;
    int ret;

    if ( ( r = ff_read( "%s", &return_name ) ) < 0 )
        return ff_err( "Can't read expected object return" );

    if ( r == 0 )
        return ff_err( "\"return\" key with no or invalid value" );

    ret = get_how_return_val( return_name );

    fl_safe_free( return_name );

    if ( ret == -1 )
        return ff_err( "Invalid value for \"return\" key" );

    fl_set_object_return( obj, ret );

    return 0;
}


/***************************************
 ***************************************/

typedef int ( * obj_attr_func )( FL_OBJECT * );

typedef struct {
    const char    * name;
    obj_attr_func   func;
} obj_attr_handlers;


static obj_attr_handlers attr_array[ ] =
{
    { "boxtype",   ff_read_boxtype   },
    { "colors",    ff_read_colors    },
    { "alignment", ff_read_alignment },
    { "style",     ff_read_lstyle    },
    { "lstyle",    ff_read_lstyle    },
    { "size",      ff_read_lsize     },
    { "lsize",     ff_read_lsize     },
    { "lcol",      ff_read_lcol      },
    { "resize",    ff_read_resize    },
    { "label",     ff_read_label     },
    { "shortcut",  ff_read_shortcut  },
    { "callback",  ff_read_callback  },
    { "name",      ff_read_name      },
    { "gravity",   ff_read_gravity   },
    { "argument",  ff_read_argument  },
    { "return",    ff_read_return    }
};


/***************************************
 * Reads all information from a .fd file until it's
 * detected that there's either a new form or object
 * or the last line of the file has been reached.
 * When called the first key for an object, "class:"
 * already has been read from the file.
 ***************************************/

static int
load_object( void )
{
    int objclass;
    char *type_name;
    int type;
    FL_Coord x, y, w, h;
    char *key, *p;
    FL_OBJECT *obj;
    int r;
        
    /* Start with trying to read the minmal information needed. First we
       want the object class */

    if ( ! ff_read( "%o", &objclass ) < 0 )
        return ff_err( "Expected object class" );

    if ( fd_magic == MAGIC2 )
        objclass = new_class( objclass );

    /* Next we need the object type. normally this is a string except for
       pseudo-objects that start and ed a group */

    if ( ! ( objclass == FL_BEGIN_GROUP || objclass == FL_END_GROUP ) )
    {
        if (    ff_read( "%k%t", &key, &type_name ) < 2
             || strcmp( key, "type" ) )
        {
            fl_safe_free( key );
            return ff_err( "Expected object type" );
        }

        fl_safe_free( key );

        if ( ( type = find_type_value( objclass, type_name ) ) < 0 )
        {
            char *tmp = fli_print_to_string( "Invalid type \"%s\" for object "
                                             "class", type_name );
            fl_safe_free( type_name );
            ff_err( tmp );
            fl_safe_free( type_name );
            return FF_READ_FAILURE;
        }

        fl_free( type_name );
    }
    else
    {
        if (    ff_read( "%k%d", &key, &type ) < 2
             || strcmp( key, "type" ) )
        {
            fl_safe_free( key );
            return ff_err( "Expected object type" );
        }
    }

    fl_safe_free( key );

    /* As the third thing the size of the object (following the "box" key) is
       required, exceptions are the pseudo-objectst that start and end a group,
       they may have less values */

    if ( ! ( objclass == FL_BEGIN_GROUP || objclass == FL_END_GROUP ) )
    {
        if (    ( r = ff_read( "%k%D%D%U%U", &key, &x, &y, &w, &h ) ) < 5
             || strcmp( key, "box" ) )
        {
            fl_safe_free( key );

            if ( r == 0 )
                ff_err( "Expected object box size" );
            else
            {
                char *msg = fli_print_to_string( "Expected object box sizes "
                                                 "as 4 values, found %d valid "
                                                 "ones", r - 1 );
                ff_err( msg );
                fl_safe_free( msg );
            }
            
            return FF_READ_FAILURE;
        }
    }
    else
    {
        /* For start and end of group we get either 1 or 4 values */

        r = ff_read( "%k%D%d%U%U", &key, &x, &y, &w, &h );
        if ( ( r != 2 && r != 5 ) || strcmp( key, "box" ) )
        {
            fl_safe_free( key );
            return ff_err( "Expected object box size with 1 or 4 valid"
                           "values" );
        }
    }

    fl_free( key );

    if ( cur_form && fd_magic < MAGIC4 )
        y = cur_form->h - y - h;

    /* Create the new object */

    if ( ! ( obj = add_an_object( objclass, type, x, y, w, h ) ) )
        return ff_err( "Failed to create an object" );

    /* Now read the optional attributes, order is irrelevant except that
       all general object attributes must come before those specific to
       the type of the object and here we only look for the general ones */

    while ( 1 )
    {
        size_t i;

        if ( ( r = ff_read( "%k", &key ) ) < 0 )
            return ff_err( "Failed to read expected key" );

        if ( r == 0 || ! strcmp( key, "Name" ) || ! strcmp( key, "class" ) )
            break;

        for ( i = 0; i < sizeof attr_array / sizeof *attr_array; i++ )
            if ( ! strcmp( key, attr_array[ i ].name ) )
            {
                fl_safe_free( key );
                if ( attr_array[ i ].func( obj ) == FF_READ_FAILURE )
                    return FF_READ_FAILURE;
                break;
            }

        if ( i == sizeof attr_array / sizeof *attr_array )
            break;
    }

    /* Some extra adjustments for spinner objects (this is a hack but
       avoiding it would require a complete change of how fdesign works) */

    if ( obj->objclass == FL_SPINNER )
    {
        FL_OBJECT *subobj = fl_get_spinner_input( obj );

        subobj->col1 = obj->col1;
        subobj->col2 = obj->col2;

        subobj->lstyle = obj->lstyle;
        subobj->lsize  = obj->lsize;
    }

    /* If the key read isn't "Name" or "class" (indicating the start of the
       next form or object) it must be an object specific key */

    if ( r == 1 && strcmp( key, "Name" ) && strcmp( key, "class" ) )
       r = load_objclass_spec_info( obj, key );
    else
    {
        /* If a key could be read continue, otherwise this must be (nearly)
           the end of the file and the name of the main function must be
           readable (or something is wrong) */

        if ( r == 1 )
            r = ! strcmp( key, "class" ) ?
                FF_AT_START_OF_OBJECT : FF_AT_START_OF_FORM;
        else
        {
            if ( ff_read( "%v", &p ) < 1 )
                return ff_err( "Expected main function name, not found here" );

            fli_sstrcpy( main_name, p, MAX_VAR_LEN );
            fl_safe_free( p );

            r = FF_AT_END_OF_FILE;
        }

        fl_safe_free( key );
    }

    fli_handle_object( obj, FL_ATTRIB, 0, 0, 0, NULL, 0 );

    return r;
}


/***************************************
 * Saves a form definition to the file
 ***************************************/

void
write_form( FILE    * fl,
            FL_FORM * form,
            char      fname[ ] )
{
    int onumb;
    FL_OBJECT *obj;

    fprintf( fl, "\n=============== FORM ===============\n" );
    fprintf( fl, "Name: %s\n", fname );
    fprintf( fl, "Width: %d\n", convert_u( form->w ) );
    fprintf( fl, "Height: %d\n", convert_u( form->h ) );

    /* print the object number */

    for ( onumb = 0, obj = form->first->next; obj; obj = obj->next )
        onumb += obj->parent == NULL;

    fprintf( fl, "Number of Objects: %d\n", onumb );

    /* print the objects */

    obj = form->first->next;
    while ( obj != NULL )
    {
        save_object( fl, obj );
        obj = obj->next;
    }
}


/***************************************
 * Reads all information about a form from an .fd file until
 * it's detected that a new form starts or the last line of
 * the file has been reached. When we arrive here the "Name:"
 * key as well as the name of the form itself already have been
 * read in. We return when we detect that a new form starts
 * (due to the "Mame:" key or the last line of the file should
 * have been reached.
 ***************************************/

int
read_form( void )
{
    char *key;
    float w = -1.0,
          h = -1.0;
    int num_objects = -1;
    int r;
    int i;

    /* Try to read information pertinent to the form (name already has been
       read in) until the first key for an object in the form ("class:") is
       found */

    while ( 1 )
    {
        if ( ff_read( "%k", &key ) < 1 )
            return ff_err( "Invalid format of file" );

        if ( ! strcmp( key, "class" ) )
        {
            fl_safe_free( key );
            break;
        }
        else if ( ! strcmp( key, "Width" ) )
        {
            fl_safe_free( key );

            if ( ff_read( "%f", &w ) < 1 )
                return ff_err( "Can't read expected form width" );

            if ( w < 0.0 )
                return ff_err( "Invalid negative form width" );
        }
        else if ( ! strcmp( key, "Height" ) )
        {
            fl_safe_free( key );

            if ( ff_read( "%f", &h ) < 1 )
                return ff_err( "Can't read expected form height" );

            if ( h < 0.0 )
                return ff_err( "Invalid negative form height" );
        }
        else if ( ! strcmp( key, "Number of Objects" ) )
        {
            fl_safe_free( key );

            if ( ff_read( "%d", &num_objects ) < 1 )
                return ff_err( "Can't read expected number of objects in "
                               "form" );

            if ( num_objects <= 0 )
                return ff_err( "Invalid number of objects, must be at "
                               "least 1" );
        }
    }

    /* Before continuing with reading the form's objects check that at
       least width, height and number of objects in form were found */

    if ( w < 0.0 )
        return ff_err( "Width of form not found" );

    if ( h < 0.0 )
        return ff_err( "Height of form not found" );

    if ( num_objects < 0 )
        return ff_err( "Number of objects in form not found" );

    /* Create the new form */

    cur_form = fl_bgn_form( FL_NO_BOX, ( FL_Coord ) w, ( FL_Coord ) h );
    fl_end_form( );

    /* Read all objects until next form starts or end file seems to
       have been reached */

    r = FF_AT_START_OF_OBJECT;

    for ( i = 0; i < num_objects && r == FF_AT_START_OF_OBJECT; i++ )
        r = load_object( );

    if ( r == FF_READ_FAILURE )
        return FF_READ_FAILURE;

    if ( r == FF_AT_START_OF_OBJECT )
        return ff_err( "More objects found than expected" );

    if ( i < num_objects )
        return ff_err( "Less objects found than expected" );

    return r;
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */