erbrowser/src/indexFileHandler.cpp File Reference

#include <stdio.h>
#include "expat.h"
#include "browserTypes.h"
#include "indexFileHandler.h"
#include "browserLog.h"
#include <libermanifest/ermanifest.h>

Go to the source code of this file.

Defines

#define BUFFER_SIZE   256
#define MAX_LEN   1024
#define INDEX_DEBUG   0
#define TRACE(x...)   do {} while(0)

Functions

static int contain_url (const char *first, const char *second)
int get_pagelist_length (erManifest *manifest)
PageListItempageListItem_create (erManifest *manifest, const int index)
void pageInfo_release (PageInfo *pInfo)
void pageListItem_release (PageListItem *pItem)
void index_file_close (IndexInfo *index)
int index_file_init (const char *pathName, IndexInfo *pIndex)
int index_file_get_page_count (IndexInfo *index, const char *type)
int index_file_get_page_number (IndexInfo *index, const char *type, const char *url)
char * index_file_get_page_location (IndexInfo *index, const char *type, const int number)
void dump (IndexInfo *index)


Define Documentation

#define BUFFER_SIZE   256

Definition at line 37 of file indexFileHandler.cpp.

#define INDEX_DEBUG   0

Definition at line 39 of file indexFileHandler.cpp.

#define MAX_LEN   1024

Definition at line 38 of file indexFileHandler.cpp.

#define TRACE ( x...   )     do {} while(0)

Definition at line 44 of file indexFileHandler.cpp.


Function Documentation

static int contain_url ( const char *  first,
const char *  second 
) [static]

Definition at line 575 of file indexFileHandler.cpp.

00576 {
00577     if (NULL == first || NULL == second) return 1;
00578     int len1   = strlen(first), len2 = strlen(second);
00579     int min    = len1; 
00580     if (min > len2) min = len2;
00581     
00582     first += len1; second += len2;
00583     for(int i = 0; i <min; ++i)
00584     {
00585         if (*first-- != *second--) 
00586         {
00587             return 0;
00588         }
00589     }
00590     return 1;
00591 }

void dump ( IndexInfo index  ) 

Dump IndexInfo information, for debug

Parameters:
IndexInfo reference to the current inde file
Returns:
void

Definition at line 830 of file indexFileHandler.cpp.

00831 {
00832     if (NULL == index) return;
00833     
00834     printf("=======================Index file information=======================\n");
00835     printf("length %d\n", index->length);
00836     for(int i = 0; i <index->length; ++i)
00837     {
00838         PageListItem *pItem = index->pagelist[i];
00839         printf("\titem type: %s\tcount: %d\n", pItem->type, pItem->count);
00840         
00841         for(int j = 0; j < pItem->count; ++j)
00842         {
00843             PageInfo *pInf = pItem->page[j];
00844             printf("\t\tpage number: %d\turl: %s\n", pInf->number, pInf->url);
00845         }
00846     }
00847     printf("====================================================================\n");
00848 }    

int get_pagelist_length ( erManifest manifest  ) 

Definition at line 594 of file indexFileHandler.cpp.

00595 {
00596     char xpath[MAX_LEN]  = {0};
00597     char type[MAX_LEN]   = {0};
00598     int i = 0;
00599     int ret = RET_OK;
00600     while (1)
00601     {
00602         snprintf(xpath, MAX_LEN, "/package/index/pagelist[%d]", i + 1);
00603         ret = ermXmlGetAttributeString(manifest, xpath, "type", type, MAX_LEN);
00604         if (RET_ERR == ret) break;
00605         ++i;
00606     }
00607     return i;
00608 }

Here is the call graph for this function:

void index_file_close ( IndexInfo index  ) 

Close the index/manifest file

Parameters:
IndexInfo pointer
Returns:
-

Definition at line 678 of file indexFileHandler.cpp.

