CLI interface to medialist (fossil mirror)
Revision | d3ba7203f744eef499ca853ef45d0010b5f88059 (tree) |
---|---|
Time | 2021-09-23 18:21:19 |
Author | mio <stigma@disr...> |
Commiter | mio |
Reformat add.d
FossilOrigin-Name: 8aebd101bd198417cc803e6bec754c85bc035907cc104c223d12d9af271c0288
@@ -116,86 +116,85 @@ private string[] retrieveListHeaders(File* listFile) | ||
116 | 116 | |
117 | 117 | private void addMissingHeaders(ref string[] fileHeaders, File* listFile) |
118 | 118 | { |
119 | - import std.algorithm.searching : canFind; | |
120 | - | |
121 | - if (false == canFind(fileHeaders, "start_date")) | |
122 | - { | |
123 | - addHeader(listFile, "start_date"); | |
124 | - fileHeaders ~= "start_date"; | |
125 | - } | |
126 | - | |
127 | - if (false == canFind(fileHeaders, "end_date")) | |
128 | - { | |
129 | - addHeader(listFile, "end_date"); | |
130 | - fileHeaders ~= "end_date"; | |
131 | - } | |
132 | - | |
133 | - if (false == canFind(fileHeaders, "last_updated")) | |
134 | - { | |
135 | - addHeader(listFile, "last_updated"); | |
136 | - fileHeaders ~= "last_updated"; | |
137 | - } | |
119 | + import std.algorithm.searching : canFind; | |
120 | + | |
121 | + if (false == canFind(fileHeaders, "start_date")) | |
122 | + { | |
123 | + addHeader(listFile, "start_date"); | |
124 | + fileHeaders ~= "start_date"; | |
125 | + } | |
126 | + | |
127 | + if (false == canFind(fileHeaders, "end_date")) | |
128 | + { | |
129 | + addHeader(listFile, "end_date"); | |
130 | + fileHeaders ~= "end_date"; | |
131 | + } | |
132 | + | |
133 | + if (false == canFind(fileHeaders, "last_updated")) | |
134 | + { | |
135 | + addHeader(listFile, "last_updated"); | |
136 | + fileHeaders ~= "last_updated"; | |
137 | + } | |
138 | 138 | } |
139 | 139 | |
140 | 140 | private |
141 | 141 | void addHeader(File* f, string newHeader) @trusted |
142 | 142 | in |
143 | 143 | { |
144 | - assert(f.isOpen == true); | |
145 | - assert(newHeader !is null); | |
144 | + assert(f.isOpen == true); | |
145 | + assert(newHeader !is null); | |
146 | 146 | } |
147 | 147 | out |
148 | 148 | { |
149 | - assert(f.isOpen == true); | |
149 | + assert(f.isOpen == true); | |
150 | 150 | } |
151 | 151 | do |
152 | 152 | { |
153 | - import std.file : rename, remove, tempDir; | |
154 | - import std.path : buildPath, baseName; | |
153 | + import std.file : rename, remove, tempDir; | |
154 | + import std.path : buildPath, baseName; | |
155 | 155 | |
156 | - immutable origPath = f.name; | |
157 | - immutable tempPath = buildPath(tempDir(), baseName(origPath)); | |
156 | + immutable origPath = f.name; | |
157 | + immutable tempPath = buildPath(tempDir(), baseName(origPath)); | |
158 | 158 | |
159 | - File tempFile = File(tempPath, "w+"); | |
159 | + File tempFile = File(tempPath, "w+"); | |
160 | 160 | |
161 | - string line = ""; | |
162 | - bool pastHeader = false; | |
161 | + string line = ""; | |
162 | + bool pastHeader = false; | |
163 | 163 | |
164 | - f.rewind(); | |
164 | + f.rewind(); | |
165 | 165 | |
166 | - while ((line = f.readln()) !is null) | |
167 | - { | |
168 | - if (false == pastHeader && '#' != line[0]) | |
169 | - { | |
170 | - pastHeader = true; | |
171 | - tempFile.writefln("%s\t%s", line.strip, newHeader); | |
172 | - } | |
173 | - else | |
174 | - { | |
175 | - /* line may not have a newline character */ | |
176 | - tempFile.writeln(line.strip); | |
177 | - } | |
178 | - } | |
179 | - | |
180 | - f.close(); | |
166 | + while ((line = f.readln()) !is null) | |
167 | + { | |
168 | + if (false == pastHeader && '#' != line[0]) | |
169 | + { | |
170 | + pastHeader = true; | |
171 | + tempFile.writefln("%s\t%s", line.strip, newHeader); | |
172 | + } | |
173 | + else | |
174 | + { | |
175 | + /* line may not have a newline character */ | |
176 | + tempFile.writeln(line.strip); | |
177 | + } | |
178 | + } | |
181 | 179 | |
182 | - /* for whatever fucking reason, both "copy" and "rename" don't work. */ | |
180 | + f.close(); | |
183 | 181 | |
184 | - remove(origPath); | |
182 | + remove(origPath); | |
185 | 183 | |
186 | - f.open(origPath, "a+"); | |
187 | - tempFile.rewind(); | |
184 | + /* some operating systems don't like to copy from the tempdir? */ | |
185 | + f.open(origPath, "a+"); | |
186 | + tempFile.rewind(); | |
188 | 187 | |
189 | - while ((line = tempFile.readln()) !is null) | |
190 | - { | |
191 | - f.write(line); | |
192 | - } | |
188 | + while ((line = tempFile.readln()) !is null) | |
189 | + { | |
190 | + f.write(line); | |
191 | + } | |
193 | 192 | |
194 | - /* rewind because we need to re-read the headers */ | |
195 | - f.rewind(); | |
193 | + /* rewind because we need to re-read the headers */ | |
194 | + f.rewind(); | |
196 | 195 | |
197 | - tempFile.close(); | |
198 | - remove(tempPath); | |
196 | + tempFile.close(); | |
197 | + remove(tempPath); | |
199 | 198 | } |
200 | 199 | |
201 | 200 | private void writeNewItem(File* listFile, HeaderPairType[NUMBER_OF_ML_HEADERS] headersAndPositions, |
@@ -258,15 +257,25 @@ private void replicateIndent(File* listFile, HeaderPairType headerPair, size_t* | ||
258 | 257 | *previousIndent = headerPair[0]; |
259 | 258 | } |
260 | 259 | |
261 | -@trusted private void display_usage(string program_name) | |
260 | +@trusted @nogc private void display_usage(string programName) | |
262 | 261 | { |
263 | - stderr.writefln("usage: %s add <list_name> <title> [options] | |
262 | + import core.stdc.stdio : fprintf; | |
263 | + import core.stdc.stdio : cstderr = stderr; | |
264 | + import core.stdc.stdlib : malloc, free; | |
265 | + | |
266 | + // Can't add NULL byte to D string, so allocate a temp string and free it. | |
267 | + char* progname = cast(char*)malloc(char.sizeof * programName.length); | |
268 | + scope(exit) free(progname); | |
269 | + | |
270 | + progname[0..programName.length] = programName[]; | |
271 | + progname[programName.length] = '\0'; | |
272 | + | |
273 | + fprintf(cstderr, "usage: %s add <list_name> <title> [options] | |
264 | 274 | |
265 | 275 | list_name = the list name to add the new item to |
266 | 276 | title = the title of the new item |
267 | 277 | |
268 | 278 | options: |
269 | - -p, --progress the initial progress of the new item (default: ??/??) | |
270 | - -s, --status the initial status of the new item (default: UNKNOWN)", | |
271 | - program_name); | |
279 | +-p, --progress the initial progress of the new item (default: ??/??) | |
280 | +-s, --status the initial status of the new item (default: UNKNOWN)\n", progname); | |
272 | 281 | } |