1 package nom.tam.fits.utilities;
2
3 import nom.tam.fits.HeaderCard;
4
5 /*
6 * #%L
7 * nom.tam FITS library
8 * %%
9 * Copyright (C) 1996 - 2024 nom-tam-fits
10 * %%
11 * This is free and unencumbered software released into the public domain.
12 *
13 * Anyone is free to copy, modify, publish, use, compile, sell, or
14 * distribute this software, either in source code form or as a compiled
15 * binary, for any purpose, commercial or non-commercial, and by any
16 * means.
17 *
18 * In jurisdictions that recognize copyright laws, the author or authors
19 * of this software dedicate any and all copyright interest in the
20 * software to the public domain. We make this dedication for the benefit
21 * of the public at large and to the detriment of our heirs and
22 * successors. We intend this dedication to be an overt act of
23 * relinquishment in perpetuity of all present and future rights to this
24 * software under copyright law.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 * OTHER DEALINGS IN THE SOFTWARE.
33 * #L%
34 */
35
36 /**
37 * <p>
38 * This class handles the writing of a card line. It keeps track of the position in the line and will limit it to 80
39 * characters. A write will never cross the line border but a write when the line is at position 80 will start a new
40 * line.
41 * </p>
42 * <p>
43 * This class probably should have been in the {@link nom.tam.util} package but somehow ended up here, so we'll have to
44 * stick with it.
45 * </p>
46 *
47 * @author Richard van Nieuwenhoven
48 *
49 * @deprecated (<i>for internal use</i>) Replaced by the package-level <code>nom.tam.fits.HeaderCardFormatter</code>.
50 */
51 @Deprecated
52 public class FitsLineAppender {
53
54 /**
55 * A String of 80 spaces to fill up fits card space.
56 */
57 private static final String FULL_CARD_AS_SPACES = String.format("%80s", "");
58
59 /**
60 * the underlying StringBuilder to which the writing of fits lines happens.
61 */
62 private final StringBuilder buffer;
63
64 /**
65 * the char current position in the line.
66 */
67 private int charCount;
68
69 /**
70 * create a new FitsLineAppender that will have space allocated for one line.
71 */
72 @Deprecated
73 public FitsLineAppender() {
74 buffer = new StringBuilder(HeaderCard.FITS_HEADER_CARD_SIZE);
75 }
76
77 /**
78 * append a character to the fits line.
79 *
80 * @param character the character to append to the line.
81 */
82 @Deprecated
83 public void append(char character) {
84 buffer.append(character);
85 charCount++;
86 }
87
88 /**
89 * Append a sub-string to this line.
90 *
91 * @param stringValue the sub string to append.
92 */
93 @Deprecated
94 public void append(FitsSubString stringValue) {
95 stringValue.appendTo(buffer);
96 charCount += stringValue.length();
97 }
98
99 /**
100 * append a string to the fits line, but limit the append to the line length. rest of the string will be silently
101 * truncated.
102 *
103 * @param string the string to append
104 */
105 @Deprecated
106 public void append(String string) {
107 charCount = charCount % HeaderCard.FITS_HEADER_CARD_SIZE;
108 int newLength = charCount + string.length();
109 if (newLength > HeaderCard.FITS_HEADER_CARD_SIZE) {
110 buffer.append(string, 0, HeaderCard.FITS_HEADER_CARD_SIZE - charCount);
111 charCount = 0;
112 } else {
113 charCount = newLength;
114 buffer.append(string);
115 }
116 }
117
118 /**
119 * append a string to the buffer, replacing all occurrences of a character with an other.
120 *
121 * @param key the string to write
122 * @param toReplace the character to replace
123 * @param with the character to replace the toReplace character with.
124 */
125 @Deprecated
126 public void appendReplacing(String key, char toReplace, char with) {
127 int size = key.length();
128 for (int index = 0; index < size; index++) {
129 char character = key.charAt(index);
130 if (character == toReplace) {
131 buffer.append(with);
132 } else {
133 buffer.append(character);
134 }
135 }
136 charCount += size;
137 }
138
139 /**
140 * append a number of spaces to the line, limited to the line length! This will only be done if the line is already
141 * started, so attention when a line is still empty this method will have no effect on empty lines.
142 *
143 * @param count the number of spaces to write.
144 */
145 @Deprecated
146 public void appendSpacesTo(int count) {
147 charCount = charCount % HeaderCard.FITS_HEADER_CARD_SIZE;
148 if (charCount != 0) {
149 int spaces = count - charCount;
150 if (spaces > 0) {
151 buffer.append(FitsLineAppender.FULL_CARD_AS_SPACES, 0, spaces);
152 charCount += spaces;
153 }
154 }
155 }
156
157 /**
158 * fill the rest of current line with spaces and start a new fits line.
159 */
160 @Deprecated
161 public void completeLine() {
162 int count = HeaderCard.FITS_HEADER_CARD_SIZE - charCount % HeaderCard.FITS_HEADER_CARD_SIZE;
163 if (count < HeaderCard.FITS_HEADER_CARD_SIZE) {
164 buffer.append(FitsLineAppender.FULL_CARD_AS_SPACES, 0, count);
165 }
166 // line completed start with 0;
167 charCount = 0;
168 }
169
170 /**
171 * @return the character position in the current line.
172 */
173 @Deprecated
174 public int length() {
175 charCount = charCount % HeaderCard.FITS_HEADER_CARD_SIZE;
176 return charCount;
177 }
178
179 /**
180 * @return the number of characters still available in the current fits line.
181 */
182 @Deprecated
183 public int spaceLeftInLine() {
184 charCount = charCount % HeaderCard.FITS_HEADER_CARD_SIZE;
185 return HeaderCard.FITS_HEADER_CARD_SIZE - charCount;
186 }
187
188 @Override
189 @Deprecated
190 public String toString() {
191 return buffer.toString();
192 }
193
194 /**
195 * Appends a substring of a FITS keyword to the buffer
196 *
197 * @param key The fits keyword
198 * @param start the starting index of the substring to append
199 * @param end the index where the substring ends (exclusive).
200 */
201 @Deprecated
202 public void append(String key, int start, int end) {
203 buffer.append(key, start, end);
204 charCount += end - start;
205 }
206 }