Guest User

cache segfaults because of array declaration?

a guest
Nov 24th, 2010
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. gchar *deskmenu_check_file_cache (gchar *filename){
  2.  
  3. gboolean cached = FALSE;
  4.  
  5. gchar *cache_name[1000];
  6.  
  7. gchar *cache_file[1000];
  8.  
  9. gchar *t = NULL;
  10.  
  11. gchar *f;
  12.  
  13.  
  14.  
  15. if (strlen(filename) == 0) {
  16.  
  17.         /*
  18.  
  19.         set 0,0 & 0,1 to be the default config
  20.  
  21.         check to see given argument for filename
  22.  
  23.         set default filename to be [configdir]/compiz/deskmenu/menu.xml
  24.  
  25.         create it if don't exist
  26.  
  27.         */
  28.  
  29.         filename = g_build_path (G_DIR_SEPARATOR_S,
  30.  
  31.                                    g_get_user_config_dir (),
  32.  
  33.                                    "compiz",
  34.  
  35.                                    "deskmenu",
  36.  
  37.                                    "menu.xml",
  38.  
  39.                                    NULL);
  40.  
  41.     if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR))
  42.  
  43.         {
  44.  
  45.             const gchar* const *cursor = g_get_system_config_dirs ();
  46.  
  47.             gchar *path = NULL;
  48.  
  49.             while (*cursor)
  50.  
  51.             {
  52.  
  53.                 g_free (path);
  54.  
  55.                 path = g_strdup (*cursor);
  56.  
  57.                 filename = g_build_path (G_DIR_SEPARATOR_S,
  58.  
  59.                                         path,
  60.  
  61.                                         "compiz",
  62.  
  63.                                         "deskmenu",
  64.  
  65.                                         "menu.xml",
  66.  
  67.                                         NULL);
  68.  
  69.        
  70.  
  71.                 if (g_file_get_contents (filename, &f, NULL, NULL))
  72.  
  73.                 {
  74.  
  75.                     cache_name[0] = filename;
  76.  
  77.                     g_free (path);
  78.  
  79.                     break;
  80.  
  81.                 }
  82.  
  83.                     cursor++;
  84.  
  85.             }
  86.  
  87.         }
  88.  
  89.         else
  90.  
  91.         {
  92.  
  93.             if (filename != cache_name[0]) //somehow this comparison always works, even though it shouldn't
  94.  
  95.             {
  96.  
  97.                 g_file_get_contents (filename, &f, NULL, NULL);
  98.  
  99.                 g_print("Cacheing default file...\n");
  100.  
  101.             }
  102.  
  103.             g_print("Retrieving cached default file...\n");
  104.  
  105.             cache_name[0] = filename;
  106.  
  107.         }
  108.  
  109.     cache_file[0] = f; //doesn't write back to the slot in array
  110.  
  111.     g_print(cache_name[0]);
  112.  
  113.     g_free (filename); 
  114.  
  115.     t = g_strdup(cache_file[0]);
  116.  
  117. }
  118.  
  119. else {
  120.  
  121.         int i;
  122.  
  123.         i = 0;
  124.  
  125.         //search cache
  126.  
  127.         while(cache_name[i]){
  128.  
  129.             if (filename == cache_name[i]) //this comparison doesn't work
  130.  
  131.             {
  132.  
  133.                 cached = TRUE;
  134.  
  135.                 t = g_strdup(cache_file[i]);
  136.  
  137.                 g_print("Found cached file!\n");
  138.  
  139.                 break;
  140.  
  141.             }
  142.  
  143.             i++;
  144.  
  145.         }
  146.  
  147.         //cache file if it isn't in the cache
  148.  
  149.         if (!cached) {
  150.  
  151.             if (g_file_get_contents (filename, &f, NULL, NULL))
  152.  
  153.                 {
  154.  
  155.                     g_print("Cacheing new file...\n");
  156.  
  157.                     cache_name[i+1] = filename; //doesn't write back to the slot in array
  158.  
  159.                     cache_file[i+1] = f; //doesn't write back to the slot in array
  160.  
  161.                     t = g_strdup(cache_file[i+1]);
  162.  
  163.                 }
  164.  
  165.                 else {
  166.  
  167.                     if (cache_name[0] && cache_file[0]) //do if both exist, again, doesn't work
  168.  
  169.                     {
  170.  
  171.                         g_print("Couldn't find specified file, loading default...\n");
  172.  
  173.                         t = g_strdup(cache_file[0]);
  174.  
  175.                     }
  176.  
  177.                     else
  178.  
  179.                     {
  180.  
  181.                         g_printerr ("Couldn't find a menu file\n");
  182.  
  183.                         exit (1);
  184.  
  185.                     }
  186.  
  187.                 }
  188.  
  189.         }
  190.  
  191.         g_print(filename);
  192.  
  193.         g_free (filename);
  194.  
  195.     }
  196.  
  197. return t;
  198.  
  199. }
Add Comment
Please, Sign In to add comment