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
[email protected]
http://lists.lugod.org/mailman/listinfo/vox-tech

Reply via email to