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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 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 | { |
32 | FILE *fd; |
33 | seq->pos= 0; |
34 | if ((fd= my_fopen(file, O_RDONLY, MYF(MY_WME))) == NULL) |
35 | return 1; |
36 | if (my_init_dynamic_array(&seq->seq, sizeof(ulong), 10, 10, MYF(0))) |
37 | return 1; |
38 | |
39 | for(;;) |
40 | { |
41 | ulong num; |
42 | char line[22]; |
43 | if (fgets(line, sizeof(line), fd) == NULL) |
44 | break; |
45 | num= atol(line); |
46 | if (insert_dynamic(&seq->seq, (uchar*) &num)) |
47 | return 1; |
48 | } |
49 | fclose(fd); |
50 | 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 | { |
64 | DBUG_ASSERT(seq->seq.elements > 0); |
65 | DBUG_ASSERT(seq->pos < seq->seq.elements); |
66 | 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 | { |
78 | delete_dynamic(&seq->seq); |
79 | } |
80 | |
81 | |
82 | /** |
83 | @brief Starts the sequence from beginning |
84 | |
85 | @param seq Reference on the sequence storage. |
86 | */ |
87 | |
88 | void seq_storage_rewind(SEQ_STORAGE *seq) |
89 | { |
90 | 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 | { |
105 | FILE *fd; |
106 | 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 | } |
111 | |