l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
December 2: Social gathering
Next Installfest:
TBD
Latest News:
Nov. 18: Club officer elections
Page last updated:
2010 Feb 13 17:36

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
[vox-tech] stupid hash doesn't work with libglib-2.0
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vox-tech] stupid hash doesn't work with libglib-2.0



I am trying to eliminate duplicates, so I figured I would just use a
hash from libglib-2.0. It worked fine with static pointers (or whatever
you call that portion oof code)

Problem is, once I try it with dynamically allocated pointers, it
doesn't eliminiate any duplicates. 

Anything obvious that I am missing?

Comple with the following.

gcc `pkg-config glib-2.0 --cflags` read_hash.c -o read_hash

brian
-- 
Brian Lavender
http://www.brie.com/brian/

"About 3 million computers get sold every year in China, but people don't
pay for the software. Someday they will, though. As long as they are going
to steal it, we want them to steal ours. They'll get sort of addicted, and
then we'll somehow figure out how to collect sometime in the next decade."

-- Bill Gates (Microsoft) 1998
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#define BUF_SZ 80

void printeach(gpointer a, gpointer b, gpointer userdata) {
 
  g_printf("%s Key is %d, value is %d\n",(char *)userdata,*(int *)a, *(int *)b);
}


gboolean compare2(int *a, int *b)
{
  gboolean rvalue;
  printf("a is *%d* b is *%d*\n", *a, *b);
  if (*a == *b)
    rvalue = TRUE;
  else
    rvalue = FALSE;
  return rvalue;
}

// foo - raw line for input string
// length - length of input string
// myind - pointer to individual to be created.
void make_toks(char *foo, int length, int *myind, GHashTable **myHTable)
{
  char *svptr1;
  char delims[] = " ";
  char *result = NULL;
  int idx = 0;
  int i;

  result = strtok_r( foo, delims, &svptr1 );
  i = atoi(result);
  printf("Got value %d\n",i);
  memcpy(myind, &i, sizeof(int));
  g_hash_table_insert(*myHTable, myind, myind);

}


int load_audit(GSList **auditList, char *myfile, GHashTable **myHTable)
{
  FILE *input; // input file
  char *rd_buf; // buffer for reading input
  int nchars; // number of characters read
  int nRecords = 0;
  size_t cur_sz = BUF_SZ;
  int *a;

  // Empty the list
  if (*auditList != NULL)
    {
      g_slist_free(*auditList);
      *auditList = NULL;
    }

  // Open the file
  //g_printf("file to open %s\n",myfile);

  input = fopen(myfile, "r");
  if (input == NULL)
    {
      perror("Failed to open file");
      exit(-1);
    }
  // buffer for reading input
  rd_buf = (char *) malloc( BUF_SZ * sizeof(char) );

  nchars = getline(&rd_buf, &cur_sz, input);

  while (nchars != -1) {
    a = (int *)g_slice_alloc0(sizeof(int));
    if ( rd_buf[nchars-1] == '\n')
      rd_buf[nchars-1] = '\0'; // get rid of newline. Probably won't work on PC though \n\r
    make_toks(rd_buf, nchars, a, myHTable);
    *auditList = g_slist_append(*auditList, a);
    nRecords++;
    nchars = getline(&rd_buf, &cur_sz, input);
  }

  free(rd_buf);

  if (fclose(input) != 0) {
    perror("Failed to close");
    exit(-1);
  }
  return nRecords;
}

int main() {
  GHashTable *myHTable;
  GSList *auditData = NULL;
  GSList *iterator = NULL;

  int nRecords = 0;
  int *myInt;

  char *myfile = "./myints.txt";

  myHTable = g_hash_table_new(NULL,(GEqualFunc)compare2);

  nRecords = load_audit(&auditData, myfile, &myHTable);

  g_printf("Read %d records\n",nRecords);


  //print the list data

  for (iterator = auditData; iterator; iterator = iterator->next) {
    myInt = (int*)iterator->data;
    g_printf("got this %d\n", *myInt);

    }
  g_hash_table_foreach(myHTable, printeach, "First list");

}
1
4
56
32
12
1
4
56
56
12
_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
O'Reilly and Associates
For numerous book donations.