Back to index

gcompris  8.2.2
Functions
cache.c File Reference
#include "gcompris.h"
#include "gc_core.h"
#include <string.h>

Go to the source code of this file.

Functions

void _cache_init (const gchar *basedir, const gchar *currentdir)
 For debug only.
void gc_cache_init (int max_size)
 Initialize the cache system.
void gc_cache_end ()
 End the cache system.
void gc_cache_clear ()
 Clear the cache.
gchar * gc_cache_get (gchar *url)
 Get a file from the cache based on it's URL.
gchar * gc_cache_insert (const gchar *url, const char *buffer, gssize length)
 Put and Get a file from the cache.

Function Documentation

void _cache_init ( const gchar *  basedir,
const gchar *  currentdir 
)

For debug only.

recursively parse the cache and fill up the hash with files there

Definition at line 62 of file cache.c.

{
#ifdef USE_GNET
  GcomprisProperties *properties = gc_prop_get();
  GDir *dir;
  const gchar *file;

  if(!currentdir)
    currentdir = basedir;

  dir = g_dir_open(currentdir, 0, NULL);

  printf("  _cache_init %s\n", currentdir);

  while((file = g_dir_read_name(dir)))
    {
      gchar *fullfile = g_strconcat(currentdir, "/", file, NULL);
      printf("  processing file = %s\n", fullfile);

      if(g_file_test(fullfile, G_FILE_TEST_IS_DIR))
       {
         _cache_init(basedir, fullfile);
       }
      else if(g_file_test(fullfile, G_FILE_TEST_IS_REGULAR))
       {
         /* Strip file to get the KEY part out of it */
         printf("  Adding '%s' in the cache\n", fullfile);
         g_hash_table_insert(hash_cache,
                           g_strconcat(properties->server, "/",
                                     fullfile + strlen(basedir) + 1,
                                     NULL),
                           g_strdup(fullfile));

       }
      g_free(fullfile);
    }

  g_dir_close(dir);
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void gc_cache_clear ( )

Clear the cache.

All files in the cache are removed

Definition at line 165 of file cache.c.

{
#ifdef USE_GNET
  g_hash_table_foreach(hash_cache,
                     (GHFunc) _clear_cache,
                     NULL);

#endif
}
void gc_cache_end ( )

End the cache system.

Definition at line 139 of file cache.c.

{
#ifdef USE_GNET

  if(!hash_cache)
    return;

  g_hash_table_destroy (hash_cache);
  hash_cache = NULL;
#endif
}
gchar* gc_cache_get ( gchar *  url)

Get a file from the cache based on it's URL.

Definition at line 178 of file cache.c.

{
#ifdef USE_GNET
  return((char *)g_hash_table_lookup(hash_cache, url));
#else
  return NULL;
#endif
}
void gc_cache_init ( int  max_size)

Initialize the cache system.

cache

Parameters:
max_size,:the max size in byte of the cache

Definition at line 108 of file cache.c.

{
#ifdef USE_GNET
  cache_max_size = max_size;

  /* No server defined, the cache is useless */
  if(!gc_prop_get()->server ||
     !gc_prop_get()->cache_dir)
    return;

  hash_cache = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);

  printf("gc_cache_init\n");

  /* try to create it */
  printf("Creating cache dir %s\n", gc_prop_get()->cache_dir);
  if(g_mkdir_with_parents(gc_prop_get()->cache_dir, 0755))
    {
      g_error("Failed to create the cache directory");
    }
  printf("  opened top directory\n");
  /* Load the previous cache directory if any */
  _cache_init(gc_prop_get()->cache_dir, NULL);

  dump_cache();
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

gchar* gc_cache_insert ( const gchar *  url,
const char *  buffer,
gssize  length 
)

Put and Get a file from the cache.

The data in 'buffer' are saved in the cache under the name 'url'. A file is created and is returned.

Parameters:
url,:the url of the data in buffer
buffer,:the data for the url
length,:length of contents, or -1 if contents is a nul-terminated string
Returns:
a full path to the file in the cache

Definition at line 196 of file cache.c.

{
#ifdef USE_GNET
  /* Save the buffer in the cache */
  if(g_file_set_contents("TBD", buffer, length, NULL))
    g_hash_table_replace(hash_cache, (gpointer) url, (gpointer) "TBD");
#endif
  return("TBD");
}