1 // Larbin
2 // Sebastien Ailleret
3 // 09-12-01 -> 09-12-01
4 
5 
6 static int nbdir = -1;
7 static int nbfile = filesPerDir;
8 static char *fileName;
9 static uint endFileName;
10 static int indexFds = -1;
11 static char buf[maxUrlSize+30];
12 
13 /** give the name of the file given dir and file number
14  * this char * is static */
getSpecName(int nbdir,int nbfile)15 static void getSpecName(int nbdir, int nbfile) {
16   sprintf(fileName+endFileName, "d%5i/f%5i%s",
17           nbdir, nbfile, privilegedExts[0]);
18   for (int i=endFileName+1; fileName[i]==' '; i++)
19     fileName[i] = '0';
20   for (int i=endFileName+8; fileName[i]==' '; i++)
21     fileName[i] = '0';
22 }
23 
getIndexName(int nbdir)24 static void getIndexName(int nbdir) {
25   sprintf(fileName+endFileName, "d%5i/index", nbdir);
26   for (int i=endFileName+1; fileName[i]==' '; i++)
27     fileName[i] = '0';
28 }
29 
30 /** in case of specific save */
initSpecific()31 void initSpecific () {
32   mkdir(specDir, S_IRWXU);
33   endFileName = strlen(specDir);
34   fileName = new char[endFileName+20];
35   strcpy(fileName, specDir);
36   if (fileName[endFileName-1] != '/') fileName[endFileName++] = '/';
37 }
38 
39 /** open file descriptor */
newSpec()40 void html::newSpec () {
41   nbfile++;
42   if (nbfile >= filesPerDir) { // new dir
43     nbdir++; nbfile = 0;
44     // create the directory
45     getIndexName(nbdir);
46     fileName[endFileName+6] = 0;
47     if (mkdir(fileName, S_IRWXU) != 0) perror("trouble while creating dir");
48     fileName[endFileName+6] = '/';
49     // open new index
50     close(indexFds);
51     indexFds = creat(fileName, S_IRWXU);
52     if (indexFds < 0) {
53       std::cerr << "cannot open file " << fileName << " : "
54            << strerror(errno) << std::endl;
55       exit(1);
56     }
57   }
58   mydir = nbdir;
59   myfile = nbfile;
60   // write index
61   int s = sprintf(buf, "%4d  ", nbfile);
62   s += here->writeUrl(buf+s);
63   buf[s++] = '\n';
64   ecrireBuff(indexFds, buf, s);
65   // open new file
66   getSpecName(nbdir, nbfile);
67   fdsSpec = creat(fileName, S_IRWXU);
68   if (fdsSpec < 0) {
69     std::cerr << "cannot open file " << fileName << " : "
70          << strerror(errno) << std::endl;
71     exit(1);
72   }
73   nbSpec = 0;
74 }
75 
76 /** feed file descriptor */
pipeSpec()77 bool html::pipeSpec () {
78   int nb = buffer + pos - posParse;
79   nbSpec += nb;
80   if (nbSpec >= maxSpecSize) {
81     errno = tooBig;
82     return true;
83   }
84   ecrireBuff(fdsSpec, posParse, nb);
85   pos = posParse - buffer;
86   return false;
87 }
88 
89 /** get the content of the page */
getContent()90 char *html::getContent () {
91   static char content[maxSpecSize];
92   if (mydir >= 0) {
93     getSpecName(mydir, myfile);
94     int fds = open (fileName, O_RDONLY);
95     if (fds < 0) perror(fileName);
96     int cont = 1;
97     int pos = 0;
98     while (cont) {
99       cont = read(fds, content+pos, maxSpecSize-1-pos);
100       pos += cont;
101     }
102     content[pos] = 0;
103     close(fds);
104     return content;
105   } else {
106     return contentStart;
107   }
108 }
109 
110 #define getSize() nbSpec
111 
112 #define constrSpec() \
113   fdsSpec = -1; \
114   mydir = -1;   // indicate no save
115 
116 #define endOfInput() 0
117 
118 #define destructSpec() if (fdsSpec != -1) close(fdsSpec);
119