I'd maintain some data structure that will count the number of appearances for each and every string added to it.
Here's some kind of linked list that counts number of insertions.
List.h:
Code:
#ifndef LIST_H_
#define LIST_H_
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define MAX_NAME 1024
typedef struct entry {
char key[MAX_NAME];
int value;
struct entry* next;
}entry_t;
typedef struct list {
entry_t* head;
entry_t* tail;
size_t size;
}list_t;
list_t* createList();
void addToList(list_t* list, const char* key);
void printList(list_t* list);
void destroyList(list_t* list);
#endif /* LIST_H_ */
List.c:
Code:
#include"List.h"
list_t* createList() {
list_t* new_list=(list_t*)malloc(sizeof(list_t));
new_list->size=0;
return new_list;
}
void addToList(list_t* list, const char* key) {
// first entry gets special treatment
if (!list->size) {
entry_t* new_entry=(entry_t*)malloc(sizeof(entry_t));
strncpy(new_entry->key,key, MAX_NAME);
new_entry->value=1;
new_entry->next=NULL;
list->head=new_entry;
list->tail=new_entry;
list->size++;
return;
}
entry_t* iterator=list->head;
do {
if (!strcmp(iterator->key, key)) {
iterator->value++;
return;
}
iterator=iterator->next;
} while (iterator);
// no match found, add new entry...
entry_t* new_entry=(entry_t*)malloc(sizeof(entry_t));
strncpy(new_entry->key, key, MAX_NAME);
new_entry->value=1;
new_entry->next=NULL;
list->tail->next=new_entry;
list->tail=list->tail->next;
list->size++;
return;
}
void printList(list_t* list) {
entry_t* iterator=list->head;
while (iterator) {
printf("key: %s\t\tvalue: %d\n", iterator->key, iterator->value);
iterator=iterator->next;
}
}
void destroyList(list_t* list) {
int i;
entry_t* temp;
for (i=0; i<list->size; ++i) {
temp=list->head;
list->head=list->head->next;
free(temp);
}
free(list);
}
And this is how you'd use it:
main.c:
Code:
#include<stdio.h>
#include"List.h"
int main() {
list_t* list=createList();
addToList(list, "file1");
addToList(list, "file2");
addToList(list, "file3");
addToList(list, "file2");
addToList(list, "file2");
addToList(list, "file3");
printList(list);
destroyList(list);
}
Output:
Code:
key: file1 value: 1
key: file2 value: 3
key: file3 value: 2
This can be also extended in order to support some more operations to your taste and needs.
If you decide to use it eventually, bare in mind that this was not extensively checked, debugged and tested, and was written willy nilly just to get you started.