Function to find number of files in a directory

Thread Solved

Join Date: Nov 2008
Posts: 40
Reputation: Creator07 is an unknown quantity at this point 
Solved Threads: 2
Creator07 Creator07 is offline Offline
Light Poster

Function to find number of files in a directory

 
0
  #1
May 14th, 2009
Hello guys,

I am doing a small C/C++ program(mainly for use in my father's work - Includes a batch processing inside a folder which is repetitive).

I just need to find the number of files in a folder(not necessarily of a single filetype). What I thought was using system("dir > TmpFile") , and checking in that file. The code for checking in this goes quite complex.

Can somebody suggest me a simple function(NOT MFC), to do count no. of files in a given folder.

- Appreciate your help,
Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Function to find number of files in a directory

 
1
  #2
May 14th, 2009
Read here http://www.adrianxw.dk/SoftwareSite/...irstFile1.html (and part 2 and 3), and in the retrieving loop you could simply have a counter that's returned by the function.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 40
Reputation: Creator07 is an unknown quantity at this point 
Solved Threads: 2
Creator07 Creator07 is offline Offline
Light Poster

Re: Function to find number of files in a directory

 
0
  #3
May 14th, 2009
Well! Thanks. I was trying to avoid windows.h, though... But, I think, that is fine for now. Thanks for the link twomers.
Rep+ for you.
Have a nice day!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 118
Reputation: marco93 is infamous around these parts marco93 is infamous around these parts marco93 is infamous around these parts 
Solved Threads: 12
marco93 marco93 is offline Offline
Junior Poster

Re: Function to find number of files in a directory

 
-1
  #4
May 14th, 2009
This code is wrong (no recursive, no reparse points, etc)
See the official MS sample (SDK)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,834
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Function to find number of files in a directory

 
0
  #5
May 14th, 2009
Originally Posted by marco93 View Post
This code is wrong (no recursive, no reparse points, etc)
See the official MS sample (SDK)
If you're going to call the code 'wrong', do you mind giving an example?
To be honest Marco, your posts are getting a bit boring. You always post something like: 'This sux, use win32 API' and sometimes you add a link to somesort of crappy newsgroup.
Don't call something wrong if you can't explain why.
Last edited by niek_e; May 14th, 2009 at 9:24 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: varnesh_kp is an unknown quantity at this point 
Solved Threads: 0
varnesh_kp varnesh_kp is offline Offline
Newbie Poster

SearchCode

 
0
  #6
Oct 16th, 2009
  1. /* SearchEngine.cpp : Creating Search Index and perform searching with the given
  2. input term(S) */
  3.  
  4. #include <stdio.h>
  5. #include <windows.h>
  6. #include <string.h>
  7. #include <conio.h>
  8. #include <stdlib.h>
  9. #define HASHSIZE 101
  10.  
  11. typedef struct _Document Document;
  12. typedef struct _WordList WordList;
  13. typedef struct _Position Position;
  14.  
  15.  
  16. /* Hold positions of words in file */
  17. struct _Position {
  18. long pos;
  19. int flag;
  20. Position *next;
  21. };
  22.  
  23. /* Hold list of Documents */
  24. struct _Document {
  25. char *docname;
  26. int count;
  27. Position *wordposition;
  28. Document *next;
  29. };
  30.  
  31.  
  32.  
  33. /* Hold list of words */
  34. struct _WordList {
  35. char *name;
  36. WordList *next;
  37. Document *doc;
  38.  
  39. };
  40. WordList* wordTable[HASHSIZE];
  41. int main(int argc, char * argv[]) {
  42.  
  43. char *dirName;
  44. char key[256];
  45.  
  46.  
  47. void performDirectoryScan(char *dirName);
  48. void createSearchIndex(char *dirName,char *fileName);
  49. void initWordTable();
  50. unsigned int hash(char *word);
  51. WordList* lookup(char *word);
  52. char* getStringDuplicate(char *words);
  53. char* getNextWord(char *key);
  54. Document* getNewDocument(TCHAR *fileName);
  55. Document* getDocumentList(char *word);
  56. void insertToWordTable(char* name,TCHAR *filename);
  57. void insertWordPosition(Document* docName,long wordpos);
  58. void findWordPosition(Position* fposList,Position *tposList);
  59. void processDocumentList(Document *fdocList,char *word);
  60. void printDocumentList(Document *docList);
  61.  
  62. void doSearch(char *key);
  63. void displayTable();
  64. void cleanup();
  65.  
  66. dirName=(char *)malloc((strlen(argv[1])+ 1) );
  67. strcpy(dirName,argv[1]);
  68. strcat(dirName,"*.*");
  69. initWordTable();
  70. performDirectoryScan(dirName);
  71. displayTable();
  72. printf("\n Enter Search term :");
  73. scanf("%s",key);
  74. doSearch(key);
  75. cleanup(wordTable);
  76. getch();
  77. return 0;
  78. }
  79.  
  80. /* Initialize the word table */
  81.  
  82. void initWordTable() {
  83. int i;
  84. for(i=0;i<HASHSIZE;i++)
  85. wordTable[i]=NULL;
  86. }
  87.  
  88.  
  89. /* return hash value */
  90.  
  91. unsigned int hash(char words[]){
  92. unsigned int h=0;
  93. int i;
  94. for( i=0 ; words[i] != NULL ; i++)
  95. h=words[i]+h*31;
  96. return h%HASHSIZE;
  97. }
  98.  
  99. char* getStringDuplicate(char *words){
  100. int l=strlen(words)+1;
  101. char *dupStr=(char*)malloc(l*sizeof(char));
  102. strcpy(dupStr,words);
  103. if(dupStr==NULL)
  104. return NULL;
  105. else
  106. return dupStr;
  107. }
  108.  
  109. /* Scanning all files from Directory */
  110.  
  111. void performDirectoryScan(char *dirName)
  112. {
  113.  
  114.  
  115. WIN32_FIND_DATA info;
  116. int i=0;
  117. HANDLE h = FindFirstFile(dirName,&info);
  118. if (h == INVALID_HANDLE_VALUE)
  119. {
  120. return;
  121. }
  122. do
  123. {
  124. if (strcmp(info.cFileName, ".") != 0 && strcmp(info.cFileName, "..") != 0)
  125. {
  126. createSearchIndex(dirName,info.cFileName,h);
  127. }
  128. } while (FindNextFile(h, &info));
  129.  
  130. FindClose(h);
  131. }
  132.  
  133. /* Creating Indexer using words scanned */
  134.  
  135. void createSearchIndex(char *dirName,char *fileName,HANDLE h) {
  136.  
  137. WordList *wl;
  138. FILE *inputFile;
  139. char ch,*words;
  140. int i=0,j=0;
  141. int length=0;
  142. long wordpos;
  143.  
  144. wl=(WordList *)malloc(sizeof(WordList));
  145. words=(char *)malloc((GetFileSize(h,fileName))*sizeof (char));
  146. strcat(dirName,fileName);
  147. inputFile = fopen(dirName,"r");
  148. if (inputFile==NULL) {
  149. printf("File does not exist");
  150. return;
  151. }
  152. ch = fgetc (inputFile);
  153. while (ch != EOF) {
  154. wordpos=ftell(inputFile);
  155. *(words+i) = ch;
  156. if(ch == ' ') {
  157. *(words+i) = '\0';
  158. wordpos=(wordpos-strlen(words)) + 1;
  159. insertToWordTable(wordTable,&words,wordpos,fileName);
  160. i=0;
  161. j++;
  162. length++;
  163. }
  164. ch = fgetc(inputFile);
  165. }
  166. insertToWordTable(&words,fileName);
  167. fclose (inputFile);
  168. }
  169.  
  170.  
  171.  
  172.  
  173. /* Look up routine to scan words that are hashed */
  174.  
  175. WordList* lookup(char *words){
  176. unsigned int hi=hash(words);
  177. WordList *wl=wordTable[hi];
  178. for ( ; wl!=NULL ; wl=wl->next ) {
  179. if(!strcmp(wl->name,words))
  180. return wl;
  181. }
  182. wl=(WordList*)malloc(sizeof(WordList));
  183. if (wl==NULL) {
  184. printf("\n Cannot allocate memory for wordlist \n");
  185. return;
  186. }
  187. wl->name=getStringDuplicate(words);
  188. wl->doc=NULL;
  189. wl->next=wordTable[hi];
  190. wordTable[hi]=wl;
  191. return wl;
  192. }
  193.  
  194. /* Returns new position of given word */
  195.  
  196. void insertWordPosition(Document* docName,long wordpos) {
  197. Position *curpos,*newpos;
  198. newpos=(Position *)malloc(sizeof(Position));
  199. newpos->pos=wordpos;
  200. newpos->flag=0;
  201. newpos->next=NULL;
  202. curpos=docName->wordposition;
  203. while(curpos->next != NULL) {
  204. curpos=curpos->next;
  205. }
  206. curpos->next=newpos;
  207.  
  208. }
  209.  
  210. /* returns a new document */
  211.  
  212. Document* getNewDocument(char *fileName) {
  213.  
  214. char *str;
  215. Document *newdoc;
  216. newdoc=(Document *)malloc(sizeof (Document) );
  217. newdoc->count=0;
  218. newdoc->docname=(char *)malloc(sizeof (char));
  219. strcpy(newdoc->docname,fileName);
  220. newdoc->count++;
  221. newdoc->wordposition=NULL;
  222. newdoc->next=NULL;
  223. return newdoc;
  224. }
  225.  
  226.  
  227. /* Insert the term into hashtable based on hashvalue */
  228.  
  229. void insertToWordTable(char **words,long wordpos,char *fileName){
  230. unsigned int hi;
  231. WordList* wl;
  232. char *str;
  233. Position *temppos,*newpos;
  234. Document *tempdoc,*newdoc,*prevdoc,*curdoc;
  235. wl=lookup(*words,wordTable);
  236. curdoc=wl->doc;
  237. prevdoc=curdoc;
  238. temppos=curdoc->wordposition;
  239. while ( curdoc != NULL ) {
  240. if(wcscmp(curdoc->docname,fileName)==NULL){
  241. curdoc->count++;
  242. insertWordPosition(curdoc,wordpos);
  243. return;
  244. }
  245. prevdoc=curdoc;
  246. curdoc=curdoc->next;
  247. }
  248. newdoc=getNewDocument(fileName);
  249. if(wl->doc==NULL){
  250. wl->doc=newdoc;
  251. return;
  252. }
  253. prevdoc->next=newdoc;
  254. insertWordPosition(newdoc,wordpos);
  255. return;
  256.  
  257. }
  258.  
  259. /* Display Contents of the table */
  260.  
  261. void displayTable(){
  262. int i;
  263. WordList *wl;
  264. Document *tempdoc;
  265. printf("\nWords In the Hash Table are:\n");
  266.  
  267. for ( i=0 ; i<HASHSIZE ; i++){
  268.  
  269. wl=wordTable[i];
  270. for ( ; wl!=NULL ; wl=wl->next ) {
  271. printf("\n \nWord :%s \n",wl->name);
  272. for ( tempdoc=wl->doc ; tempdoc!=NULL ; tempdoc=tempdoc->next ) {
  273.  
  274. printf("\nDocument name is: %s",tempdoc->docname);
  275. printf("\n Count : %d",tempdoc->count);
  276. }
  277. }
  278. }
  279. }
  280.  
  281. /* get all the words from input key */
  282.  
  283. char* getNextWord(char *key) {
  284. char *words;
  285. static int i=0;
  286. int j;
  287. words=(char *) malloc (sizeof (char));
  288. for (j=0; *(key+i) != NULL ; i++,j++ ) {
  289. *(words+j)=*(key+i);
  290. if(*(key+i) == ' ') {
  291. *(words+j)='\0';
  292. i++;
  293. return words;
  294. }
  295. }
  296. *(words+j)='\0';
  297. return words;
  298. }
  299.  
  300. /* get document list for the given word */
  301.  
  302. Document* getDocumentList(char *word) {
  303.  
  304. WordList *wl;
  305. wl=lookup(word,wordTable);
  306. if(wl!=NULL)
  307. return wl->doc;
  308. }
  309.  
  310.  
  311. /* comparisons of positions for successful search */
  312. void findWordPosition(Position* fposList,Position *tposList) {
  313. Position *firstList,*secondList;
  314. long pos;
  315. for (firstList=fposList ; firstList != NULL ; firstList=firstList->next) {
  316. secondList=tposList;
  317. pos=firstList->pos;
  318. for ( ; firstList->pos <= secondList->pos ; secondList=secondList->next) {
  319. pos++;
  320. if ( pos == secondList->pos ) {
  321. firstList->flag=1;
  322. }
  323. }
  324. }
  325. }
  326.  
  327. /* process documents for position of the given input terms */
  328. void processDocumentList(Document *fdocList,char *word) {
  329.  
  330. Document *ndocList,*tdocList;
  331. Position *nposList,*tposList;
  332. long *keyposList;
  333. tdocList=fdocList;
  334. ndocList=getDocumentList(word,wordTable);
  335. for(; ndocList->next != NULL ;ndocList=ndocList->next) {
  336.  
  337. if (wcscmp(tdocList->docname,ndocList->docname) == 0) {
  338. tposList=tdocList->wordposition;
  339. nposList=ndocList->wordposition;
  340. findWordPosition(tposList,nposList);
  341.  
  342. }
  343. }
  344. }
  345.  
  346. /* print all the successful documents */
  347. void printDocumentList(Document *docList) {
  348.  
  349. Document *tempdoc;
  350. Position *posList;
  351. tempdoc=docList;
  352. for (; tempdoc != NULL ; tempdoc=tempdoc->next) {
  353. posList=docList->wordposition;
  354. for (; posList!=NULL ; posList=posList->next) {
  355. if (posList->flag==1)
  356. printf("\n\n%S Pos : %d\n\n",docList->docname,posList->pos);
  357. }
  358. }
  359. }
  360.  
  361.  
  362. /* perform search based on key term(s) given */
  363. void doSearch(char *key) {
  364.  
  365. Document *fdocList,*tempdocList;
  366. int i,j=0,k=0,wcount=0,doccount=0,pcount=0;
  367. char *word;
  368. word=key;
  369. while (!*word) {
  370. if(*word==' ')
  371. wcount++;
  372. }
  373. wcount++;
  374. word=getNextWord(key);
  375. fdocList=getDocumentList(word,wordTable);
  376. tempdocList=fdocList;
  377. if ( tempdocList == NULL ) {
  378. printf("No such term in any document\n");
  379. return ;
  380. }
  381. for (; tempdocList->next != NULL ; tempdocList=tempdocList->next ) {
  382. for ( i=1; i<wcount; i++ ) {
  383. word=getNextWord(key);
  384. processDocumentList(tempdocList,word,wordTable);
  385. }
  386. }
  387. printDocumentList(fdocList);
  388. }
  389.  
  390.  
  391. /* free hash table entries */
  392.  
  393. void cleanup(){
  394. int i;
  395. WordList *wl,*temp;
  396. for (i=0; i<HASHSIZE; i++) {
  397. if(wordTable[i]!=NULL){
  398. wl=wordTable[i];
  399. while(wl!=NULL){
  400. temp=wl->next;
  401. free(wl);
  402. wl=temp;
  403. }
  404. }
  405. }
  406. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 118
Reputation: marco93 is infamous around these parts marco93 is infamous around these parts marco93 is infamous around these parts 
Solved Threads: 12
marco93 marco93 is offline Offline
Junior Poster
 
-1
  #7
Oct 16th, 2009
Originally Posted by niek_e View Post
If you're going to call the code 'wrong', do you mind giving an example?
To be honest Marco, your posts are getting a bit boring. You always post something like: 'This sux, use win32 API' and sometimes you add a link to somesort of crappy newsgroup.
Don't call something wrong if you can't explain why.
You're a complete noob. (Probably very , very young.. )
Newsgroups (Usenet) is by far the best place on the Web, for more than 20 years !
There are the greatest Win32 gurus, MS consultants, some of Windows creators, and best-sellers authors in the world (you can even find Bjarne Stroustrup himself...)
Aaaaaaah... those kids who know nothing...
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: mika_ is an unknown quantity at this point 
Solved Threads: 0
mika_ mika_ is offline Offline
Newbie Poster
 
-1
  #8
Oct 16th, 2009
Originally Posted by marco93 View Post
Newsgroups (Usenet) is by far the best place on the Web, for more than 20 years !
There are the greatest Win32 gurus, MS consultants, some of Windows creators, and best-sellers authors in the world (you can even find Bjarne Stroustrup himself...)

+ 1000 !
(and Charles Petzold, but this newbie certainly even don't know him...)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,407
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso
 
0
  #9
Oct 17th, 2009
You're a complete noob. (Probably very , very young.. )
Newsgroups (Usenet) is by far the best place on the Web, for more than 20 years !
There are the greatest Win32 gurus, MS consultants, some of Windows creators, and best-sellers authors in the world (you can even find Bjarne Stroustrup himself...)
Aaaaaaah... those kids who know nothing...
I've come to the conclusion that both you and mika_ are the same person.

Aaaaaaah... those kids who know nothing...
You know nothing, all you know how to do is link to crappy newsgroups, and every last one of us looks forward to the day that you leave, nobody wants you here.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: varnesh_kp is an unknown quantity at this point 
Solved Threads: 0
varnesh_kp varnesh_kp is offline Offline
Newbie Poster

VarCode

 
-1
  #10
Oct 19th, 2009
/* SearchEngine.cpp : Creating Search Index and perform searching with the given
input term(S) */

#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <limits.h>

#define HASHSIZE 101

typedef struct _Document Document;
typedef struct _WordList WordList;
typedef struct _Position Position;


/* Hold positions of words in file */
struct _Position {
long pos;
int flag;
Position *next;
};

/* Hold list of Documents */
struct _Document {
TCHAR *docname;
int count;
Position *wordposition;
Document *next;
};



/* Hold list of words */
struct _WordList {
char *name;
WordList *next;
Document *doc;

};

int main(int argc, char * argv[]) {

char *dirName;
char key[256];
WordList* wordTable[HASHSIZE];

void performDirectoryScan(char *dirName,WordList* wordTable[]);
void createSearchIndex(WordList* wordTable[],char *dirName,char *fileName,HANDLE h);
void initWordTable(WordList* wordTable[]);
unsigned int hash(char *word);
WordList* lookup(WordList* wordTable[],char *word);
char* getStringDuplicate(char *words);
char* getNextWord(char *key);
WordList* getNewWordList(char *words);
Document* getNewDocument(char *fileName,long wordpos);
Document* getDocumentList(char *word);
void insertToWordTable(WordList* wordTable[],char **words,long wordpos,char *fileName);
void insertWordPosition(Document* docName,long wordpos);
void findWordPosition(Position* fposList,Position *tposList);
void processDocumentList(Document *fdocList,char *word);
void printDocumentList(Document *docList);
void doSearch(WordList* wordTable[],char *key);
void displayTable(WordList* wordTable[]);
void cleanup(WordList* wordTable[]);

dirName=(char *)malloc((strlen(argv[1])+ 1)* sizeof(char));
strcpy(dirName,argv[1]);
initWordTable(&wordTable);
performDirectoryScan(dirName,wordTable);
displayTable(wordTable);
printf("\n Enter Search term :");
scanf("%s",key);
doSearch(wordTable,key);
cleanup(wordTable);

getch();
return 0;
}

/* Initialize the word table */

void initWordTable(WordList* wordTable[]) {
int i;
for(i=0;i<HASHSIZE;i++) {
wordTable[i]=NULL;
}
}


/* return hash value */

unsigned int hash(char words[]){
unsigned int h=0;
int i;
for( i=0 ; words[i] != NULL ; i++)
h=words[i]+h*31;
return h%HASHSIZE;
}

/* return duplicate string */

char* getStringDuplicate(char *words){
int l=strlen(words)+1;
char *dupStr=(char*)malloc(l*sizeof (char) );
strcpy(dupStr,words);
if(dupStr==NULL)
return NULL;
else
return dupStr;
}

/* Scanning all files from Directory */

void performDirectoryScan(char *dirName,WordList* wordTable[])
{

WIN32_FIND_DATA info;
HANDLE h;
char *dirPath;
int i=0;
dirPath=(char *)malloc((strlen(dirName)+4)*sizeof (char));
strcpy(dirPath,dirName);
strcat(dirPath,"*.*");
h = FindFirstFile(dirPath,&info);
if (h == INVALID_HANDLE_VALUE)
{
return;
}
do
{
if (strcmp(info.cFileName, ".") != 0 && strcmp(info.cFileName, "..") != 0)
{
createSearchIndex(wordTable,dirName,info.cFileName,h);
}
} while (FindNextFile(h, &info));

FindClose(h);
}

/* Creating Indexer using words scanned */

void createSearchIndex(WordList* wordTable[],char *dirName,TCHAR *fileName,HANDLE h) {

FILE *inputFile;
char ch,*words;
int i=0,j=0;
int length=0;
long wordpos;

words=(char *)malloc(1000 * sizeof (char));
strcat(dirName,fileName);
inputFile = fopen(dirName,"r");
if (inputFile==NULL) {
printf("File does not exist");
return;
}
ch = fgetc (inputFile);
while (ch != EOF) {
words[i++] = ch;
wordpos=ftell(inputFile);
if(ch == ' ') {
words[--i]='\0';
wordpos=(wordpos-strlen(words)) + 1;
insertToWordTable(wordTable,words,wordpos,fileName);
i=0;
}
ch = fgetc(inputFile);
}
words[--i]='\0';
insertToWordTable(wordTable,words,wordpos,fileName);
free(words);
fclose (inputFile);
}

/* create a new world list */
WordList* getNewWordList(char *words) {

char *name;
Document *newdoc;
WordList *wl;
Position *newpos;
newpos=(Position *)malloc(sizeof (Position));
newdoc=(Document *)malloc(sizeof (Document));
wl=(WordList*)malloc(sizeof (WordList));
newpos->pos=0;
newpos->flag=0;
newpos->next=NULL;
newdoc->count=0;
newdoc->docname=(TCHAR *)malloc(sizeof(TCHAR));
newdoc->wordposition=newpos;
newdoc->next=NULL;
wl->name=getStringDuplicate(words);
wl->doc=newdoc;
wl->next=newdoc;
return wl;
}

/* Look up routine to scan words that are hashed */

WordList* lookup(char *words,WordList* wordTable[]){
unsigned int hi=hash(words);
WordList* wl;
wl=wordTable[hi];
for ( ; wl!=NULL ; wl=wl->next ) {
if(!strcmp(wl->name,words))
return wl;
}
wl=getNewWordList(words);
wl->next=wordTable[hi];
wordTable[hi]=wl;
return wl;
}

/* Returns new position of given word */

void insertWordPosition(Document* docName,long wordpos) {
Position *curpos,*newpos;
newpos=(Position *)malloc(sizeof(Position));
if (newpos != NULL) {
newpos->pos=wordpos;
newpos->flag=0;
newpos->next=NULL;
}
curpos=docName->wordposition;
while(curpos->next != NULL) {
curpos=curpos->next;
}
curpos->next=newpos;

}

/* returns a new document */

Document* getNewDocument(TCHAR *fileName,long wordpos) {

char *str;
Document *newdoc;
Position *newpos;
newdoc=(Document *)malloc(sizeof (Document) );
newpos=(Position *)malloc(sizeof (Position) );
newdoc->count=0;
newdoc->docname=(TCHAR *)malloc(sizeof (TCHAR));
wcscpy(newdoc->docname,fileName);
newdoc->count++;
newpos->pos=wordpos;
newpos->flag=0;
newpos->next=NULL;
newdoc->wordposition=newpos;
newdoc->next=NULL;
return newdoc;
}


/* Insert the term into hashtable based on hashvalue */

void insertToWordTable(WordList* wordTable[],char *words,long wordpos,TCHAR *fileName){
unsigned int hi;
WordList* wl;
Position *temppos,*newpos;
Document *tempdoc,*newdoc,*prevdoc,*curdoc;

wl=lookup(words,wordTable);
curdoc=wl->doc;
prevdoc=curdoc;
while ( curdoc != NULL ) {
if ( wcscmp(curdoc->docname,fileName)==NULL ){
curdoc->count++;
insertWordPosition(curdoc,wordpos);
return;
}
prevdoc=curdoc;
curdoc=curdoc->next;
}
newdoc=getNewDocument(fileName,wordpos);
if ( wl->doc==NULL ){
wl->doc=newdoc;
return;
}
prevdoc->next=newdoc;

return;

}

/* Display Contents of the table */

void displayTable(WordList* wordTable[]){
int i;
WordList* wl;
Document *tempdoc;
printf("\nWords In the Hash Table are:\n");

for ( i=0 ; i<HASHSIZE ; i++){

wl=wordTable[i];
for ( ; wl!=NULL ; wl=wl->next ) {
printf("\n \nWord :%s \n",wl->name);
for ( tempdoc=wl->doc ; tempdoc!=NULL ; tempdoc=tempdoc->next ) {

printf("\nDocument name is: %S",tempdoc->docname);
printf("\n Count : %d",tempdoc->count);
}
}
}
}

/* get all the words from input key */

char* getNextWord(char *key) {
char *words;
static int i=0;
int j;
words=(char *) malloc (sizeof (char));
for (j=0; *(key+i) != NULL ; i++,j++ ) {
*(words+j)=*(key+i);
if(*(key+i) == ' ') {
*(words+j)='\0';
i++;
return words;
}
}
*(words+j)='\0';
return words;
}

/* get document list for the given word */

Document* getDocumentList(WordList* wordTable[],char *word) {

WordList *wl;
wl=lookup(word,wordTable);
if(wl!=NULL)
return wl->doc;
}


/* comparisons of positions for successful search */
void findWordPosition(Position* fposList,Position *tposList) {
Position *firstList,*secondList;
long pos;
for (firstList=fposList ; firstList != NULL ; firstList=firstList->next) {
secondList=tposList;
pos=firstList->pos;
for ( ; firstList->pos <= secondList->pos ; secondList=secondList->next) {
pos++;
if ( pos == secondList->pos ) {
firstList->flag=1;
}
}
}
}

/* process documents for position of the given input terms */
void processDocumentList(Document *fdocList,char *word,WordList wordTable[]) {

Document *ndocList,*tdocList;
Position *nposList,*tposList;
long *keyposList;
tdocList=fdocList;
ndocList=getDocumentList(wordTable,word);
for(; ndocList->next != NULL ;ndocList=ndocList->next) {

if (wcscmp(tdocList->docname,ndocList->docname) == 0) {
tposList=tdocList->wordposition;
nposList=ndocList->wordposition;
findWordPosition(tposList,nposList);

}
}
}

/* print all the successful documents */
void printDocumentList(Document *docList) {

Document *tempdoc;
Position *posList;
tempdoc=docList;
for (; tempdoc != NULL ; tempdoc=tempdoc->next) {
posList=docList->wordposition;
for (; posList!=NULL ; posList=posList->next) {
if (posList->flag==1)
printf("\n\n%S Pos : %d\n\n",docList->docname,posList->pos);
}
}
}


/* perform search based on key term(s) given */
void doSearch(WordList* wordTable[],char *key) {

Document *fdocList,*tempdocList;
int i,j=0,k=0,wcount=0,doccount=0,pcount=0;
char *word;
word=key;
while (!*word) {
if(*word==' ')
wcount++;
}
wcount++;
word=getNextWord(key);
fdocList=getDocumentList(wordTable,word);
tempdocList=fdocList;
if ( tempdocList == NULL ) {
printf("No such term in any document\n");
return ;
}
for (; tempdocList->next != NULL ; tempdocList=tempdocList->next ) {
for ( i=1; i<wcount; i++ ) {
word=getNextWord(key);
processDocumentList(tempdocList,word,wordTable);
}
}
printDocumentList(fdocList);
}


/* free hash table entries */

void cleanup(WordList* wordTable[]){
int i;
WordList *wl,*temp;
for (i=0; i<HASHSIZE; i++) {
if(wordTable[i]!=NULL){
wl=wordTable[i];
while(wl!=NULL){
temp=wl->next;
free(wl);
wl=temp;
}
}
}
}
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC