1 : /* Copyright (C) 2008 MySQL AB
2 :
3 : This program is free software; you can redistribute it and/or modify
4 : it under the terms of the GNU General Public License as published by
5 : the Free Software Foundation; version 2 of the License.
6 :
7 : This program is distributed in the hope that it will be useful,
8 : but WITHOUT ANY WARRANTY; without even the implied warranty of
9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 : GNU General Public License for more details.
11 :
12 : You should have received a copy of the GNU General Public License
13 : along with this program; if not, write to the Free Software
14 : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
15 :
16 : #include "../maria_def.h"
17 : #include "sequence_storage.h"
18 :
19 :
20 : /**
21 : @brief Initializes the sequence from the sequence file.
22 :
23 : @param seq Reference on the sequence storage.
24 : @param file Path to the file where to write the sequence
25 :
26 : @retval 0 OK
27 : @retval 1 Error
28 : */
29 :
30 : my_bool seq_storage_reader_init(SEQ_STORAGE *seq, const char *file)
31 0 : {
32 : FILE *fd;
33 0 : seq->pos= 0;
34 0 : if ((fd= my_fopen(file, O_RDONLY, MYF(MY_WME))) == NULL)
35 0 : return 1;
36 0 : if (my_init_dynamic_array(&seq->seq, sizeof(ulong), 10, 10))
37 0 : return 1;
38 :
39 : for(;;)
40 : {
41 : ulong num;
42 : char line[22];
43 0 : if (fgets(line, sizeof(line), fd) == NULL)
44 0 : break;
45 0 : num= atol(line);
46 0 : if (insert_dynamic(&seq->seq, (uchar*) &num))
47 0 : return 1;
48 : }
49 0 : fclose(fd);
50 0 : return 0;
51 : }
52 :
53 :
54 : /**
55 : @brief Gets next number from the sequence storage
56 :
57 : @param seq Reference on the sequence storage.
58 :
59 : @return Next number from the sequence.
60 : */
61 :
62 : ulong seq_storage_next(SEQ_STORAGE *seq)
63 0 : {
64 0 : DBUG_ASSERT(seq->seq.elements > 0);
65 0 : DBUG_ASSERT(seq->pos < seq->seq.elements);
66 0 : return (*(dynamic_element(&seq->seq, seq->pos++, ulong *)));
67 : }
68 :
69 :
70 : /**
71 : @brief Frees resources allocated for the storage
72 :
73 : @param seq Reference on the sequence storage.
74 : */
75 :
76 : void seq_storage_destroy(SEQ_STORAGE *seq)
77 2 : {
78 2 : delete_dynamic(&seq->seq);
79 : }
80 :
81 :
82 : /**
83 : @brief Starts the sequence from begining
84 :
85 : @param seq Reference on the sequence storage.
86 : */
87 :
88 : void seq_storage_rewind(SEQ_STORAGE *seq)
89 0 : {
90 0 : seq->pos= 0;
91 : }
92 :
93 : /**
94 : @brief Writes a number to the sequence file.
95 :
96 : @param file Path to the file where to write the sequence
97 : @pagem num Number to be written
98 :
99 : @retval 0 OK
100 : @retval 1 Error
101 : */
102 :
103 : my_bool seq_storage_write(const char *file, ulong num)
104 0 : {
105 : FILE *fd;
106 0 : return ((fd= my_fopen(file, O_CREAT | O_APPEND | O_WRONLY, MYF(MY_WME))) ==
107 : NULL ||
108 : fprintf(fd, "%lu\n", num) < 0 ||
109 : fclose(fd) != 0);
110 : }
|