[RFC ;-)]: some more flexible way to load localized files (hints, help, etc)



This patch provide more flexible method to load localized files from <mc_home>
directory. There is a new function 
char *load_mc_home_file (const char * filename, char ** allocated_filename)
derived from cmd.c:get_random_hint(). It use next algorithm. 
Let mc_home is "/usr/lib/mc", filename is "mc.hlp" and guess_message_value ()
returned "russian_cis.cl8koi8r".
    1) It try to load             "/usr/lib/mc/mc.hlp.russian_cis.cl8koi8r"
    2) If 1) fails it try to load "/usr/lib/mc/mc.hlp.ru"
    3) If 3) fails it try to load "/usr/src/mc/mc.hlp" (default)
If allocated_filename is not NULL it store a pointer to a filename loaded 
at last try and must be g_free()d after use.

Now it can be used to load hint and help files. Here is an absent a ChangeLog 
entry becase this letter is a RFC only.  Tomasz Kloczko offered to rewrite
help system from scratch and a second reason is my English.

Good luck!
Andrew.

Index: gtkedit/editcmd.c
===================================================================
RCS file: /home/sav/.cvsroot/mc/gtkedit/editcmd.c,v
retrieving revision 1.27
diff -u -r1.27 editcmd.c
--- gtkedit/editcmd.c	2000/10/30 18:35:32	1.27
+++ gtkedit/editcmd.c	2000/11/01 11:01:43
@@ -165,9 +165,7 @@
 
 void edit_help_cmd (WEdit * edit)
 {
-    char *hlpdir = concat_dir_and_file (mc_home, _("mc.hlp"));
-    interactive_display (hlpdir, "[Internal File Editor]");
-    free (hlpdir);
+    interactive_display (NULL, "[Internal File Editor]");
     edit->force |= REDRAW_COMPLETELY;
 }
 
