1 /*----------------------------------------------------------------------------
2 --
3 --  Module:           xitXmStr.c
4 --
5 --  Project:          xit  - X Internal Toolkit
6 --  System:           xit  - X Internal Toolkit
7 --    Subsystem:      <>
8 --    Function block: <>
9 --
10 --  Description:
11 --    XmString routines.
12 --
13 --  Filename:         xitXmStr.c
14 --
15 --  Authors:          Roger Larsson, Ulrika Bornetun
16 --  Creation date:    1991-10-20
17 --
18 --
19 --  (C) Copyright Ulrika Bornetun, Roger Larsson (1995)
20 --      All rights reserved
21 --
22 --  Permission to use, copy, modify, and distribute this software and its
23 --  documentation for any purpose and without fee is hereby granted,
24 --  provided that the above copyright notice appear in all copies. Ulrika
25 --  Bornetun and Roger Larsson make no representations about the usability
26 --  of this software for any purpose. It is provided "as is" without express
27 --  or implied warranty.
28 ----------------------------------------------------------------------------*/
29 
30 /* SCCS module identifier. */
31 static char SCCSID[] = "@(#) Module: xitXmStr.c, Version: 1.1, Date: 95/02/18 15:10:53";
32 
33 
34 /*----------------------------------------------------------------------------
35 --  Include files
36 ----------------------------------------------------------------------------*/
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 
44 #include <X11/Intrinsic.h>
45 #include <X11/Shell.h>
46 
47 #include <Xm/Xm.h>
48 #include <Xm/Text.h>
49 
50 #include "System.h"
51 
52 #include "xitTools.h"
53 
54 
55 /*----------------------------------------------------------------------------
56 --  Macro definitions
57 ----------------------------------------------------------------------------*/
58 
59 
60 /*----------------------------------------------------------------------------
61 --  Type declarations
62 ----------------------------------------------------------------------------*/
63 
64 
65 /*----------------------------------------------------------------------------
66 --  Global definitions
67 ----------------------------------------------------------------------------*/
68 
69 
70 /*----------------------------------------------------------------------------
71 --  Function prototypes
72 ----------------------------------------------------------------------------*/
73 
74 static int
75   compareStrings( const void  *string1,
76                   const void  *string2 );
77 
78 
79 
80 /*----------------------------------------------------------------------------
81 --  Functions
82 ----------------------------------------------------------------------------*/
83 
84 XmString
xitStringConcatAndFree(XmString to_the_string,XmString string)85   xitStringConcatAndFree( XmString  to_the_string,
86                           XmString  string )
87 {
88 
89   /* Variables. */
90   XmString  new_string;
91 
92 
93   /* Code. */
94 
95   new_string = XmStringConcat( to_the_string, string );
96 
97   XmStringFree( to_the_string );
98   XmStringFree( string );
99 
100   return( new_string );
101 
102 } /* xitStringConcatAndFree */
103 
104 
105 /*----------------------------------------------------------------------*/
106 
107 char
xitStringGetLabel(Widget widget)108   *xitStringGetLabel( Widget  widget )
109 {
110 
111   /* Variables. */
112   char      *char_ref;
113   char      *label_ref;
114   Arg       args[ 5 ];
115   Cardinal  n;
116   XmString  xstr;
117 
118 
119   /* Code. */
120 
121   n = 0;
122   XtSetArg( args[ n ], XmNlabelString, &xstr ); n++;
123   XtGetValues( widget, args, n );
124 
125   if( XmStringGetLtoR( xstr, CS, &char_ref ) ) {
126     XmStringFree( xstr );
127 
128     label_ref = SysNewString( char_ref );
129     XtFree( char_ref );
130 
131     return( label_ref );
132   }
133 
134 
135   return( NULL );
136 
137 } /* xitStringGetLabel */
138 
139 
140 /*----------------------------------------------------------------------*/
141 
142 char
xitStringGetString(XmString xstr,XmStringCharSet char_set)143   *xitStringGetString( XmString         xstr,
144                        XmStringCharSet  char_set )
145 {
146 
147   /* Variables. */
148   char  *char_ref;
149   char  *label_ref;
150 
151 
152   /* Code. */
153 
154   if( XmStringGetLtoR( xstr, char_set, &char_ref ) ) {
155 
156     label_ref = SysNewString( char_ref );
157     XtFree( char_ref );
158 
159     return( label_ref );
160   }
161 
162 
163   return( NULL );
164 
165 } /* xitStringGetString */
166 
167 
168 /*----------------------------------------------------------------------*/
169 
170 char
xitStringGetText(Widget widget)171   *xitStringGetText( Widget  widget )
172 {
173 
174   /* Variables. */
175   char      *char_ref;
176   char      *label_ref;
177 
178 
179   /* Code. */
180 
181   char_ref = XmTextGetString( widget );
182 
183   label_ref = SysNewString( char_ref );
184   XtFree( char_ref );
185 
186 
187   return( label_ref );
188 
189 } /* xitStringGetText */
190 
191 
192 /*----------------------------------------------------------------------*/
193 
194 void
xitStringSetLabel(Widget widget,char * char_ref)195   xitStringSetLabel( Widget  widget,
196                      char    *char_ref )
197 {
198 
199   /* Variables. */
200   Arg       args[ 5 ];
201   Cardinal  n;
202   XmString  xstr;
203 
204 
205   /* Code. */
206 
207   xstr = XmStringCreateLtoR( char_ref, CS );
208 
209   n = 0;
210   XtSetArg( args[ n ], XmNlabelString, xstr ); n++;
211   XtSetValues( widget, args, n );
212 
213   XmStringFree( xstr );
214 
215 
216   return;
217 
218 } /* xitStringSetLabel */
219 
220 
221 /*----------------------------------------------------------------------*/
222 
223 void
xitStringSetTextFromFile(Widget widget,char * file_name)224   xitStringSetTextFromFile( Widget  widget,
225                             char    *file_name )
226 {
227 
228   /* Variables. */
229   int          status;
230   char         *text_ref;
231   FILE         *file_ref;
232   struct stat  file_info;
233 
234 
235   /* Code. */
236 
237   /* Fetch the file. */
238   status = stat( file_name, &file_info );
239   if( status != 0 )
240     return;
241 
242   if( ! S_ISREG( file_info.st_mode ) )
243     return;
244 
245   file_ref = fopen( file_name, "r" );
246   if( file_ref == NULL )
247     return;
248 
249   /* Allocate space for the contents of the file. */
250   text_ref = SysMalloc( file_info.st_size + 1 );
251 
252   /* Read the contents. */
253   status = fread( text_ref, sizeof( char ), file_info.st_size + 1, file_ref );
254 
255   if( status != 0 ) {
256 
257     XmTextPosition  position;
258 
259     *(text_ref + file_info.st_size) = '\0';
260 
261     /* Update the text fieled. */
262     position = XmTextGetInsertionPosition( widget );
263     XmTextReplace( widget, position, position, text_ref );
264 
265     XmTextShowPosition( widget, position );
266 
267   } /* if */
268 
269 
270   /* Clean up. */
271   SysFree( text_ref );
272   fclose( file_ref );
273 
274 
275   return;
276 
277 } /* xitStringSetTextFromFile */
278 
279 
280 /*----------------------------------------------------------------------*/
281 
282 void
xitSortStringList(XmString * list,int elements)283   xitSortStringList( XmString  *list,
284                      int       elements )
285 {
286 
287   /* Type definitions. */
288   typedef  char  *str_ref;
289 
290 
291   /* Variables. */
292   int   index;
293   char  **strings_table;
294 
295 
296   /* Code. */
297 
298   if( elements <= 0 )
299     return;
300 
301   /* Allocate space for a string array. */
302   strings_table = (char **) SysMalloc( elements * sizeof( str_ref ) );
303 
304   /* Fill the string table. */
305   for( index = 0; index < elements; index++ )
306     *(strings_table + index) = xitStringGetString( *(list + index), CS );
307 
308 
309   /* Use the predefined quicksort. */
310   qsort( (void *) strings_table,
311          (size_t) elements, sizeof( str_ref ),
312          compareStrings );
313 
314 
315   /* Allocate the sorted strings (and free the old). */
316   for( index = 0; index < elements; index++ ) {
317     XmStringFree( *(list + index) );
318 
319     *(list + index) = XmStringCreateLtoR( *(strings_table + index), CS );
320 
321     SysFree( *(strings_table + index) );
322   }
323 
324   SysFree( strings_table );
325 
326 
327   return;
328 
329 } /* xitSortStringList */
330 
331 
332 /*----------------------------------------------------------------------*/
333 
334 static int
compareStrings(const void * string1,const void * string2)335   compareStrings( const void  *string1,
336                   const void  *string2 )
337 {
338 
339   /* Variables. */
340   char  **cmp_str1 = (char **) string1;
341   char  **cmp_str2 = (char **) string2;
342 
343 
344   /* Code. */
345 
346   return( strcmp( *cmp_str1, *cmp_str2 ) );
347 
348 } /* compareStrings */
349 
350 
351