OSDN Git Service

LinGui: add sliders for psy-rd and psy-trellis
[handbrake-jp/handbrake-jp-git.git] / gtk / src / x264handler.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * x264handler.c
4  * Copyright (C) John Stebbins 2008 <stebbins@stebbins>
5  * 
6  * x264handler.c is free software.
7  * 
8  * You may redistribute it and/or modify it under the terms of the
9  * GNU General Public License, as published by the Free Software
10  * Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  */
13
14 #include <gtk/gtk.h>
15 #include <string.h>
16 #include "settings.h"
17 #include "values.h"
18 #include "callbacks.h"
19 #include "presets.h"
20 #include "hb-backend.h"
21 #include "x264handler.h"
22
23 static void x264_opt_update(signal_user_data_t *ud, GtkWidget *widget);
24 static gchar* sanitize_x264opts(signal_user_data_t *ud, const gchar *options);
25
26 // Flag needed to prevent x264 options processing from chasing its tail
27 static gboolean ignore_options_update = FALSE;
28
29 void
30 x264_widget_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
31 {
32         ghb_widget_to_setting(ud->settings, widget);
33         if (!ignore_options_update)
34         {
35                 ignore_options_update = TRUE;
36                 x264_opt_update(ud, widget);
37                 ignore_options_update = FALSE;
38         }
39         ghb_check_dependency(ud, widget);
40         ghb_clear_presets_selection(ud);
41 }
42
43 void
44 x264_me_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
45 {
46         const GValue *gval;
47         gint me;
48
49         ghb_widget_to_setting(ud->settings, widget);
50         if (!ignore_options_update)
51         {
52                 ignore_options_update = TRUE;
53                 x264_opt_update(ud, widget);
54                 ignore_options_update = FALSE;
55         }
56         ghb_check_dependency(ud, widget);
57         ghb_clear_presets_selection(ud);
58         widget = GHB_WIDGET(ud->builder, "x264_merange");
59         gval = ghb_settings_get_value(ud->settings, "x264_me");
60         me = ghb_lookup_combo_int("x264_me", gval);
61         if (me < 2)
62         {       // me < umh
63                 // me_range 4 - 16
64                 gtk_spin_button_set_range(GTK_SPIN_BUTTON(widget), 4, 16);
65         }
66         else
67         {
68                 // me_range 4 - 64
69                 gtk_spin_button_set_range(GTK_SPIN_BUTTON(widget), 4, 64);
70         }
71 }
72
73 void
74 x264_entry_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
75 {
76         g_debug("x264_entry_changed_cb ()");
77         if (!ignore_options_update)
78         {
79                 GtkWidget *textview;
80                 gchar *options;
81
82                 textview = GTK_WIDGET(GHB_WIDGET(ud->builder, "x264Option"));
83                 ghb_widget_to_setting(ud->settings, textview);
84                 options = ghb_settings_get_string(ud->settings, "x264Option");
85                 ignore_options_update = TRUE;
86                 ghb_x264_parse_options(ud, options);
87                 if (!GTK_WIDGET_HAS_FOCUS(textview))
88                 {
89                         gchar *sopts;
90
91                         sopts = sanitize_x264opts(ud, options);
92                         ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
93                         ghb_x264_parse_options(ud, sopts);
94                         g_free(sopts);
95                 }
96                 g_free(options);
97                 ignore_options_update = FALSE;
98         }
99 }
100
101 gboolean
102 x264_focus_out_cb(GtkWidget *widget, GdkEventFocus *event, 
103         signal_user_data_t *ud)
104 {
105         gchar *options, *sopts;
106
107         ghb_widget_to_setting(ud->settings, widget);
108         options = ghb_settings_get_string(ud->settings, "x264Option");
109         sopts = sanitize_x264opts(ud, options);
110         ignore_options_update = TRUE;
111         if (sopts != NULL && strcmp(sopts, options) != 0)
112         {
113                 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
114                 ghb_x264_parse_options(ud, sopts);
115         }
116         g_free(options);
117         g_free(sopts);
118         ignore_options_update = FALSE;
119         return FALSE;
120 }
121
122 enum
123 {
124         X264_OPT_DEBLOCK,
125         X264_OPT_PSY,
126         X264_OPT_INT,
127         X264_OPT_COMBO,
128         X264_OPT_BOOL,
129 };
130
131 struct x264_opt_map_s
132 {
133         gchar **opt_syns;
134         gchar *name;
135         gchar *def_val;
136         gint type;
137         gboolean found;
138 };
139
140 static gchar *x264_ref_syns[] = {"ref", "frameref", NULL};
141 static gchar *x264_mixed_syns[] = {"mixed-refs", "mixed_refs", NULL};
142 static gchar *x264_bframes_syns[] = {"bframes", NULL};
143 static gchar *x264_direct_syns[] = 
144         {"direct", "direct-pred", "direct_pred", NULL};
145 static gchar *x264_weightb_syns[] = {"weightb", "weight-b", "weight_b", NULL};
146 static gchar *x264_bpyramid_syns[] = {"b-pyramid", "b_pyramid", NULL};
147 static gchar *x264_me_syns[] = {"me", NULL};
148 static gchar *x264_merange_syns[] = {"merange", "me-range", "me_range", NULL};
149 static gchar *x264_subme_syns[] = {"subme", "subq", NULL};
150 static gchar *x264_analyse_syns[] = {"analyse", "partitions", NULL};
151 static gchar *x264_8x8dct_syns[] = {"8x8dct", NULL};
152 static gchar *x264_deblock_syns[] = {"deblock", "filter", NULL};
153 static gchar *x264_trellis_syns[] = {"trellis", NULL};
154 static gchar *x264_pskip_syns[] = {"no-fast-pskip", "no_fast_pskip", NULL};
155 static gchar *x264_psy_syns[] = {"psy-rd", NULL};
156 static gchar *x264_decimate_syns[] = 
157         {"no-dct-decimate", "no_dct_decimate", NULL};
158 static gchar *x264_cabac_syns[] = {"cabac", NULL};
159
160 static gint
161 find_syn_match(const gchar *opt, gchar **syns)
162 {
163         gint ii;
164         for (ii = 0; syns[ii] != NULL; ii++)
165         {
166                 if (strcmp(opt, syns[ii]) == 0)
167                         return ii;
168         }
169         return -1;
170 }
171
172 struct x264_opt_map_s x264_opt_map[] =
173 {
174         {x264_ref_syns, "x264_refs", "1", X264_OPT_INT},
175         {x264_mixed_syns, "x264_mixed_refs", "0", X264_OPT_BOOL},
176         {x264_bframes_syns, "x264_bframes", "0", X264_OPT_INT},
177         {x264_direct_syns, "x264_direct", "spatial", X264_OPT_COMBO},
178         {x264_weightb_syns, "x264_weighted_bframes", "0", X264_OPT_BOOL},
179         {x264_bpyramid_syns, "x264_bpyramid", "0", X264_OPT_BOOL},
180         {x264_me_syns, "x264_me", "hex", X264_OPT_COMBO},
181         {x264_merange_syns, "x264_merange", "16", X264_OPT_INT},
182         {x264_subme_syns, "x264_subme", "6", X264_OPT_COMBO},
183         {x264_analyse_syns, "x264_analyse", "some", X264_OPT_COMBO},
184         {x264_8x8dct_syns, "x264_8x8dct", "0", X264_OPT_BOOL},
185         {x264_deblock_syns, "x264_deblock_alpha", "0,0", X264_OPT_DEBLOCK},
186         {x264_deblock_syns, "x264_deblock_beta", "0,0", X264_OPT_DEBLOCK},
187         {x264_trellis_syns, "x264_trellis", "0", X264_OPT_COMBO},
188         {x264_pskip_syns, "x264_no_fast_pskip", "0", X264_OPT_BOOL},
189         {x264_decimate_syns, "x264_no_dct_decimate", "0", X264_OPT_BOOL},
190         {x264_cabac_syns, "x264_cabac", "1", X264_OPT_BOOL},
191         {x264_psy_syns, "x264_psy_rd", "1,0", X264_OPT_PSY},
192         {x264_psy_syns, "x264_psy_trell", "1,0", X264_OPT_PSY},
193 };
194 #define X264_OPT_MAP_SIZE (sizeof(x264_opt_map)/sizeof(struct x264_opt_map_s))
195
196 static const gchar*
197 x264_opt_get_default(const gchar *opt)
198 {
199         gint jj;
200         for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
201         {
202                 if (find_syn_match(opt, x264_opt_map[jj].opt_syns) >= 0)
203                 {
204                         return x264_opt_map[jj].def_val;
205                 }
206         }
207         return "";
208 }
209
210 static void
211 x264_update_int(signal_user_data_t *ud, const gchar *name, const gchar *val)
212 {
213         gint ival;
214
215         if (val == NULL) return;
216         ival = g_strtod (val, NULL);
217         ghb_ui_update(ud, name, ghb_int64_value(ival));
218 }
219
220 static gchar *true_str[] =
221 {
222         "true",
223         "yes",
224         "1",
225         NULL
226 };
227
228 static gboolean 
229 str_is_true(const gchar *str)
230 {
231         gint ii;
232         for (ii = 0; true_str[ii]; ii++)
233         {
234                 if (g_ascii_strcasecmp(str, true_str[ii]) == 0)
235                         return TRUE;
236         }
237         return FALSE;
238 }
239
240 static void
241 x264_update_bool(signal_user_data_t *ud, const gchar *name, const gchar *val)
242 {
243         if (val == NULL)
244                 ghb_ui_update(ud, name, ghb_boolean_value(1));
245         else
246                 ghb_ui_update(ud, name, ghb_boolean_value(str_is_true(val)));
247 }
248
249 static void
250 x264_update_combo(signal_user_data_t *ud, const gchar *name, const gchar *val)
251 {
252         GtkTreeModel *store;
253         GtkTreeIter iter;
254         gchar *shortOpt;
255         gint ivalue;
256         gboolean foundit = FALSE;
257         GtkWidget *widget;
258
259         if (val == NULL) return;
260         widget = GHB_WIDGET(ud->builder, name);
261         if (widget == NULL)
262         {
263                 g_debug("Failed to find widget for key: %s\n", name);
264                 return;
265         }
266         store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
267         if (gtk_tree_model_get_iter_first (store, &iter))
268         {
269                 do
270                 {
271                         gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
272                         if (strcmp(shortOpt, val) == 0)
273                         {
274                                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
275                                 g_free(shortOpt);
276                                 foundit = TRUE;
277                                 break;
278                         }
279                         g_free(shortOpt);
280                 } while (gtk_tree_model_iter_next (store, &iter));
281         }
282         if (!foundit)
283         {
284                 if (gtk_tree_model_get_iter_first (store, &iter))
285                 {
286                         do
287                         {
288                                 gtk_tree_model_get(store, &iter, 2, &shortOpt, 3, &ivalue, -1);
289                                 if (strcmp(shortOpt, "custom") == 0)
290                                 {
291                                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, 4, val, -1);
292                                         gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
293                                         g_free(shortOpt);
294                                         foundit = TRUE;
295                                         break;
296                                 }
297                                 g_free(shortOpt);
298                         } while (gtk_tree_model_iter_next (store, &iter));
299                 }
300         }
301         // Its possible the value hasn't changed. Since settings are only
302         // updated when the value changes, I'm initializing settings here as well.
303         ghb_widget_to_setting(ud->settings, widget);
304 }
305
306 static void
307 x264_update_deblock(signal_user_data_t *ud, const gchar *xval)
308 {
309         gdouble avalue, bvalue;
310         gchar *end;
311         gchar *val;
312         gchar *bval = NULL;
313
314         if (xval == NULL) return;
315         val = g_strdup(xval);
316         bvalue = avalue = 0;
317         if (val != NULL) 
318         {
319                 gchar *pos = strchr(val, ',');
320                 if (pos != NULL)
321                 {
322                         bval = pos + 1;
323                         *pos = 0;
324                 }
325                 avalue = g_strtod (val, &end);
326                 if (bval != NULL)
327                 {
328                         bvalue = g_strtod (bval, &end);
329                 }
330         }
331         g_free(val);
332         ghb_ui_update(ud, "x264_deblock_alpha", ghb_int64_value(avalue));
333         ghb_ui_update(ud, "x264_deblock_beta", ghb_int64_value(bvalue));
334 }
335
336 static void
337 x264_update_psy(signal_user_data_t *ud, const gchar *xval)
338 {
339         gdouble rd_value, trell_value;
340         gchar *end;
341         gchar *val;
342         gchar *trell_val = NULL;
343
344         if (xval == NULL) return;
345         val = g_strdup(xval);
346         rd_value = trell_value = 0;
347         if (val != NULL) 
348         {
349                 gchar *pos = strchr(val, ',');
350                 if (pos != NULL)
351                 {
352                         trell_val = pos + 1;
353                         *pos = 0;
354                 }
355                 rd_value = g_strtod (val, &end);
356                 if (trell_val != NULL)
357                 {
358                         trell_value = g_strtod (trell_val, &end);
359                 }
360         }
361         g_free(val);
362         ghb_ui_update(ud, "x264_psy_rd", ghb_double_value(rd_value));
363         ghb_ui_update(ud, "x264_psy_trell", ghb_double_value(trell_value));
364 }
365
366 void
367 ghb_x264_parse_options(signal_user_data_t *ud, const gchar *options)
368 {
369         gchar **split = g_strsplit(options, ":", -1);
370         if (split == NULL) return;
371
372         gint ii;
373         gint jj;
374
375         for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
376                 x264_opt_map[jj].found = FALSE;
377
378         for (ii = 0; split[ii] != NULL; ii++)
379         {
380                 gchar *val = NULL;
381                 gchar *pos = strchr(split[ii], '=');
382                 if (pos != NULL)
383                 {
384                         val = pos + 1;
385                         *pos = 0;
386                 }
387                 for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
388                 {
389                         if (find_syn_match(split[ii], x264_opt_map[jj].opt_syns) >= 0)
390                         {
391                                 x264_opt_map[jj].found = TRUE;
392                                 switch(x264_opt_map[jj].type)
393                                 {
394                                 case X264_OPT_INT:
395                                         x264_update_int(ud, x264_opt_map[jj].name, val);
396                                         break;
397                                 case X264_OPT_BOOL:
398                                         x264_update_bool(ud, x264_opt_map[jj].name, val);
399                                         break;
400                                 case X264_OPT_COMBO:
401                                         x264_update_combo(ud, x264_opt_map[jj].name, val);
402                                         break;
403                                 case X264_OPT_DEBLOCK:
404                                         // dirty little hack.  mark deblock_beta found as well
405                                         x264_opt_map[jj+1].found = TRUE;
406                                         x264_update_deblock(ud, val);
407                                         break;
408                                 case X264_OPT_PSY:
409                                         // dirty little hack.  mark psy_trell found as well
410                                         x264_opt_map[jj+1].found = TRUE;
411                                         x264_update_psy(ud, val);
412                                         break;
413                                 }
414                                 break;
415                         }
416                 }
417         }
418         // For any options not found in the option string, set ui to
419         // default values
420         for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
421         {
422                 if (!x264_opt_map[jj].found)
423                 {
424                         gchar *val = strdup(x264_opt_map[jj].def_val);
425                         switch(x264_opt_map[jj].type)
426                         {
427                         case X264_OPT_INT:
428                                 x264_update_int(ud, x264_opt_map[jj].name, val);
429                                 break;
430                         case X264_OPT_BOOL:
431                                 x264_update_bool(ud, x264_opt_map[jj].name, val);
432                                 break;
433                         case X264_OPT_COMBO:
434                                 x264_update_combo(ud, x264_opt_map[jj].name, val);
435                                 break;
436                         case X264_OPT_DEBLOCK:
437                                 x264_update_deblock(ud, val);
438                                 break;
439                         case X264_OPT_PSY:
440                                 x264_update_psy(ud, val);
441                                 break;
442                         }
443                         x264_opt_map[jj].found = TRUE;
444                         g_free(val);
445                 }
446         }
447         g_strfreev(split);
448 }
449
450 gchar*
451 get_deblock_val(signal_user_data_t *ud)
452 {
453         gchar *alpha, *beta;
454         gchar *result;
455         alpha = ghb_settings_get_string(ud->settings, "x264_deblock_alpha");
456         beta = ghb_settings_get_string(ud->settings, "x264_deblock_beta");
457         result = g_strdup_printf("%s,%s", alpha, beta);
458         g_free(alpha);
459         g_free(beta);
460         return result;
461 }
462
463 gchar*
464 get_psy_val(signal_user_data_t *ud)
465 {
466         gdouble rd, trell;
467         gchar *result;
468         rd = ghb_settings_get_double(ud->settings, "x264_psy_rd");
469         trell = ghb_settings_get_double(ud->settings, "x264_psy_trell");
470         result = g_strdup_printf("%g,%g", rd, trell);
471         return result;
472 }
473
474 static void
475 x264_opt_update(signal_user_data_t *ud, GtkWidget *widget)
476 {
477         gint jj;
478         const gchar *name = gtk_widget_get_name(widget);
479         gchar **opt_syns = NULL;
480         const gchar *def_val = NULL;
481         gint type;
482
483         for (jj = 0; jj < X264_OPT_MAP_SIZE; jj++)
484         {
485                 if (strcmp(name, x264_opt_map[jj].name) == 0)
486                 {
487                         // found the options that needs updating
488                         opt_syns = x264_opt_map[jj].opt_syns;
489                         def_val = x264_opt_map[jj].def_val;
490                         type = x264_opt_map[jj].type;
491                         break;
492                 }
493         }
494         if (opt_syns != NULL)
495         {
496                 GString *x264opts = g_string_new("");
497                 gchar *options;
498                 gchar **split = NULL;
499                 gint ii;
500                 gboolean foundit = FALSE;
501
502                 options = ghb_settings_get_string(ud->settings, "x264Option");
503                 if (options)
504                 {
505                         split = g_strsplit(options, ":", -1);
506                         g_free(options);
507                 }
508                 for (ii = 0; split && split[ii] != NULL; ii++)
509                 {
510                         gint syn;
511                         gchar *val = NULL;
512                         gchar *pos = strchr(split[ii], '=');
513                         if (pos != NULL)
514                         {
515                                 val = pos + 1;
516                                 *pos = 0;
517                         }
518                         syn = find_syn_match(split[ii], opt_syns);
519                         if (syn >= 0)
520                         { // Updating this option
521                                 gchar *val;
522                                 foundit = TRUE;
523                                 if (type == X264_OPT_DEBLOCK)
524                                         val = get_deblock_val(ud);
525                                 else if (type == X264_OPT_PSY)
526                                         val = get_psy_val(ud);
527                                 else
528                                 {
529                                         GValue *gval;
530                                         gval = ghb_widget_value(widget);
531                                         if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
532                                         {
533                                                 if (ghb_value_boolean(gval))
534                                                         val = g_strdup("1");
535                                                 else
536                                                         val = g_strdup("0");
537                                         }
538                                         else
539                                         {
540                                                 val = ghb_widget_string(widget);
541                                         }
542                                         ghb_value_free(gval);
543                                 }
544                                 if (strcmp(def_val, val) != 0)
545                                 {
546                                         g_string_append_printf(x264opts, "%s=%s:", opt_syns[syn], val);
547                                 }
548                                 g_free(val);
549                         }
550                         else if (val != NULL)
551                                 g_string_append_printf(x264opts, "%s=%s:", split[ii], val);
552                         else
553                                 g_string_append_printf(x264opts, "%s:", split[ii]);
554
555                 }
556                 if (split) g_strfreev(split);
557                 if (!foundit)
558                 {
559                         gchar *val;
560                         if (type == X264_OPT_DEBLOCK)
561                                 val = get_deblock_val(ud);
562                         else if (type == X264_OPT_PSY)
563                                 val = get_psy_val(ud);
564                         else
565                         {
566                                 GValue *gval;
567                                 gval = ghb_widget_value(widget);
568                                 if (G_VALUE_TYPE(gval) == G_TYPE_BOOLEAN)
569                                 {
570                                         if (ghb_value_boolean(gval))
571                                                 val = g_strdup("1");
572                                         else
573                                                 val = g_strdup("0");
574                                 }
575                                 else
576                                 {
577                                         val = ghb_widget_string(widget);
578                                 }
579                                 ghb_value_free(gval);
580                         }
581                         if (strcmp(def_val, val) != 0)
582                         {
583                                 g_string_append_printf(x264opts, "%s=%s:", opt_syns[0], val);
584                         }
585                         g_free(val);
586                 }
587                 // Update the options value
588                 // strip the trailing ":"
589                 gchar *result;
590                 gint len;
591                 result = g_string_free(x264opts, FALSE);
592                 len = strlen(result);
593                 if (len > 0) result[len - 1] = 0;
594                 gchar *sopts;
595                 sopts = sanitize_x264opts(ud, result);
596                 ghb_ui_update(ud, "x264Option", ghb_string_value(sopts));
597                 ghb_x264_parse_options(ud, sopts);
598                 g_free(sopts);
599                 g_free(result);
600         }
601 }
602
603 static void
604 x264_remove_opt(gchar **opts, gchar **opt_syns)
605 {
606         gint ii;
607         for (ii = 0; opts[ii] != NULL; ii++)
608         {
609                 gchar *opt;
610                 opt = g_strdup(opts[ii]);
611                 gchar *pos = strchr(opt, '=');
612                 if (pos != NULL)
613                 {
614                         *pos = 0;
615                 }
616                 if (find_syn_match(opt, opt_syns) >= 0)
617                 {
618                         // Mark as deleted
619                         opts[ii][0] = 0;
620                 }
621                 g_free(opt);
622         }
623 }
624
625 // Construct the x264 options string
626 // The result is allocated, so someone must free it at some point.
627 static gchar*
628 sanitize_x264opts(signal_user_data_t *ud, const gchar *options)
629 {
630         GString *x264opts = g_string_new("");
631         gchar **split = g_strsplit(options, ":", -1);
632
633         // Remove entries that match the defaults
634         gint ii;
635         for (ii = 0; split[ii] != NULL; ii++)
636         {
637                 gchar *val = NULL;
638                 gchar *opt = g_strdup(split[ii]);
639                 gchar *pos = strchr(opt, '=');
640                 if (pos != NULL)
641                 {
642                         val = pos + 1;
643                         *pos = 0;
644                 }
645                 else
646                 {
647                         val = "1";
648                 }
649                 const gchar *def_val = x264_opt_get_default(opt);
650                 if (strcmp(val, def_val) == 0)
651                 {
652                         // Matches the default, so remove it
653                         split[ii][0] = 0;
654                 }
655                 g_free(opt);
656         }
657         gint refs = ghb_settings_get_int(ud->settings, "x264_refs");
658         if (refs <= 1)
659         {
660                 x264_remove_opt(split, x264_mixed_syns);
661         }
662         gint bframes = ghb_settings_get_int(ud->settings, "x264_bframes");
663         if (bframes == 0)
664         {
665                 x264_remove_opt(split, x264_weightb_syns);
666                 x264_remove_opt(split, x264_direct_syns);
667         }
668         if (bframes <= 1)
669         {
670                 x264_remove_opt(split, x264_bpyramid_syns);
671         }
672         if (!ghb_settings_get_boolean(ud->settings, "x264_cabac"))
673         {
674                 x264_remove_opt(split, x264_trellis_syns);
675         }
676         for (ii = 0; split[ii] != NULL; ii++)
677         {
678                 if (split[ii][0] != 0)
679                         g_string_append_printf(x264opts, "%s:", split[ii]);
680         }
681         g_strfreev(split);
682         // strip the trailing ":"
683         gchar *result;
684         gint len;
685         result = g_string_free(x264opts, FALSE);
686         len = strlen(result);
687         if (len > 0) result[len - 1] = 0;
688         return result;
689 }
690