Index: src/view.c
===================================================================
RCS file: /home/sav/.cvsroot/mc/src/view.c,v
retrieving revision 1.40
diff -u -r1.40 view.c
--- src/view.c	2000/04/18 11:19:19	1.40
+++ src/view.c	2000/10/30 11:46:00
@@ -1830,9 +1838,7 @@
 /* Real view only */
 static void help_cmd (void)
 {
-    char *hlpfile = concat_dir_and_file (mc_home, _("mc.hlp"));
-    interactive_display (hlpfile, "[Internal File Viewer]");
-    g_free (hlpfile);
+    interactive_display (NULL, "[Internal File Viewer]");
     /*
     view_refresh (0);
     */
Index: src/dlg.c
===================================================================
RCS file: /home/sav/.cvsroot/mc/src/dlg.c,v
retrieving revision 1.31
diff -u -r1.31 dlg.c
--- src/dlg.c	2000/04/25 08:57:06	1.31
+++ src/dlg.c	2000/10/30 11:48:42
@@ -636,8 +636,6 @@
 
 static INLINE void dialog_handle_key (Dlg_head *h, int d_key)
 {
-   char *hlpfile;
-
     switch (d_key){
     case KEY_LEFT:
     case KEY_UP:
@@ -650,9 +648,7 @@
 	break;
 
     case KEY_F(1):
-        hlpfile = concat_dir_and_file (mc_home, _("mc.hlp"));
-	interactive_display (hlpfile, h->help_ctx);
-        g_free (hlpfile);
+	interactive_display (NULL, h->help_ctx);
 	do_refresh ();
 	break;
 
Index: src/tree.c
===================================================================
RCS file: /home/sav/.cvsroot/mc/src/tree.c,v
retrieving revision 1.23
diff -u -r1.23 tree.c
--- src/tree.c	2000/10/30 11:51:34	1.23
+++ src/tree.c	2000/10/30 11:52:23
@@ -651,9 +651,7 @@
 
 static void tree_help_cmd (void)
 {
-    char *hlpfile = concat_dir_and_file (mc_home, _("mc.hlp"));
-    interactive_display (hlpfile,  "[Directory Tree]");
-    g_free (hlpfile);
+    interactive_display (NULL,  "[Directory Tree]");
 }
 
 static int tree_copy_cmd (WTree *tree)
Index: src/help.c
===================================================================
RCS file: /home/sav/.cvsroot/mc/src/help.c,v
retrieving revision 1.9
diff -u -p -r1.9 help.c
--- src/help.c	2000/10/30 11:54:21	1.9
+++ src/help.c	2000/10/31 19:00:52
@@ -759,14 +759,26 @@ interactive_display_finish (void)
 void
 interactive_display (char *filename, char *node)
 {
-    WButtonBar *help_bar;
-    Widget     *md;
-    
-    if ((data = load_file (filename)) == 0){
+    WButtonBar	*help_bar;
+    Widget	*md;
+    char	*hlpfile = filename;
+
+    if (filename)
+	data = load_file (filename);
+    else
+	data = load_mc_home_file ("mc.hlp", &hlpfile);
+
+    if (data == NULL){
 	message (1, MSG_ERROR, _(" Can't open file %s \n %s "),
-		 filename, unix_error_string (errno));
-	return;
+		 hlpfile, unix_error_string (errno));
     }
+
+    if (!filename)
+	g_free (hlpfile);
+
+    if (!data)
+	return;
+
     if (!(main_node = search_string (data, node))){
 	message (1, MSG_ERROR, _(" Can't find node %s in help file "), node);
 	interactive_display_finish ();
Index: src/cmd.c
===================================================================
RCS file: /home/sav/.cvsroot/mc/src/cmd.c,v
retrieving revision 1.66
diff -u -p -r1.66 cmd.c
--- src/cmd.c	2000/09/01 13:59:07	1.66
+++ src/cmd.c	2001/05/03 19:24:16
@@ -1201,9 +1201,7 @@ void other_symlink_cmd (void)
 
 void help_cmd (void)
 {
-   char *hlpfile = concat_dir_and_file (mc_home, "mc.hlp");
-   interactive_display (hlpfile, "[main]");
-   g_free (hlpfile);
+   interactive_display (NULL, "[main]");
 }
 
 void view_panel_cmd (void)
@@ -1269,8 +1267,6 @@ char *guess_message_value (unsigned want
 char *get_random_hint (void)
 {
     char *data, *result, *eol;
-    char *hintfile_base, *hintfile;
-    char *lang;
     int  len;
     int start;
     
@@ -1294,24 +1290,7 @@ char *get_random_hint (void)
     last_sec = tv.tv_sec;
 #endif
 
-    hintfile_base = concat_dir_and_file (mc_home, MC_HINT);
-    lang = guess_message_value (0);
-
-    hintfile = g_strdup_printf ("%s.%s", hintfile_base, lang);
-    data = load_file (hintfile);
-    g_free (hintfile);
-
-    if (!data) {
-	hintfile = g_strdup_printf ("%s.%.2s", hintfile_base, lang);
-	data = load_file (hintfile);
-	g_free (hintfile);
-
-	if (!data)
-	    data = load_file (hintfile_base);
-    }
-
-    g_free (lang);
-    g_free (hintfile_base);
+    data = load_mc_home_file (MC_HINT, NULL));
     if (!data)
 	return 0;
 
Index: src/util.c
===================================================================
RCS file: /home/sav/.cvsroot/mc/src/util.c,v
retrieving revision 1.33
diff -u -p -r1.33 util.c
--- src/util.c	2001/01/03 12:00:25	1.33
+++ src/util.c	2001/05/04 13:06:45
@@ -589,6 +589,43 @@ char *load_file (char *filename)
     }
 }
 
+char *load_mc_home_file (const char *filename, char ** allocated_filename)
+{
+    char *hintfile_base, *hintfile;
+    char *lang;
+    char *data;
+
+    hintfile_base = concat_dir_and_file (mc_home, filename);
+    lang = guess_message_value (0);
+
+    hintfile = g_strdup_printf ("%s.%s", hintfile_base, lang);
+    data = load_file (hintfile);
+
+    if (!data) {
+	g_free (hintfile);
+	hintfile = g_strdup_printf ("%s.%.2s", hintfile_base, lang);
+	data = load_file (hintfile);
+
+	if (!data) {
+	    g_free (hintfile);
+	    hintfile = hintfile_base;
+	    data = load_file (hintfile_base);
+	}
+    }
+
+    g_free (lang);
+
+    if (hintfile != hintfile_base)
+	g_free (hintfile_base);
+
+    if (allocated_filename)
+	*allocated_filename = hintfile;
+    else
+	g_free (hintfile);
+
+    return data;
+}
+
 /* Check strftime() results. Some systems (i.e. Solaris) have different
 short-month-name sizes for different locales */ 
 size_t i18n_checktimelength (void)
Index: src/util.h
===================================================================
RCS file: /home/sav/.cvsroot/mc/src/util.h,v
retrieving revision 1.22
diff -u -p -r1.22 util.h
--- src/util.h	2000/06/15 09:18:29	1.22
+++ src/util.h	2001/05/04 13:08:23
@@ -46,6 +46,7 @@ int set_int (char *, char *, int);
 int get_int (char *, char *, int);
 
 char *load_file (char *filename);
+char *load_mc_home_file (const char *filename, char ** allocated_filename);
 
 #include "fs.h"
 #ifndef S_ISLNK




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]