Setting Values in DtwResource
May 26, 2025 ยท View on GitHub
This document provides a guide on how to set different types of values to resources using the DtwResource API. It covers setting strings, binary data, numeric values, and boolean values, as well as organizing resources into subfolders. The examples provided demonstrate practical usage of the API for creating and managing resources in a specified directory.
Overview
The DtwResource API allows developers to create and manage resources (files and directories) in a structured manner. Resources can store various types of data, including strings, binary content, integers, doubles, and booleans. This API also supports hierarchical organization through sub-resources (subfolders and files).
Prerequisites
- Include the necessary header file for the
DtwResourceAPI (e.g.,doTheWorldOne.c). - Ensure the target directory exists or is writable for resource creation.
Setting Different Types of Values
Below are the key methods for setting values to resources, along with detailed explanations and a complete example.
1. Initializing a Resource
To start working with resources, initialize a DtwResource object by specifying the root directory where the resources will be created or managed.
DtwResource *values = new_DtwResource("tests/target/new_folder");
- Parameter: The path to the root directory (
tests/target/new_folderin this case). - Note: If the directory does not exist, it will be created automatically upon committing changes.
2. Setting a String Value
You can store a string in a resource (file) using the DtwResource_set_string method.
DtwResource *string_element = DtwResource_sub_resource(values, "text.txt");
DtwResource_set_string(string_element, "nothing");
- Steps:
- Create a sub-resource (file) named
text.txtunder the root resourcevalues. - Set the string content
"nothing"to this resource.
- Create a sub-resource (file) named
- Result: A file named
text.txtwill be created with the content"nothing".
3. Setting Binary Data
Binary data, such as images or other non-text files, can be stored using the DtwResource_set_binary method.
long size;
unsigned char *blob = dtw_load_binary_content("tests/target/blob.png", &size);
DtwResource *blob_element = DtwResource_sub_resource(values, "blob.png");
DtwResource_set_binary(blob_element, blob, size);
free(blob);
- Steps:
- Load binary content from a source file (
blob.png) into a buffer (blob) and retrieve its size. - Create a sub-resource named
blob.pngunder the root resourcevalues. - Set the binary content to this resource using the buffer and size.
- Free the allocated buffer to prevent memory leaks.
- Load binary content from a source file (
- Result: A file named
blob.pngwill be created with the binary content of the source file.
4. Setting Numeric Values
The API supports setting long integers and double-precision floating-point numbers.
Setting a Long Integer
DtwResource *bInt = DtwResource_sub_resource(values, "b.txt");
DtwResource_set_long(bInt, 25);
- Steps:
- Create a sub-resource named
b.txt. - Set the long integer value
25to this resource.
- Create a sub-resource named
- Result: A file named
b.txtwill be created with the text representation of the value25.
Setting a Double Value
DtwResource *cDouble = DtwResource_sub_resource(values, "c.txt");
DtwResource_set_double(cDouble, 10.5);
- Steps:
- Create a sub-resource named
c.txt. - Set the double value
10.5to this resource.
- Create a sub-resource named
- Result: A file named
c.txtwill be created with the text representation of the value10.5.
5. Setting a Boolean Value
Boolean values can be stored using the DtwResource_set_bool method.
DtwResource *dBool = DtwResource_sub_resource(values, "d.txt");
DtwResource_set_bool(dBool, true);
- Steps:
- Create a sub-resource named
d.txt. - Set the boolean value
trueto this resource.
- Create a sub-resource named
- Result: A file named
d.txtwill be created with the text representation of the boolean value (e.g.,1ortruedepending on implementation).
6. Organizing Resources in Subfolders
Resources can be organized hierarchically by creating sub-resources as folders.
DtwResource *sub_folder = DtwResource_sub_resource(values, "sub_folder");
DtwResource *string_element2 = DtwResource_sub_resource(sub_folder, "a.txt");
DtwResource_set_string(string_element2, "nothing");
- Steps:
- Create a sub-resource named
sub_folderunder the root resourcevalues(this acts as a directory). - Create a sub-resource named
a.txtundersub_folder. - Set the string content
"nothing"toa.txt.
- Create a sub-resource named
- Result: A directory named
sub_folderwill be created, containing a filea.txtwith the content"nothing".
7. Committing Changes
After setting values to resources, commit the changes to ensure they are written to the filesystem.
DtwResource_commit(values);
- Note: Without committing, the changes may not be persisted to the disk.
8. Freeing Resources
To prevent memory leaks, free the DtwResource object after use.
DtwResource_free(values);
- Note: This does not delete the files or directories created; it only releases the memory used by the
DtwResourcestructure.
Complete Example
Below is a complete code example demonstrating the above operations:
#include "doTheWorldOne.c"
int main() {
// Initialize the root resource
DtwResource *values = new_DtwResource("tests/target/new_folder");
// Set a string value
DtwResource *string_element = DtwResource_sub_resource(values, "text.txt");
DtwResource_set_string(string_element, "nothing");
// Set binary data
long size;
unsigned char *blob = dtw_load_binary_content("tests/target/blob.png", &size);
DtwResource *blob_element = DtwResource_sub_resource(values, "blob.png");
DtwResource_set_binary(blob_element, blob, size);
free(blob);
// Set a long integer
DtwResource *bInt = DtwResource_sub_resource(values, "b.txt");
DtwResource_set_long(bInt, 25);
// Set a double value
DtwResource *cDouble = DtwResource_sub_resource(values, "c.txt");
DtwResource_set_double(cDouble, 10.5);
// Set a boolean value
DtwResource *dBool = DtwResource_sub_resource(values, "d.txt");
DtwResource_set_bool(dBool, true);
// Create a subfolder and set a string value inside it
DtwResource *sub_folder = DtwResource_sub_resource(values, "sub_folder");
DtwResource *string_element2 = DtwResource_sub_resource(sub_folder, "a.txt");
DtwResource_set_string(string_element2, "nothing");
// Commit changes to the filesystem
DtwResource_commit(values);
// Free the resource to prevent memory leaks
DtwResource_free(values);
return 0;
}
Notes
- Ensure the target directory has appropriate write permissions to avoid errors during resource creation or data writing.
- Overlapping resource names (e.g., setting multiple values to the same file) may result in overwriting data. Be cautious with resource naming.
- The
DtwResource_commitmethod must be called to persist changes. Without it, the operations remain in memory and are not written to disk. - Always free allocated memory (e.g., binary data buffers) and
DtwResourceobjects to prevent memory leaks.
Troubleshooting
- File Not Created: Verify that the directory path specified in
new_DtwResourceis accessible and writable. - Data Overwritten: Check for duplicate resource names, as setting a new value to an existing resource may overwrite the previous content.
- Memory Issues: Ensure all dynamically allocated memory (e.g., binary data buffers) and
DtwResourceobjects are properly freed.