44116eb
diff -up scim-pinyin-0.5.91/src/scim_pinyin_imengine.cpp.orig scim-pinyin-0.5.91/src/scim_pinyin_imengine.cpp
44116eb
--- scim-pinyin-0.5.91/src/scim_pinyin_imengine.cpp.orig	2007-11-09 16:51:49.000000000 +0800
44116eb
+++ scim-pinyin-0.5.91/src/scim_pinyin_imengine.cpp	2007-11-09 16:52:06.000000000 +0800
44116eb
@@ -656,6 +656,11 @@ PinyinFactory::refresh ()
44116eb
 void
44116eb
 PinyinFactory::save_user_library ()
44116eb
 {
44116eb
+    String tmp_user_pinyin_table = m_user_pinyin_table + ".tmp";
44116eb
+    String tmp_user_phrase_lib = m_user_phrase_lib + ".tmp";
44116eb
+    String tmp_user_pinyin_phrase_lib = m_user_pinyin_phrase_lib + ".tmp";
44116eb
+    String tmp_user_pinyin_phrase_index = m_user_pinyin_phrase_index + ".tmp";
44116eb
+
44116eb
     // First make the user data directory.
44116eb
     if (access (m_user_data_directory.c_str (), R_OK | W_OK) != 0) {
44116eb
         mkdir (m_user_data_directory.c_str (), S_IRUSR | S_IWUSR | S_IXUSR);
44116eb
@@ -670,11 +675,23 @@ PinyinFactory::save_user_library ()
44116eb
         lib->optimize_phrase_frequencies ();
44116eb
     }
44116eb
 
44116eb
-    m_pinyin_global.save_pinyin_table (m_user_pinyin_table.c_str (), m_user_data_binary);
44116eb
-    m_pinyin_global.save_user_phrase_lib (m_user_phrase_lib.c_str (),
44116eb
-                                          m_user_pinyin_phrase_lib.c_str (),
44116eb
-                                          m_user_pinyin_phrase_index.c_str (),
44116eb
+    // save user data in tmp files
44116eb
+    m_pinyin_global.save_pinyin_table (tmp_user_pinyin_table.c_str (), m_user_data_binary);
44116eb
+    m_pinyin_global.save_user_phrase_lib (tmp_user_phrase_lib.c_str (),
44116eb
+                                          tmp_user_pinyin_phrase_lib.c_str (),
44116eb
+                                          tmp_user_pinyin_phrase_index.c_str (),
44116eb
                                           m_user_data_binary);
44116eb
+
44116eb
+   // unlink old user data files, and rename tmp files.
44116eb
+   unlink (m_user_pinyin_table.c_str ());
44116eb
+   rename (tmp_user_pinyin_table.c_str (), m_user_pinyin_table.c_str ());
44116eb
+   
44116eb
+   unlink (m_user_phrase_lib.c_str ());
44116eb
+   unlink (m_user_pinyin_phrase_lib.c_str ());
44116eb
+   unlink (m_user_pinyin_phrase_index.c_str ());
44116eb
+   rename (tmp_user_phrase_lib.c_str (), m_user_phrase_lib.c_str ());
44116eb
+   rename (tmp_user_pinyin_phrase_lib.c_str (), m_user_pinyin_phrase_lib.c_str ());
44116eb
+   rename (tmp_user_pinyin_phrase_index.c_str (), m_user_pinyin_phrase_index.c_str ());
44116eb
 }
44116eb
 
44116eb
 void