[go: up one dir, main page]

File: util.c

package info (click to toggle)
libforms 1.2.3-1.7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,904 kB
  • sloc: ansic: 97,669; sh: 11,156; makefile: 951
file content (277 lines) | stat: -rw-r--r-- 6,395 bytes parent folder | download | duplicates (5)
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
/*
 *  This file is part of the XForms library package.
 *
 *  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 util.c
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *
 * X independent utilities
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include/forms.h"
#include "flinternal.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>


/***************************************
 * Returns if two objects are the same - not of any use normally,
 * but seems to help with the Python port (so don't use it for
 * anything else, it might be gone if it should not needed for
 * the Python port anymore) 
 ***************************************/

int
fl_is_same_object( FL_OBJECT * obj1,
                   FL_OBJECT * obj2 )
{
    return obj1 == obj2;
}


/***************************************
 * Sets the form window
 ***************************************/

void
fli_set_form_window( FL_FORM * form )
{
    if ( form && form->window != None )
        flx->win = form->window;
}


/***************************************
 * Function is kept only for backward compatibility
 ***************************************/

void
fl_show_errors( int y   FL_UNUSED_ARG )
{
}


/***************************************
 * Convenience replacement for XFlush()
 ***************************************/

void
fl_XFlush( void )
{
    XFlush( fl_display );
}


/***************************************
 * For debugging
 ***************************************/

#define VN( v )  { v, #v }

static FLI_VN_PAIR flevent[ ] =
{
    VN( FL_ENTER     ),
    VN( FL_LEAVE     ),
    VN( FL_PUSH      ),
    VN( FL_RELEASE   ),
    VN( FL_STEP      ),
    VN( FL_SHORTCUT  ),
    VN( FL_UPDATE    ),
    VN( FL_MOTION    ),
    VN( FL_KEYPRESS  ),
    VN( FL_DRAW      ),
    VN( FL_FOCUS     ),
    VN( FL_UNFOCUS   ),
    VN( FL_FREEMEM   ),
    VN( FL_DRAWLABEL ),
    VN( FL_DBLCLICK  ),
    VN( FL_OTHER     ),
    VN( FL_ATTRIB    ),
    { -1, NULL }
};


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

const char *
fli_event_name( int ev )
{
    return fli_get_vn_name( flevent, ev );
}


static FLI_VN_PAIR flclass[ ] =
{
    VN( FL_BUTTON        ),
    VN( FL_LIGHTBUTTON   ),
    VN( FL_ROUNDBUTTON   ),
    VN( FL_ROUND3DBUTTON ),
    VN( FL_CHECKBUTTON   ),
    VN( FL_BITMAPBUTTON  ),
    VN( FL_PIXMAPBUTTON  ),
    VN( FL_BITMAP        ),
    VN( FL_PIXMAP        ),
    VN( FL_BOX           ),
    VN( FL_TEXT          ),
    VN( FL_MENU          ),
    VN( FL_CHART         ),
    VN( FL_CHOICE        ),
    VN( FL_COUNTER       ),
    VN( FL_SLIDER        ),
    VN( FL_VALSLIDER     ),
    VN( FL_INPUT         ),
    VN( FL_BROWSER       ),
    VN( FL_DIAL          ),
    VN( FL_TIMER         ),
    VN( FL_CLOCK         ),
    VN( FL_POSITIONER    ),
    VN( FL_FREE          ),
    VN( FL_XYPLOT        ),
    VN( FL_FRAME         ),
    VN( FL_LABELFRAME    ),
    VN( FL_CANVAS        ),
    VN( FL_GLCANVAS      ),
    VN( FL_TABFOLDER     ),
    VN( FL_SCROLLBAR     ),
    VN( FL_SCROLLBUTTON  ),
    VN( FL_MENUBAR       ),
    VN( FL_IMAGECANVAS   ),
    VN( FL_TEXTBOX       ),
    VN( FL_SPINNER       ),
    { -1, NULL }
};


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

const char *
fli_object_class_name( FL_OBJECT * ob )
{
    if ( ! ob )
        return "null";
    else if ( ob == FL_EVENT )
        return "FL_EVENT";

    return fli_get_vn_name( flclass, ob->objclass );
}


/***************************************
 * Function tries to read a line (of arbirary length) from a file
 * On failure (either due to read error or missing memory) NULL is
 * returned, otherwise a pointer to an allocated buffer that must
 * be freed by the caller.
 ***************************************/

#define STRING_TRY_LENGTH 128

char *
fli_read_line( FILE *fp )
{
    char *line = NULL;
    char *old_line = NULL;
    size_t len = STRING_TRY_LENGTH;
    size_t old_len = 0;

    while ( 1 )
    {
        if ( ( line = fl_realloc( line, len ) ) == NULL )
        {
            fli_safe_free( old_line );
            M_err( "fli_read_line", "Running out of memory\n" );
            return NULL;
        }

        if ( ! fgets( line + old_len, len - old_len, fp ) )
        {
            if ( ferror( fp ) )
            {
                M_err( "fli_read_line", "Failed to read from file" );
                fl_free( line );
                return NULL;
            }

            if ( old_len == 0 )
            {
                fl_free( line );
                return NULL;
            }

            M_warn( "fli_read_line", "Missing newline at end of line" );
            break;
        }

        if ( strchr( line + old_len, '\n' ) )
            break;

        old_line = line;
        old_len = len - 1;
        len *= 2;
    }

    old_line = line;
    if ( ( line = fl_realloc( line, strlen( line ) + 1 ) ) == NULL )
        return old_line;
    return line;
}


/*******************************************
 * Secure string copy: a maximum of (n-1) bytes will be copied, the 
 * destination string is always ends in a '\0'.
 * Returns: dest or NULL if src or dest are NULL or n is 0
 *******************************************/

char *
fli_sstrcpy( char       * dest,
             const char * src,
             size_t       n )
{
    size_t l = src ? strlen( src ) : 0;

    if ( ! src || ! dest || n == 0 )
        return NULL;

    if ( l < n )
        memcpy( dest, src, l + 1 );
    else
    {
        memcpy( dest, src, n - 1 );
        dest[ n - 1 ] = '\0';
    }

    return dest;
}


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