SDDSlib
Loading...
Searching...
No Matches
fixcounts.c File Reference

Contains the implementation of the fixcount function to update data point counts in a file. More...

#include "mdb.h"
#include "table.h"
#include <ctype.h>

Go to the source code of this file.

Macros

#define LINE_LENGTH   1024
 

Functions

int fixcount (char *filename, long n_points)
 Updates the data point count in a specified file.
 

Detailed Description

Contains the implementation of the fixcount function to update data point counts in a file.

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. It is intended for use with data files that follow a specific format, excluding SDDS files.

Definition in file fixcounts.c.

Macro Definition Documentation

◆ LINE_LENGTH

#define LINE_LENGTH   1024

Definition at line 13 of file fixcounts.c.

Function Documentation

◆ fixcount()

int fixcount ( char * filename,
long n_points )

Updates the data point count in a specified file.

Function: fixcount

The fixcount function reads a file and updates the count of data points at a specific location within the file. If n_points is -1, it counts the actual number of data points in the file, excluding lines that start with '!' (comment lines). Otherwise, it uses the provided n_points value. The function then writes the count back into the file at a predetermined position, ensuring the count fits within the allocated space.

The function skips updating files that start with "SDDS" followed by a digit, returning -1 in such cases.

Parameters
filenameThe path to the file whose data point count is to be updated.
n_pointsThe number of data points to set in the file. If -1, the function will count the data points by reading the file.
Returns
Returns the number of data points written to the file on success. Returns 0 on failure, or if the count does not fit in the allocated space. Returns -1 if the file is identified as an SDDS file (starts with "SDDS" followed by a digit).

Definition at line 33 of file fixcounts.c.

33 {
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}
char * pad_with_spaces(char *s, int n)
Adds a specified number of spaces to the end of a string.