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 public FitsLineAppender() { 73 buffer = new StringBuilder(HeaderCard.FITS_HEADER_CARD_SIZE); 74 } 75 76 /** 77 * append a character to the fits line. 78 * 79 * @param character the character to append to the line. 80 */ 81 public void append(char character) { 82 buffer.append(character); 83 charCount++; 84 } 85 86 /** 87 * Append a sub-string to this line. 88 * 89 * @param stringValue the sub string to append. 90 */ 91 public void append(FitsSubString stringValue) { 92 stringValue.appendTo(buffer); 93 charCount += stringValue.length(); 94 } 95 96 /** 97 * append a string to the fits line, but limit the append to the line length. rest of the string will be silently 98 * truncated. 99 * 100 * @param string the string to append 101 */ 102 public void append(String string) { 103 charCount = charCount % HeaderCard.FITS_HEADER_CARD_SIZE; 104 int newLength = charCount + string.length(); 105 if (newLength > HeaderCard.FITS_HEADER_CARD_SIZE) { 106 buffer.append(string, 0, HeaderCard.FITS_HEADER_CARD_SIZE - charCount); 107 charCount = 0; 108 } else { 109 charCount = newLength; 110 buffer.append(string); 111 } 112 } 113 114 /** 115 * append a string to the buffer, replacing all occurrences of a character with an other. 116 * 117 * @param key the string to write 118 * @param toReplace the character to replace 119 * @param with the character to replace the toReplace character with. 120 */ 121 public void appendReplacing(String key, char toReplace, char with) { 122 int size = key.length(); 123 for (int index = 0; index < size; index++) { 124 char character = key.charAt(index); 125 if (character == toReplace) { 126 buffer.append(with); 127 } else { 128 buffer.append(character); 129 } 130 } 131 charCount += size; 132 } 133 134 /** 135 * append a number of spaces to the line, limited to the line length! This will only be done if the line is already 136 * started, so attention when a line is still empty this method will have no effect on empty lines. 137 * 138 * @param count the number of spaces to write. 139 */ 140 public void appendSpacesTo(int count) { 141 charCount = charCount % HeaderCard.FITS_HEADER_CARD_SIZE; 142 if (charCount != 0) { 143 int spaces = count - charCount; 144 if (spaces > 0) { 145 buffer.append(FitsLineAppender.FULL_CARD_AS_SPACES, 0, spaces); 146 charCount += spaces; 147 } 148 } 149 } 150 151 /** 152 * fill the rest of current line with spaces and start a new fits line. 153 */ 154 public void completeLine() { 155 int count = HeaderCard.FITS_HEADER_CARD_SIZE - charCount % HeaderCard.FITS_HEADER_CARD_SIZE; 156 if (count < HeaderCard.FITS_HEADER_CARD_SIZE) { 157 buffer.append(FitsLineAppender.FULL_CARD_AS_SPACES, 0, count); 158 } 159 // line completed start with 0; 160 charCount = 0; 161 } 162 163 /** 164 * @return the character position in the current line. 165 */ 166 public int length() { 167 charCount = charCount % HeaderCard.FITS_HEADER_CARD_SIZE; 168 return charCount; 169 } 170 171 /** 172 * @return the number of characters still available in the current fits line. 173 */ 174 public int spaceLeftInLine() { 175 charCount = charCount % HeaderCard.FITS_HEADER_CARD_SIZE; 176 return HeaderCard.FITS_HEADER_CARD_SIZE - charCount; 177 } 178 179 @Override 180 public String toString() { 181 return buffer.toString(); 182 } 183 184 /** 185 * Appends a substring of a FITS keyword to the buffer 186 * 187 * @param key The fits keyword 188 * @param start the starting index of the substring to append 189 * @param end the index where the substring ends (exclusive). 190 */ 191 public void append(String key, int start, int end) { 192 buffer.append(key, start, end); 193 charCount += end - start; 194 } 195 }