00679 {
00680     if (NULL == index)
00681     {
00682         TRACE("Invalid IndexInfo pointer!");
00683         return;
00684     }
00685     
00686     if (NULL == index->pagelist || 0 >= index->length)
00687     {
00688         // not initialized
00689         return;
00690     }
00691     
00692     // release all memory
00693     for(int i = 0; i < index->length; ++i)
00694     {
00695         pageListItem_release(index->pagelist[i]);
00696     }
00697     free(index->pagelist);
00698 }

Here is the call graph for this function:

int index_file_get_page_count ( IndexInfo index,
const char *  type 
)

Retrieve the page count from the index file, given the page type

Parameters:
FILE reference to the current inde file
type page type
Returns:
pagecount (DEFAULT_PAGE_COUNT in case of error)

Definition at line 733 of file indexFileHandler.cpp.

00734 {
00735     if (NULL == index)
00736     {
00737         TRACE("Invalid index pointer!");
00738         return INVALID_PAGE_COUNT;
00739     }
00740     
00741     if (NULL == type)
00742     {
00743         TRACE("Invalid type pointer!");
00744         return INVALID_PAGE_COUNT;
00745     }
00746     
00747     for(int i = 0; i < index->length; ++i)
00748     {
00749         if (0 == strcmp(index->pagelist[i]->type, type))
00750         {
00751             return index->pagelist[i]->count;
00752         }
00753     }
00754     return INVALID_PAGE_COUNT;
00755 }

char* index_file_get_page_location ( IndexInfo index,
const char *  type,
const int  number 
)

Retrieve the page number from the index file, given the page type and the url

Parameters:
IndexInfo reference to the current inde file
type page type
number page number
Returns:
url (allocated memory should be freed by the calling routine)

Definition at line 798 of file indexFileHandler.cpp.

00799 {
00800     if (NULL == index)
00801     {
00802         TRACE("Invalid index pointer!");
00803         return NULL;
00804     }
00805     
00806     if (NULL == type)
00807     {
00808         TRACE("Invalid type pointer!");
00809         return NULL;
00810     }
00811 
00812     
00813     for(int i = 0; i < index->length; ++i)
00814     {
00815         if (0 == strcmp(index->pagelist[i]->type, type))
00816         {
00817             PageListItem *pItem = index->pagelist[i];
00818             for(int j = 0; j < pItem->count; ++j)
00819             {
00820                 if (number == pItem->page[j]->number)
00821                 {
00822                     return pItem->page[j]->url;
00823                 }
00824             }
00825         }
00826     }
00827     return NULL;
00828 }

int index_file_get_page_number ( IndexInfo index,
const char *  type,
const char *  url 
)

Retrieve the page number from the index file, given the page type and the url

Parameters:
IndexInfo reference to the current index struct
type page type
url page url
Returns:
pagenumber

Definition at line 760 of file indexFileHandler.cpp.

00761 {
00762     if (NULL == index)
00763     {
00764         TRACE("Invalid index pointer!");
00765         return INVALID_PAGE_COUNT;
00766     }
00767     
00768     if (NULL == type)
00769     {
00770         TRACE("Invalid type pointer!");
00771         return INVALID_PAGE_COUNT;
00772     }
00773     
00774     if (NULL == url)
00775     {
00776         TRACE("Invalid url pointer!");
00777         return INVALID_PAGE_COUNT;
00778     }
00779    
00780     for(int i = 0; i < index->length; ++i)
00781     {
00782         if (0 == strcmp(index->pagelist[i]->type, type))
00783         {
00784             PageListItem *pItem = index->pagelist[i];
00785             for(int j = 0; j < pItem->count; ++j)
00786             {
00787                 // should use contain_url. 
00788                 if (contain_url(url, pItem->page[j]->url))
00789                 {
00790                     return pItem->page[j]->number;
00791                 }
00792             }
00793         }
00794     }
00795     return INVALID_PAGE_NMBR;
00796 }

Here is the call graph for this function:

int index_file_init ( const char *  file,
IndexInfo index 
)

Open the index/manifest file

Parameters:
file name
Returns:
non zero in case of error

Definition at line 700 of file indexFileHandler.cpp.

