record_stats.c

Display summary information about the number of records found

00001 
00004 /*
00005  * Copyright (C) 2004-2006 Mike Frysinger <vapier@gmail.com>
00006  * Released under the BSD license.  For more information,
00007  * please see: http://opensource.org/licenses/bsd-license.php
00008  *
00009  * $Header: /cvsroot/freestdf/libstdf/examples/record_stats.c,v 1.11 2005/11/13 08:56:58 vapier Exp $
00010  */
00011 
00012 #include <libstdf.h>
00013 
00014 /*
00015  * libhash
00016  */
00017 #if HAVE_HASH_H
00018 #include <hash.h>
00019 #define HASH_VARS \
00020     void *key, *val; \
00021     hash hash_table; \
00022     struct hash_iterator hit;
00023 #define HASH_INIT \
00024     hash_initialise(&hash_table, 61, \
00025                     hash_hash_string, hash_compare_string, hash_copy_string, \
00026                     free, free);
00027 #define HASH_UPDATE \
00028     key = (void*)recname; \
00029     if (!hash_retrieve(&hash_table, key, (void**)&val)) { \
00030         val = (void*)malloc(sizeof(long)); \
00031         *(long*)val = 0; \
00032         hash_insert(&hash_table, key, val); \
00033     } \
00034     (*(long*)val)++;
00035 #define HASH_PRINT \
00036     key = NULL; \
00037     hash_iterator_initialise(&hash_table, &hit); \
00038     while (hash_fetch_next(&hash_table, &hit, (void **)&key, (void **)&val)) \
00039         printf("\t%s : %li\n", (char*)key, *(long*)val); \
00040     hash_iterator_deinitialise(&hash_table, &hit); \
00041     hash_deinitialise(&hash_table);
00042 
00043 /*
00044  * Ecore
00045  */
00046 #elif HAVE_ECORE
00047 #include <Ecore.h>
00048 #define HASH_VARS \
00049     Ecore_Hash *hash_table; \
00050     long *stat;
00051 #define HASH_INIT \
00052     hash_table = ecore_hash_new(ecore_str_hash, ecore_str_compare);
00053 #define HASH_UPDATE \
00054     stat = ecore_hash_get(hash_table, recname); \
00055     if (!stat) { \
00056         stat = (long*)malloc(sizeof(long)); \
00057         *stat = 0; \
00058         ecore_hash_set(hash_table, strdup(recname), stat); \
00059     } \
00060     (*stat)++;
00061 #define HASH_PRINT \
00062     ecore_hash_for_each_node(hash_table, print_stat, NULL); \
00063     ecore_hash_destroy(hash_table);
00064 void print_stat(void *value, void *user_data)
00065 {
00066     Ecore_Hash_Node *node = ECORE_HASH_NODE(value);
00067     printf("\t%s : %li\n", (char*)(node->key), *((long*)(node->value)));
00068 }
00069 
00070 /*
00071  * glib
00072  */
00073 #elif HAVE_GLIB
00074 #include <glib.h>
00075 #define HASH_VARS \
00076     GHashTable *hash_table; \
00077     long *stat;
00078 #define HASH_INIT \
00079     hash_table = g_hash_table_new(g_str_hash, g_str_equal);
00080 #define HASH_UPDATE \
00081     stat = g_hash_table_lookup(hash_table, recname); \
00082     if (!stat) { \
00083         stat = (long*)malloc(sizeof(long)); \
00084         *stat = 0; \
00085         g_hash_table_insert(hash_table, g_strdup(recname), stat); \
00086     } \
00087     (*stat)++;
00088 #define HASH_PRINT \
00089     g_hash_table_foreach(hash_table, print_stat, NULL); \
00090     g_hash_table_destroy(hash_table);
00091 void print_stat(gpointer key, gpointer value, gpointer user_data)
00092 {
00093     printf("\t%s : %li\n", (char*)key, *((long*)value));
00094 }
00095 
00096 #else
00097 #define HASH_VARS
00098 #define HASH_INIT
00099 #define HASH_UPDATE
00100 #define HASH_PRINT
00101 #endif
00102 
00103 int main(int argc, char *argv[])
00104 {
00105     stdf_file *f;
00106     char *recname;
00107     rec_unknown *rec;
00108     long cnt;
00109     int i;
00110     HASH_VARS
00111 
00112     if (argc <= 1) {
00113         printf("Need some files to open!\n");
00114         return EXIT_FAILURE;
00115     }
00116 
00117 for (i=1; i<argc; ++i) {
00118     printf("Analyzing %s\n", argv[i]);
00119     f = stdf_open(argv[i]);
00120     if (!f) {
00121         perror("Could not open file");
00122         continue;
00123     }
00124 
00125     HASH_INIT
00126     cnt = 0;
00127     while ((rec=stdf_read_record(f)) != NULL) {
00128         recname = stdf_get_rec_name(rec->header.REC_TYP, rec->header.REC_SUB);
00129         HASH_UPDATE
00130         cnt++;
00131         stdf_free_record(rec);
00132     }
00133     stdf_close(f);
00134     HASH_PRINT
00135 
00136     printf("\tTOTAL : %li\n", cnt);
00137 }
00138     return EXIT_SUCCESS;
00139 }

Generated on Thu Jun 8 14:05:35 2006 for libstdf by  doxygen 1.4.6