/* retrieve an item from the data file */ #include #include "RandomImage.h" #include "web.h" #define HTMLSOURCE "HTMLSrc/GraphicIndexTemplate.html" #define HTMLSOURCE_HAN "HTMLSrc/GraphicIndexTemplate_han.html" char Language[10]; /* index sorting language */ main() { getentry entries[2]; /* arguments from the user via http */ int i, x; /* for general use */ int accNum; /* access control number */ strcpy(Language, "English"); /* default language = English */ accNum = -1; /* default to negative number */ printf("content-type: text/html\n\n"); /* HTML Header */ #ifdef FILEOUT printf("Debugging Mode\n"); fflush(stdout); i = 1; strcpy(entries[0].name, "AccNum"); strcpy(entries[0].val, "7"); #else i = ProcessGetMethod( entries ); #endif /* extract user's requirements */ for (x=0; x<=i; x++) { if (!strcasecmp(entries[x].name, "AccNum")) { accNum = atoi(entries[x].val); continue; } if (!strcasecmp(entries[x].name, "Lang")) { strcpy(Language, entries[x].val); continue; } } GraphicIndex(accNum); exit(0); } GraphicIndex(accNum) int accNum; /* access control number */ { FILE *fp; /* file pointer for HTML template */ char line[256]; /* HTML template line buffer */ DATABUF dat[5]; /* data buffer to read the result */ int fd; /* file descriptor for data file */ off_t offset; /* offset to move file pointer */ int cnt; /* total number of records in the data file */ time_t curTime; /* open data file */ if (!strcasecmp(Language, "English")) /* English Index? */ fd = open(SYS_CATALOG_ENG, O_RDONLY); else fd = open(SYS_CATALOG, O_RDONLY); if (fd < 0) { printf("at RandomSelect(), data file open failed"); exit(1); } /* get the total number of items */ if (lseek(fd, 0, SEEK_SET) < 0) { printf("lseek failed for total count."); exit(1); } read(fd, (char *) &cnt, sizeof(int)); if ( (accNum < 0) || accNum > cnt-1) /* default access number as radom */ { time(&curTime); accNum = ((int)((curTime)%cnt)/5)*5; } else /* round off access number as 5 multiple */ accNum = (int)(accNum/5)*5; /* move to the correct point and get the records */ offset = accNum*sizeof(DATABUF); if (lseek(fd, offset, SEEK_CUR) < 0) { printf("lseek failed for data retrieval."); exit(1); } read(fd, (char *) dat, 5*sizeof(DATABUF)); close(fd); /* print the result */ if ( !strcasecmp(Language, "Korean") ) { fp=fopen(HTMLSOURCE_HAN, "r"); } else { fp=fopen(HTMLSOURCE, "r"); } if ( fp == NULL ) { printf("HTML template open failed.\n"); exit(1); } while (fgets(line, 255, fp)) { char *ptr; if (!strncmp(line, "__RESULT__", 10)) PrintIndex(dat, cnt); else if (ptr = (char *)strstr(line, "__QUERY__")) { char buf[256]; *ptr = '\0'; strcpy(buf, dat[0].e_name); ConvertPlus(' ', buf); printf("%s%s%s", line, buf, ptr+9); } else printf("%s", line); } fclose(fp); } PrintIndex(dat, cnt) DATABUF *dat; int cnt; /* total number of items */ { int i; char e_name[100]; /* correct English name. '+' wil be replaced to ' ' */ printf("\n"); for(i=0; i<5; i++) { if (dat[i].an > cnt-1) break; /* if overflow, break */ strcpy(e_name, dat[i].e_name); ConvertPlus(' ', e_name); /* for second table */ if (i == 3) printf("\n

\n\n"); printf("", e_name, dat[i].an+1); } printf("
\n"); if (!strcmp(Language, "English")) printf("",dat[i].e_name); else printf("", dat[i].e_name); printf("\"%s\"", dat[i].imgFName, dat[i].imgFName); printf("
\n"); if ( !strcasecmp(Language, "Korean") ) { printf("%s
\n", dat[i].h_name, dat[i].h_name); printf("", dat[i].e_name); } else { printf("", dat[i].e_name); } printf("%s
(%d)

\n"); printf("\n"); if (dat[0].an-5 > 0) { printf("| FIRST ", Language); printf("| PREV ", dat[0].an-5, Language); } else { printf("| FIRST "); printf("| PREV "); } if (dat[0].an-50 > 0) printf("| 10x PREV ", dat[0].an-50, Language); else printf("| 10x PREV "); if (dat[0].an+50 < cnt) printf("| 10x NEXT ", dat[0].an+50, Language); else printf("| 10x NEXT "); if (dat[0].an+5 < cnt) { printf("| NEXT", dat[0].an+5, Language); printf("| LAST |", (int)((cnt-1)/5)*5, Language); } else { printf("| NEXT "); printf("| LAST |"); } printf("\n"); } /* convert '+' in s to c */ ConvertPlus(c, s) char c; char *s; { char *ptr; while (ptr = (char *) strstr(s, "+")) { *(ptr) = c; } }