Add pd_read_file_ws, pd_write_file_ws

This commit is contained in:
Philip Minakov 2026-03-12 13:42:34 +04:00
parent d9b0c1a020
commit 975b096a2b
No known key found for this signature in database
GPG key ID: 932F1B884934DB0F
3 changed files with 34 additions and 10 deletions

View file

@ -55,12 +55,12 @@ Creates a new node holding a string value.
Returns a pointer to the newly allocated `pd_node_t` structure or `NULL`.
```C
int pd_read_file(const char *file_name, pd_node_t ***nodes);
int pd_read_file(const char *path, pd_node_t ***nodes);
```
Reads a `.pd` file and creates an array of nodes.
- Parameters
- `file_name` - path to the input file.
- `path` - path to the input file.
- `nodes` - output parameter. On success, `*nodes` points to a newly allocated NULL‑terminated array of `pd_node_t*` pointers.
- Returns
- `0` on success (including the case of an empty file, in which case a minimal array containing only a `NULL` pointer is allocated).
@ -69,18 +69,28 @@ Reads a `.pd` file and creates an array of nodes.
- Negative values for other I/O or memory errors.
```C
int pd_write_file(const char *file_name, const pd_node_t **nodes);
int pd_read_file_ws(const WSHDR *path, pd_node_t ***nodes);
```
`WSHDR` version of `pd_read_file`.
```C
int pd_write_file(const char *path, const pd_node_t **nodes);
```
Writes an array of nodes to a file in the `.pd` format.
- Parameters
- `file_name` - path to the output file.
- `path` - path to the output file.
- `nodes` - NULL‑terminated array of node pointers.
- Returns
- `0` on success.
- `-1` if the file cannot be created.
- `-2` if a write error occurs.
```C
int pd_write_file_ws(const WSHDR *path, const pd_node_t **nodes);
```
`WSHDR` version of `pd_write_file`.
```C
size_t pd_get_max_group_size(void);
```

View file

@ -128,8 +128,8 @@ static pd_node_t *pd_parse_line(const char *p) {
return node;
}
int pd_read_file(const char *file_name, PD_NODE ***nodes) {
FILE *file = fopen(file_name, "r");
int pd_read_file(const char *path, pd_node_t ***nodes) {
FILE *file = fopen(path, "r");
if (!file) {
return -1;
}
@ -225,8 +225,14 @@ int pd_read_file(const char *file_name, PD_NODE ***nodes) {
return 0;
}
int pd_write_file(const char *file_name, const pd_node_t **nodes) {
FILE *file = fopen(file_name, "w");
int pd_read_file_ws(const WSHDR *path, pd_node_t ***nodes) {
char p[256 + 1];
ws_2str(path, p, 255);
return pd_read_file(p, nodes);
}
int pd_write_file(const char *path, const pd_node_t **nodes) {
FILE *file = fopen(path, "w");
if (!file) {
return -1;
}
@ -275,6 +281,12 @@ int pd_write_file(const char *file_name, const pd_node_t **nodes) {
return 0;
}
int pd_write_file_ws(const WSHDR *path, const pd_node_t **nodes) {
char p[256 + 1];
ws_2str(path, p, 255);
return pd_write_file(p, nodes);
}
size_t pd_get_max_group_size() {
return PD_MAX_GROUP_SIZE;
}

View file

@ -25,8 +25,10 @@ typedef PD_NODE pd_node_t;
pd_node_t *pd_alloc_integer_node(const char *group, const char *key, long value);
pd_node_t *pd_alloc_string_node(const char *group, const char *key, const char *value);
int pd_read_file(const char *file_name, pd_node_t ***nodes);
int pd_write_file(const char *file_name, const pd_node_t **nodes);
int pd_read_file(const char *path, pd_node_t ***nodes);
int pd_read_file_ws(const WSHDR *path, pd_node_t ***nodes);
int pd_write_file(const char *path, const pd_node_t **nodes);
int pd_write_file_ws(const WSHDR *path, const pd_node_t **nodes);
size_t pd_get_max_group_size();
size_t pd_get_max_key_size();