]> git.sesse.net Git - freerainbowtables/blob - Server Applications/rtperfecter0.1/RTWrite.cpp
initial
[freerainbowtables] / Server Applications / rtperfecter0.1 / RTWrite.cpp
1 /*
2         Copyright (C) 2008 Steve Thomas <SMT837784@yahoo.com>
3
4         This file is part of RT Perfecter v0.0.
5
6         RT Perfecter v0.0 is free software: you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation, either version 3 of the License, or
9         (at your option) any later version.
10
11         RT Perfecter v0.0 is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with RT Perfecter v0.0.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include "RTWrite.h"
24
25 RTWrite::RTWrite(char *fileName, int maxChainsPerFile)
26 {
27         unsigned int len = strlen(fileName);
28
29         m_file = new char[len + 1];
30         m_fileTemp = new char[len + 21];
31         strncpy(m_file, fileName, len);
32         m_file[len] = '\0';
33
34         m_curFile = 0;
35         m_curFileChains = 0;
36         m_chainsPerFile = maxChainsPerFile;
37         m_pFile = NULL;
38         m_prevEndpt = 0;
39 }
40
41 RTWrite::~RTWrite()
42 {
43         if (m_pFile != NULL)
44         {
45                 fclose(m_pFile);
46                 sprintf(m_fileTemp, m_file, m_curFileChains, m_curFile);
47                 if (rename("temp.rt", m_fileTemp) != 0)
48                 {
49                         perror(m_fileTemp);
50                         exit(1);
51                 }
52         }
53         if (m_file != NULL)
54         {
55                 delete [] m_file;
56         }
57         if (m_fileTemp != NULL)
58         {
59                 delete [] m_fileTemp;
60         }
61 }
62
63 void RTWrite::writeChain(RTChain *chain)
64 {
65
66         if (m_prevEndpt >= chain->endpt && chain->endpt != 0)
67         {
68                 printf("**** Error writeChain(): Tring to write unsorted data. ****\n");
69                 exit(1);
70         }
71         if (m_pFile == NULL)
72         {
73                 m_pFile = fopen("temp.rt", "wb");
74                 if (m_pFile == NULL)
75                 {
76                         perror(m_fileTemp);
77                         exit(1);
78                 }
79         }
80         if (fwrite((void*)chain, 16, 1, m_pFile) != 1)
81         {
82                 perror("temp.rt");
83                 exit(1);
84         }       
85         m_curFileChains++;
86         if (m_curFileChains >= m_chainsPerFile)
87         {
88                 fclose(m_pFile);
89                 m_pFile = NULL;
90                 sprintf(m_fileTemp, m_file, m_curFileChains, m_curFile);
91                 if (rename("temp.rt", m_fileTemp) != 0)
92                 {
93                         perror(m_fileTemp);
94                         exit(1);
95                 }
96                 m_curFile++;
97                 m_curFileChains = 0;
98         }
99 }
100