SDDSlib
Loading...
Searching...
No Matches
fixcounts.c
Go to the documentation of this file.
1/**
2 * @file fixcounts.c
3 * @brief Contains the implementation of the `fixcount` function to update data point counts in a file.
4 *
5 * This file provides functionality to adjust the number of data points recorded in a file by either counting the actual data points or setting it to a specified value.
6 * It is intended for use with data files that follow a specific format, excluding SDDS files.
7 */
8
9#include "mdb.h"
10#include "table.h"
11#include <ctype.h>
12
13#define LINE_LENGTH 1024
14
15/** Function: fixcount
16 *
17 * @brief Updates the data point count in a specified file.
18 *
19 * The `fixcount` function reads a file and updates the count of data points at a specific location within the file.
20 * If `n_points` is -1, it counts the actual number of data points in the file, excluding lines that start with '!' (comment lines).
21 * Otherwise, it uses the provided `n_points` value.
22 * The function then writes the count back into the file at a predetermined position, ensuring the count fits within the allocated space.
23 *
24 * The function skips updating files that start with "SDDS" followed by a digit, returning -1 in such cases.
25 *
26 * @param filename The path to the file whose data point count is to be updated.
27 * @param n_points The number of data points to set in the file.
28 * If -1, the function will count the data points by reading the file.
29 * @return Returns the number of data points written to the file on success.
30 * Returns 0 on failure, or if the count does not fit in the allocated space.
31 * Returns -1 if the file is identified as an SDDS file (starts with "SDDS" followed by a digit).
32 */
33int fixcount(char *filename, long n_points) {
34 long count;
35 FILE *fp;
36 char s[LINE_LENGTH], t[LINE_LENGTH];
37 int32_t l_count_line, posi_count_line;
38
39 if (!(fp = fopen(filename, "r")))
40 return (0);
41 if (!fgets_skip(s, LINE_LENGTH, fp, '!', 0))
42 return 0;
43 if (strncmp(s, "SDDS", 4) == 0 && isdigit(s[4]))
44 return -1;
45 if (!fgets_skip(s, LINE_LENGTH, fp, '!', 0) || !fgets_skip(s, LINE_LENGTH, fp, '!', 0) || !fgets_skip(s, LINE_LENGTH, fp, '!', 0))
46 return (0);
47 posi_count_line = ftell(fp);
48 if (!fgets_skip(s, LINE_LENGTH, fp, '!', 0))
49 return (0);
50 l_count_line = strlen(s) - 1;
51 count = 0;
52 if (n_points == -1) {
53 /* count the number of points */
54 while (fgets(s, LINE_LENGTH, fp))
55 if (s[0] != '!')
56 count++;
57 } else
58 count = n_points;
59 fclose(fp);
60 sprintf(t, "%ld", count);
61 if ((long)strlen(t) <= l_count_line && (fp = fopen(filename, "r+"))) {
62 pad_with_spaces(t, l_count_line - strlen(t));
63 if (!(fseek(fp, posi_count_line, 0) != EOF && fputs(t, fp) != EOF)) {
64 fclose(fp);
65 return (0);
66 } else {
67 fclose(fp);
68 return (count);
69 }
70 }
71 return (0);
72}
int fixcount(char *filename, long n_points)
Updates the data point count in a specified file.
Definition fixcounts.c:33
char * pad_with_spaces(char *s, int n)
Adds a specified number of spaces to the end of a string.