The "amount" is present to support the parameters that fread() and fwrite() utilize.
This makes no sense to me and it is annoying and confusing.
I end up having to just put 1.
Get rid of it and just use the file.size_read and file.size_write to specify the buffer size to read/write.
The only things that I can thing of might be atomic operations, locking, and calling the function multiple times.
These are good reasons to have an "amount".
If I end up wanting o needing an "amount", I may add additional functions later on.
#endif // _di_f_file_stream_open_
#ifndef _di_f_file_stream_read_
- f_status_t f_file_stream_read(const f_file_t file, const f_array_length_t amount, f_string_dynamic_t *buffer) {
+ f_status_t f_file_stream_read(const f_file_t file, f_string_dynamic_t *buffer) {
#ifndef _di_level_0_parameter_checking_
if (!file.size_read) return F_status_set_error(F_parameter);
- if (amount < 1) return F_status_set_error(F_parameter);
if (buffer->used > buffer->size) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
memset(buffer->string + buffer->used, 0, sizeof(file.size_read));
}
- size_read = fread(buffer->string + buffer->used, amount, file.size_read, file.stream);
+ size_read = fread(buffer->string + buffer->used, 1, file.size_read, file.stream);
if (ferror(file.stream)) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block);
return F_status_set_error(F_failure);
}
- buffer->used += size_read * amount;
+ buffer->used += size_read;
if (feof(file.stream)) break;
} // for
#endif // _di_f_file_stream_read_
#ifndef _di_f_file_stream_read_block_
- f_status_t f_file_stream_read_block(const f_file_t file, const f_array_length_t amount, f_string_dynamic_t *buffer) {
+ f_status_t f_file_stream_read_block(const f_file_t file, f_string_dynamic_t *buffer) {
#ifndef _di_level_0_parameter_checking_
if (!file.size_read) return F_status_set_error(F_parameter);
- if (amount < 1) return F_status_set_error(F_parameter);
if (buffer->used > buffer->size) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
if (!file.stream) return F_status_set_error(F_file_closed);
- const f_array_length_t buffer_size = file.size_read * amount;
-
f_status_t status = F_none;
ssize_t size_read = 0;
- if (buffer->used + buffer_size > buffer->size) {
- if (buffer->size + buffer_size > f_array_length_t_size) {
+ if (buffer->used + file.size_read > buffer->size) {
+ if (buffer->size + file.size_read > f_array_length_t_size) {
return F_status_set_error(F_string_too_large);
}
- macro_f_string_dynamic_t_resize(status, (*buffer), buffer->size + buffer_size);
+ macro_f_string_dynamic_t_resize(status, (*buffer), buffer->size + file.size_read);
if (F_status_is_error(status)) return status;
}
- size_read = fread(buffer->string + buffer->used, amount, file.size_read, file.stream);
+ size_read = fread(buffer->string + buffer->used, 1, file.size_read, file.stream);
if (ferror(file.stream)) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block);
return F_status_set_error(F_failure);
}
- buffer->used += size_read * amount;
+ buffer->used += size_read;
if (feof(file.stream)) {
return F_none_eof;
#endif // _di_f_file_stream_read_block_
#ifndef _di_f_file_stream_read_until_
- f_status_t f_file_stream_read_until(const f_file_t file, const f_array_length_t amount, const f_array_length_t total, f_string_dynamic_t *buffer) {
+ f_status_t f_file_stream_read_until(const f_file_t file, const f_array_length_t total, f_string_dynamic_t *buffer) {
#ifndef _di_level_0_parameter_checking_
if (!file.size_read) return F_status_set_error(F_parameter);
- if (amount < 1) return F_status_set_error(F_parameter);
if (buffer->used > buffer->size) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
if (F_status_is_error(status)) return status;
}
- size_read = fread(buffer->string + buffer->used, amount, file.size_read, file.stream);
+ size_read = fread(buffer->string + buffer->used, 1, file.size_read, file.stream);
if (ferror(file.stream)) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block);
return F_status_set_error(F_failure);
}
- buffer->used += size_read * amount;
+ buffer->used += size_read;
if (feof(file.stream)) {
return F_none_eof;
}
- buffer_count += size_read * amount;
+ buffer_count += size_read;
if (buffer_count == total) break;
} // for
#endif // _di_f_file_stream_reopen_
#ifndef _di_f_file_stream_write_
- f_status_t f_file_stream_write(const f_file_t file, const f_string_static_t buffer, const f_array_length_t amount, f_array_length_t *written) {
+ f_status_t f_file_stream_write(const f_file_t file, const f_string_static_t buffer, f_array_length_t *written) {
#ifndef _di_level_0_parameter_checking_
if (!file.size_write) return F_status_set_error(F_parameter);
if (buffer.used > buffer.size) return F_status_set_error(F_parameter);
- if (amount < 1) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
if (!file.stream) {
f_status_t status = F_none;
if (written) {
- private_f_file_stream_write_until(file, buffer.string, amount, buffer.used, written);
+ private_f_file_stream_write_until(file, buffer.string, buffer.used, written);
if (status == F_none && *written == buffer.used) {
return F_none_eos;
else {
f_array_length_t written_local = 0;
- private_f_file_stream_write_until(file, buffer.string, amount, buffer.used, &written_local);
+ private_f_file_stream_write_until(file, buffer.string, buffer.used, &written_local);
if (status == F_none && written_local == buffer.used) {
return F_none_eos;
#endif // _di_f_file_stream_write_
#ifndef _di_f_file_stream_write_block_
- f_status_t f_file_stream_write_block(const f_file_t file, const f_string_static_t buffer, const f_array_length_t amount, f_array_length_t *written) {
+ f_status_t f_file_stream_write_block(const f_file_t file, const f_string_static_t buffer, f_array_length_t *written) {
#ifndef _di_level_0_parameter_checking_
if (!file.size_write) return F_status_set_error(F_parameter);
if (buffer.used > buffer.size) return F_status_set_error(F_parameter);
- if (amount < 1) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
if (!file.stream) {
f_status_t status = F_none;
if (written) {
- private_f_file_stream_write_until(file, buffer.string, amount, write_max, written);
+ private_f_file_stream_write_until(file, buffer.string, write_max, written);
if (status == F_none) {
if (*written == buffer.used) {
else {
f_array_length_t written_local = 0;
- private_f_file_stream_write_until(file, buffer.string, amount, write_max, &written_local);
+ private_f_file_stream_write_until(file, buffer.string, write_max, &written_local);
if (status == F_none) {
if (written_local == buffer.used) {
#endif // _di_f_file_stream_write_block_
#ifndef _di_f_file_stream_write_until_
- f_status_t f_file_stream_write_until(const f_file_t file, const f_string_static_t buffer, const f_array_length_t amount, const f_array_length_t total, f_array_length_t *written) {
+ f_status_t f_file_stream_write_until(const f_file_t file, const f_string_static_t buffer, const f_array_length_t total, f_array_length_t *written) {
#ifndef _di_level_0_parameter_checking_
if (!file.size_write) return F_status_set_error(F_parameter);
if (buffer.used > buffer.size) return F_status_set_error(F_parameter);
- if (amount < 1) return F_status_set_error(F_parameter);
if (!total) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
f_status_t status = F_none;
if (written) {
- private_f_file_stream_write_until(file, buffer.string, amount, write_max, written);
+ private_f_file_stream_write_until(file, buffer.string, write_max, written);
if (status == F_none) {
if (*written == buffer.used) {
else {
f_array_length_t written_local = 0;
- private_f_file_stream_write_until(file, buffer.string, amount, buffer.used, &written_local);
+ private_f_file_stream_write_until(file, buffer.string, buffer.used, &written_local);
if (status == F_none) {
if (written_local == buffer.used) {
#endif // _di_f_file_stream_write_until_
#ifndef _di_f_file_stream_write_range_
- f_status_t f_file_stream_write_range(const f_file_t file, const f_string_static_t buffer, const f_array_length_t amount, const f_string_range_t range, f_array_length_t *written) {
+ f_status_t f_file_stream_write_range(const f_file_t file, const f_string_static_t buffer, const f_string_range_t range, f_array_length_t *written) {
#ifndef _di_level_0_parameter_checking_
if (!file.size_write) return F_status_set_error(F_parameter);
if (buffer.used > buffer.size) return F_status_set_error(F_parameter);
- if (amount < 1) return F_status_set_error(F_parameter);
if (range.stop < range.start) return F_status_set_error(F_parameter);
if (range.start >= buffer.used) return F_status_set_error(F_parameter);
#endif // _di_level_0_parameter_checking_
f_status_t status = F_none;
if (written) {
- private_f_file_stream_write_until(file, buffer.string + range.start, amount, write_max, written);
+ private_f_file_stream_write_until(file, buffer.string + range.start, write_max, written);
if (status == F_none) {
if (range.start + *written == buffer.used) {
else {
f_array_length_t written_local = 0;
- private_f_file_stream_write_until(file, buffer.string + range.start, amount, write_max, &written_local);
+ private_f_file_stream_write_until(file, buffer.string + range.start, write_max, &written_local);
if (status == F_none) {
if (range.start + written_local == buffer.used) {
* To determine how much was read into the buffer, record buffer->used before execution and compare to buffer->used after execution.
*
* This is different from simply using the file.size_read.
- * The file.size_read represents the amount to process for a given buffer size.
- * The total represents the maximum number of bytes to process.
+ * The file.size_read represents the amount to process at a given time.
+ * The total represents the maximum number of file.size_read to process.
* For example, if file.size_read is 16 and total is 128, then this function would need to be called 8 times until total is reached.
*
* @param file
* @param file
* The file to read.
* The file must already be open.
- * @param amount
- * The total amount of file.size_read to process.
- * This amount is multiplied against file.size_read and must be greater than 0.
+ * The file.size_read represents the amount to process at a given time.
* @param buffer
* The buffer the file is being read into.
* The contents of the file is appended into this buffer.
* @see fread()
*/
#ifndef _di_f_file_stream_read_
- extern f_status_t f_file_stream_read(const f_file_t file, const f_array_length_t amount, f_string_dynamic_t *buffer);
+ extern f_status_t f_file_stream_read(const f_file_t file, f_string_dynamic_t *buffer);
#endif // _di_f_file_stream_read_
/**
* @param file
* The file to read.
* The file must already be open.
- * @param amount
- * The total amount of file.size_read to process.
- * This amount is multiplied against file.size_read and must be greater than 0.
+ * The file.size_read represents the amount to process at a given time.
* @param buffer
* The buffer the file is being read into.
* The contents of the file is appended into this buffer.
* @see fread()
*/
#ifndef _di_f_file_stream_read_block_
- extern f_status_t f_file_stream_read_block(const f_file_t file, const f_array_length_t amount, f_string_dynamic_t *buffer);
+ extern f_status_t f_file_stream_read_block(const f_file_t file, f_string_dynamic_t *buffer);
#endif // _di_f_file_stream_read_block_
/**
* To check how much was read into the buffer, record buffer->used before execution and compare to buffer->used after execution.
*
* This is different from simply using the file.size_read.
- * The file.size_read represents the amount to process for a given buffer size.
- * The total represents the maximum number of "size" to process.
+ * The file.size_read represents the amount to process at a given time.
+ * The total represents the maximum number of file.size_read to process.
* For example, if file.size_read is 16 and total is 128, then this function would need to be called 8 times until total is reached.
*
* @param file
* The file to read.
* The file must already be open.
- * @param amount
- * The total amount of file.size_read to process.
- * This amount is multiplied against file.size_read and must be greater than 0.
+ * The file.size_read represents the amount to process at a given time.
* @param total
* The total bytes to read, unless EOF is reached first.
* @param buffer
* @see fread()
*/
#ifndef _di_f_file_stream_read_until_
- extern f_status_t f_file_stream_read_until(const f_file_t file, const f_array_length_t amount, const f_array_length_t total, f_string_dynamic_t *buffer);
+ extern f_status_t f_file_stream_read_until(const f_file_t file, const f_array_length_t total, f_string_dynamic_t *buffer);
#endif // _di_f_file_stream_read_until_
/**
*
* @return
* F_none is returned on success.
- * F_access_denied (with error bit) on access denied.
*
+ * F_access_denied (with error bit) on access denied.
* F_buffer (with error bit) if the buffer is invalid.
* F_busy (with error bit) if filesystem is too busy to perform write.
* F_file_descriptor (with error bit) if unable to load the file descriptor.
* @param file
* The file to write to.
* The file must already be open.
+ * The file.size_write represents the amount to process at a given time.
* @param buffer
* The buffer to write to the file.
- * @param amount
- * The total amount of file.size_write to process.
- * This amount is multiplied against file.size_write and must be greater than 0.
* @param written
* The total bytes written.
* Set pointer to 0 to not use.
* @see fwrite()
*/
#ifndef _di_f_file_stream_write_
- extern f_status_t f_file_stream_write(const f_file_t file, const f_string_static_t buffer, const f_array_length_t amount, f_array_length_t *written);
+ extern f_status_t f_file_stream_write(const f_file_t file, const f_string_static_t buffer, f_array_length_t *written);
#endif // _di_f_file_stream_write_
/**
* @param file
* The file to write to.
* The file must already be open.
+ * The file.size_write represents the amount to process at a given time.
* @param buffer
* The buffer to write to the file.
- * @param amount
- * The total amount of file.size_write to process.
- * This amount is multiplied against file.size_write and must be greater than 0.
* @param written
* The total bytes written.
* Set pointer to 0 to not use.
* @see fwrite()
*/
#ifndef _di_f_file_stream_write_block_
- extern f_status_t f_file_stream_write_block(const f_file_t file, const f_string_static_t buffer, const f_array_length_t amount, f_array_length_t *written);
+ extern f_status_t f_file_stream_write_block(const f_file_t file, const f_string_static_t buffer, f_array_length_t *written);
#endif // _di_f_file_stream_write_block_
/**
* @param file
* The file to write to.
* The file must already be open.
+ * The file.size_write represents the amount to process at a given time.
* @param buffer
* The buffer to write to the file.
- * @param amount
- * The total amount of file.size_write to process.
- * This amount is multiplied against file.size_write and must be greater than 0.
* @param total
* The total bytes to write, unless end of buffer is reached first.
* @param written
* @see fwrite()
*/
#ifndef _di_f_file_stream_write_until_
- extern f_status_t f_file_stream_write_until(const f_file_t file, const f_string_static_t buffer, const f_array_length_t amount, const f_array_length_t total, f_array_length_t *written);
+ extern f_status_t f_file_stream_write_until(const f_file_t file, const f_string_static_t buffer, const f_array_length_t total, f_array_length_t *written);
#endif // _di_f_file_stream_write_until_
/**
* @param file
* The file to write to.
* The file must already be open.
+ * The file.size_write represents the amount to process at a given time.
* @param buffer
* The buffer to write to the file.
- * @param amount
- * The total amount of file.size_write to process.
- * This amount is multiplied against file.size_write and must be greater than 0.
* @param range
* An inclusive start an stop range within the buffer to read.
* @param written
* @see fwrite()
*/
#ifndef _di_f_file_stream_write_range_
- extern f_status_t f_file_stream_write_range(const f_file_t file, const f_string_static_t buffer, const f_array_length_t amount, const f_string_range_t range, f_array_length_t *written);
+ extern f_status_t f_file_stream_write_range(const f_file_t file, const f_string_static_t buffer, const f_string_range_t range, f_array_length_t *written);
#endif // _di_f_file_stream_write_range_
#if !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_)
f_status_t private_f_file_close(const bool flush, int *id) {
- if (id == 0 || *id == -1) return F_none;
+ if (id == 0 || *id == -1) {
+ return F_none;
+ }
if (flush && F_status_is_error(private_f_file_flush(*id))) {
return F_status_set_error(F_file_synchronize);
private_f_file_close(F_true, &file_destination.id);
private_f_file_close(F_true, &file_source.id);
- if (size_read < 0) return F_status_set_error(F_file_read);
+ if (size_read < 0) {
+ return F_status_set_error(F_file_read);
+ }
return F_none;
}
private_f_file_close(F_true, &file_destination.id);
private_f_file_close(F_true, &file_source.id);
- if (size_read < 0) return F_status_set_error(F_file_read);
+ if (size_read < 0) {
+ return F_status_set_error(F_file_read);
+ }
return F_none;
}
if (uid != -1) {
result = chown(path, uid, -1);
- if (result < 0 && errno == EPERM) return F_status_set_error(F_access_owner);
+ if (result < 0 && errno == EPERM) {
+ return F_status_set_error(F_access_owner);
+ }
}
if (result == 0 && gid != -1) {
result = chown(path, -1, gid);
- if (result < 0 && errno == EPERM) return F_status_set_error(F_access_group);
+ if (result < 0 && errno == EPERM) {
+ return F_status_set_error(F_access_group);
+ }
}
}
else {
if (uid != -1) {
result = lchown(path, uid, -1);
- if (result < 0 && errno == EPERM) return F_status_set_error(F_access_owner);
+ if (result < 0 && errno == EPERM) {
+ return F_status_set_error(F_access_owner);
+ }
}
if (gid != -1) {
result = lchown(path, -1, gid);
- if (result < 0 && errno == EPERM) return F_status_set_error(F_access_group);
+ if (result < 0 && errno == EPERM) {
+ return F_status_set_error(F_access_group);
+ }
}
}
if (uid != -1) {
result = fchownat(at_id, path, uid, -1, flag);
- if (result < 0 && errno == EPERM) return F_status_set_error(F_access_owner);
+ if (result < 0 && errno == EPERM) {
+ return F_status_set_error(F_access_owner);
+ }
}
if (gid != -1) {
result = fchownat(at_id, path, -1, gid, flag);
- if (result < 0 && errno == EPERM) return F_status_set_error(F_access_group);
+ if (result < 0 && errno == EPERM) {
+ return F_status_set_error(F_access_group);
+ }
}
if (result < 0) {
#endif // !defined(_di_f_file_stream_descriptor_) || !defined(_di_f_file_stream_open_) || !defined(_di_f_file_stream_reopen_)
#if !defined(f_file_stream_write) || !defined(_di_f_file_stream_write_block_) || !defined(f_file_stream_write_until) || !defined(f_file_stream_write_range)
- f_status_t private_f_file_stream_write_until(const f_file_t file, const f_string_t string, const f_array_length_t amount, const f_array_length_t total, f_array_length_t *written) {
+ f_status_t private_f_file_stream_write_until(const f_file_t file, const f_string_t string, const f_array_length_t total, f_array_length_t *written) {
*written = 0;
f_status_t status = F_none;
- f_array_length_t write_amount = amount;
+ f_array_length_t write_amount = 1;
f_array_length_t write_size = file.size_write;
f_array_length_t write_max = total;
write_amount = 1;
write_size = write_max;
}
- else if (amount * file.size_write > total) {
+ else if (file.size_write > total) {
write_amount = total / file.size_write;
if (total % file.size_write) {
}
while (*written < write_max) {
+
size_write = fwrite(string + *written, write_amount, write_size, file.stream);
if (size_write < 0) {
}
while (*written < write_max && (size_write = write(file.id, string + *written, write_size)) > 0) {
+
*written += size_write;
if (*written + write_size > write_max) {
* The file must already be open.
* @param string
* The string to write to the file.
- * @param amount
- * The total amount of file.size_write to process.
- * This amount is multiplied against file.size_write and must be greater than 0.
* @param total
* The total bytes to write.
* @param written
* @see f_file_stream_write_until()
*/
#if !defined(f_file_stream_write) || !defined(_di_f_file_stream_write_block_) || !defined(f_file_stream_write_until) || !defined(f_file_stream_write_range)
- extern f_status_t private_f_file_stream_write_until(const f_file_t file, const f_string_t string, const f_array_length_t amount, const f_array_length_t total, f_array_length_t *written) f_attribute_visibility_internal;
+ extern f_status_t private_f_file_stream_write_until(const f_file_t file, const f_string_t string, const f_array_length_t total, f_array_length_t *written) f_attribute_visibility_internal;
#endif // !defined(f_file_stream_write) || !defined(_di_f_file_stream_write_block_) || !defined(f_file_stream_write_until) || !defined(f_file_stream_write_range)
/**
}
}
else {
- status = f_file_stream_read(file, 1, &cache->buffer_file);
+ status = f_file_stream_read(file, &cache->buffer_file);
if (F_status_is_error(status)) {
if (global.main->error.verbosity != f_console_verbosity_quiet) {
f_string_dynamic_t pid_buffer = f_string_dynamic_t_initialize;
- status = f_file_stream_read(pid_file, 1, &pid_buffer);
+ status = f_file_stream_read(pid_file, &pid_buffer);
if (F_status_is_error_not(status)) {
status = f_file_stream_close(F_true, &pid_file);
f_string_dynamic_t pid_buffer = f_string_dynamic_t_initialize;
- status = f_file_stream_read(pid_file, 1, &pid_buffer);
+ status = f_file_stream_read(pid_file, &pid_buffer);
if (F_status_is_error_not(status)) {
status = f_file_stream_close(F_true, &pid_file);
*
* @see controller_rule_items_increase_by().
* @see controller_rule_item_read().
- * @see f_file_stream_open().
- * @see f_file_stream_read().
* @see f_fss_count_lines().
* @see fl_fss_apply_delimit().
* @see f_string_dynamic_partial_append().
files.array[0].name = 0;
files.array[0].range.start = 0;
- status = f_file_stream_read(file, 1, &main->buffer);
+ status = f_file_stream_read(file, &main->buffer);
if (F_status_is_error(status)) {
fll_error_file_print(main->error, F_status_set_fine(status), "f_file_stream_read", F_true, "-", "read", fll_error_file_type_pipe);
break;
}
- status = f_file_stream_read(file, 1, &main->buffer);
+ status = f_file_stream_read(file, &main->buffer);
f_file_stream_close(F_true, &file);