00701 {
00702     pIndex->pagelist  = NULL;
00703     pIndex->length    = 0;
00704     erManifest manifest;
00705     if (RET_OK != ermXmlOpenFile(pathName, &manifest))
00706     {
00707         TRACE("Could not open manifest file %s", pathName);
00708         return -1;
00709     }
00710     
00711     pIndex->length    = get_pagelist_length(&manifest);
00712     if (0 >= pIndex->length)
00713     {
00714         TRACE("No Pagelist item!");
00715         return 0;
00716     }
00717     
00718     // construct one by one
00719     pIndex->pagelist = (PageListItem **)malloc(pIndex->length * sizeof(PageListItem *));
00720     for(int i = 0; i < pIndex->length; ++i)
00721     {
00722         pIndex->pagelist[i] = pageListItem_create(&manifest, i + 1);
00723     }
00724     ermXmlClose(&manifest);
00725     
00726 #if (INDEX_DEBUG)
00727     dump(pIndex);
00728 #endif
00729     
00730     return 0;
00731 }

Here is the call graph for this function:

void pageInfo_release ( PageInfo pInfo  ) 

Definition at line 653 of file indexFileHandler.cpp.

00654 {
00655     if (pInfo->url)
00656     {
00657         free(pInfo->url);
00658         pInfo->url = NULL;
00659     }
00660     pInfo->number = INVALID_PAGE_NMBR;
00661 }

PageListItem* pageListItem_create ( erManifest manifest,
const int  index 
)

Definition at line 611 of file indexFileHandler.cpp.

00612 {
00613     char url[MAX_LEN]    = {0};
00614     char type[MAX_LEN]   = {0};
00615     char xpath[MAX_LEN]  = {0};
00616     int ret = RET_OK;
00617     int count = 0;
00618     
00619     snprintf(xpath, MAX_LEN, "/package/index/pagelist[%d]", index);
00620     ret = ermXmlGetAttributeString(manifest, xpath, "type", type, MAX_LEN);
00621     ret = ermXmlGetAttributeInt(manifest, xpath, "count", &count);
00622     if (RET_ERR == ret) 
00623     {
00624         TRACE("Could not read attribute!");
00625         return NULL;
00626     }
00627   
00628     // init page List Item, count and type
00629     PageListItem *pItem = (PageListItem *)malloc(sizeof(PageListItem));
00630     pItem->type = (char *)malloc(strnlen(type, MAX_LEN) + 1);
00631     strcpy(pItem->type, type);
00632     pItem->type[strlen(pItem->type)] = 0;
00633     pItem->count = count;
00634     pItem->page  = (PageInfo **)malloc(sizeof(PageInfo *) * count);
00635 
00636     // construct PageInfo array
00637     for(int i = 1; i <= pItem->count; ++i)
00638     {
00639         // two tags : url and number
00640         snprintf(xpath, MAX_LEN, "/package/index/pagelist[%d]/page[%d]", index, i);
00641         ermXmlGetAttributeString(manifest, xpath, "url", url, MAX_LEN);
00642         ermXmlGetAttributeInt(manifest, xpath, "number", &count);   
00643         
00644         pItem->page[i - 1] = (PageInfo *)malloc(sizeof(PageInfo));             
00645         pItem->page[i - 1]->number = count;
00646         pItem->page[i - 1]->url = (char *)malloc(strnlen(url, MAX_LEN) + 1);
00647         strcpy(pItem->page[i - 1]->url, url);
00648         pItem->page[i - 1]->url[strlen(pItem->page[i - 1]->url)] = 0;
00649     }
00650     return pItem;
00651 }

Here is the call graph for this function:

void pageListItem_release ( PageListItem pItem  ) 

Definition at line 663 of file indexFileHandler.cpp.

00664 {
00665     for(int i = 0; i < pItem->count; ++i)
00666     {
00667         // free memory used by each page fields and page itself
00668         pageInfo_release(pItem->page[i]);
00669         free(pItem->page[i]);
00670     }
00671     free(pItem->type);
00672     pItem->type = NULL;
00673     pItem->count = 0;
00674 }

Here is the call graph for this function:


Generated on Sun Dec 14 17:14:50 2008 by  doxygen 1.5.6