Changeset da0fef6 in mainline
- Timestamp:
- 2012-06-23T18:24:21Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 03b2b2c
- Parents:
- d5070ef
- Location:
- uspace/app/bithenge
- Files:
-
- 5 added
- 9 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/Makefile
rd5070ef rda0fef6 29 29 USPACE_PREFIX = ../.. 30 30 LIBS = $(LIBBLOCK_PREFIX)/libblock.a 31 EXTRA_CFLAGS = -I$(LIBBLOCK_PREFIX) 31 EXTRA_CFLAGS = -I$(LIBBLOCK_PREFIX) -D__HELENOS__ -Ihelenos 32 32 BINARY = bithenge 33 33 34 34 SOURCES = \ 35 helenos/block.c \ 35 36 blob.c \ 36 block.c \37 37 file.c \ 38 38 print.c \ 39 source.c \ 39 40 test.c \ 40 41 transform.c \ -
uspace/app/bithenge/blob.c
rd5070ef rda0fef6 36 36 37 37 #include <assert.h> 38 #include <bool.h>39 38 #include <errno.h> 40 #include <macros.h>41 #include <mem.h>42 39 #include <stdlib.h> 43 40 #include "blob.h" 41 #include "os.h" 44 42 #include "tree.h" 45 43 … … 215 213 memory_blob_t *blob = memory_from_blob(base); 216 214 if (blob->needs_free) 217 free( blob->buffer);215 free((void *)blob->buffer); 218 216 free(blob); 219 217 return EOK; … … 272 270 * destroyed. 273 271 * @param len The length of the data. 274 * @param needs_free Whether the buffer should be freed with free() when the275 * blob is destroyed.272 * @param needs_free If true, the buffer will be freed with free() if this 273 * function fails or the blob is destroyed. 276 274 * @return EOK on success or an error code from errno.h. */ 277 275 int bithenge_new_blob_from_buffer(bithenge_node_t **out, const void *buffer, -
uspace/app/bithenge/file.c
rd5070ef rda0fef6 39 39 #include <errno.h> 40 40 #include <fcntl.h> 41 #include <macros.h>42 41 #include <stdio.h> 43 42 #include <stdlib.h> 44 43 #include <sys/stat.h> 44 #include <sys/types.h> 45 #include <unistd.h> 45 46 #include "blob.h" 46 47 #include "file.h" 48 #include "os.h" 47 49 48 50 typedef struct { … … 76 78 if (offset > blob->size) 77 79 return ELIMIT; 78 *size = min(*size, blob->size - offset); 79 int rc = lseek(blob->fd, offset, SEEK_SET); 80 if (rc != EOK) 81 return rc; 82 return read(blob->fd, buffer, *size); 80 if (lseek(blob->fd, offset, SEEK_SET) < 0) 81 return errno; 82 83 ssize_t amount_read; 84 aoff64_t remaining_size = *size; 85 *size = 0; 86 do { 87 amount_read = read(blob->fd, buffer, remaining_size); 88 if (amount_read < 0) 89 return errno; 90 buffer += amount_read; 91 *size += amount_read; 92 remaining_size -= amount_read; 93 } while (remaining_size && amount_read); 94 return EOK; 83 95 } 84 96 … … 125 137 } 126 138 blob->fd = fd; 139 #ifdef __HELENOS__ 127 140 blob->size = stat.size; 141 #else 142 blob->size = stat.st_size; 143 #endif 128 144 blob->needs_close = needs_close; 129 145 *out = bithenge_blob_as_node(blob_from_file(blob)); -
uspace/app/bithenge/helenos/block.c
rd5070ef rda0fef6 42 42 #include <macros.h> 43 43 #include <stdlib.h> 44 #include " blob.h"44 #include "../blob.h" 45 45 #include "block.h" 46 46 -
uspace/app/bithenge/helenos/block.h
rd5070ef rda0fef6 39 39 40 40 #include <loc.h> 41 #include " blob.h"41 #include "../blob.h" 42 42 43 43 int bithenge_new_block_blob(bithenge_node_t **, service_id_t); -
uspace/app/bithenge/print.c
rd5070ef rda0fef6 104 104 static int print_string(bithenge_print_type_t type, bithenge_node_t *node) 105 105 { 106 size_t off = 0;107 106 const char *value = bithenge_string_node_value(node); 108 wchar_t ch;109 107 printf("\""); 110 while ((ch = str_decode(value, &off, STR_NO_LIMIT)) != 0) { 108 for (string_iterator_t i = string_iterator(value); !string_iterator_done(&i); ) { 109 wchar_t ch; 110 int rc = string_iterator_next(&i, &ch); 111 if (rc != EOK) 112 return rc; 111 113 if (ch == '"' || ch == '\\') { 112 114 printf("\\%lc", (wint_t) ch); … … 134 136 return rc; 135 137 for (aoff64_t i = 0; i < size; i++) 136 printf("\\x%02x", buffer[i]);138 printf("\\x%02x", (unsigned int)(uint8_t)buffer[i]); 137 139 pos += size; 138 140 } while (size == sizeof(buffer)); -
uspace/app/bithenge/test.c
rd5070ef rda0fef6 35 35 */ 36 36 37 #include < loc.h>37 #include <errno.h> 38 38 #include <stdio.h> 39 #include <stdlib.h> 39 40 #include <sys/types.h> 40 41 #include "blob.h" 41 #include "block.h" 42 #include "file.h" 42 #include "source.h" 43 43 #include "print.h" 44 44 #include "tree.h" 45 45 46 static void47 print_data(const char *data, size_t len)48 {49 while (len--)50 printf("%02x ", (uint8_t)(*data++));51 printf("\n");52 }53 54 static void55 print_blob(bithenge_node_t *node)56 {57 bithenge_blob_t *blob = bithenge_node_as_blob(node);58 aoff64_t size;59 bithenge_blob_size(blob, &size);60 printf("Size: %d; ", (int)size);61 char buffer[64];62 size = sizeof(buffer);63 bithenge_blob_read(blob, 0, buffer, &size);64 print_data(buffer, size);65 }66 67 46 int main(int argc, char *argv[]) 68 47 { 69 bithenge_node_t *node; 70 71 service_id_t service_id; 72 loc_service_get_id("bd/initrd", &service_id, 0); 73 bithenge_new_block_blob(&node, service_id); 74 printf("Data from block:bd/initrd: "); 75 print_blob(node); 76 bithenge_node_destroy(node); 77 78 const char data[] = "'Twas brillig, and the slithy toves"; 79 bithenge_new_blob_from_data(&node, data, sizeof(data)); 80 printf("Data from memory (from_data): "); 81 print_blob(node); 82 bithenge_node_destroy(node); 83 84 bithenge_new_blob_from_buffer(&node, data, sizeof(data), false); 85 printf("Data from memory (from_buffer): "); 86 print_blob(node); 87 bithenge_node_destroy(node); 88 89 bithenge_new_file_blob(&node, "/textdemo"); 90 printf("Data from file:/textdemo: "); 91 print_blob(node); 92 bithenge_node_destroy(node); 93 94 bithenge_new_file_blob_from_fd(&node, 0); 95 printf("Data from fd:0: "); 96 print_blob(node); 97 bithenge_node_destroy(node); 98 99 // {True: {}, -1351: "\"false\"", "true": False, 0: b"..."} 100 bithenge_node_t *nodes[8]; 101 bithenge_new_boolean_node(&nodes[0], true); 102 bithenge_new_simple_internal_node(&nodes[1], NULL, 0, false); 103 bithenge_new_integer_node(&nodes[2], -1351); 104 bithenge_new_string_node(&nodes[3], "\"false\"", false); 105 bithenge_new_string_node(&nodes[4], "true", false); 106 bithenge_new_boolean_node(&nodes[5], false); 107 bithenge_new_integer_node(&nodes[6], 0); 108 bithenge_new_blob_from_data(&nodes[7], data, sizeof(data)); 109 bithenge_new_simple_internal_node(&node, nodes, 4, false); 110 bithenge_print_node(BITHENGE_PRINT_PYTHON, node); 111 printf("\n"); 112 bithenge_print_node(BITHENGE_PRINT_JSON, node); 113 printf("\n"); 114 bithenge_node_destroy(node); 48 if (argc < 2) { 49 // {True: {}, -1351: "\"false\"", "true": False, 0: b"..."} 50 const char data[] = "'Twas brillig, and the slithy toves"; 51 bithenge_node_t *node; 52 bithenge_node_t *subnodes[8]; 53 bithenge_new_boolean_node(&subnodes[0], true); 54 bithenge_new_simple_internal_node(&subnodes[1], NULL, 0, false); 55 bithenge_new_integer_node(&subnodes[2], -1351); 56 bithenge_new_string_node(&subnodes[3], "\"false\"", false); 57 bithenge_new_string_node(&subnodes[4], "true", false); 58 bithenge_new_boolean_node(&subnodes[5], false); 59 bithenge_new_integer_node(&subnodes[6], 0); 60 bithenge_new_blob_from_data(&subnodes[7], data, sizeof(data)); 61 bithenge_new_simple_internal_node(&node, subnodes, 4, false); 62 bithenge_print_node(BITHENGE_PRINT_PYTHON, node); 63 printf("\n"); 64 bithenge_print_node(BITHENGE_PRINT_JSON, node); 65 printf("\n"); 66 bithenge_node_destroy(node); 67 } else { 68 bithenge_node_t *node; 69 int rc = bithenge_node_from_source(&node, argv[1]); 70 if (rc != EOK) { 71 printf("Error creating node from source: %s\n", str_error(rc)); 72 return 1; 73 } 74 rc = bithenge_print_node(BITHENGE_PRINT_PYTHON, node); 75 if (rc != EOK) { 76 printf("Error printing node: %s\n", str_error(rc)); 77 return 1; 78 } 79 printf("\n"); 80 bithenge_node_destroy(node); 81 } 115 82 116 83 return 0; -
uspace/app/bithenge/transform.c
rd5070ef rda0fef6 35 35 */ 36 36 37 #include <byteorder.h>38 37 #include <errno.h> 39 38 #include "blob.h" -
uspace/app/bithenge/transform.h
rd5070ef rda0fef6 75 75 /** Find the length of the prefix of a blob this transform can use as input. In 76 76 * other words, figure out how many bytes this transform will use up. This 77 * method is optional and can return an error, but it is required for struct77 * method is optional and can return an error, but it must succeed for struct 78 78 * subtransforms. 79 79 * @memberof bithenge_transform_t -
uspace/app/bithenge/tree.c
rd5070ef rda0fef6 37 37 #include <errno.h> 38 38 #include <stdlib.h> 39 #include <str.h>40 39 #include "blob.h" 40 #include "os.h" 41 41 #include "tree.h" 42 42 … … 59 59 case BITHENGE_NODE_STRING: 60 60 if (node->string_value.needs_free) 61 free( node->string_value.ptr);61 free((void *)node->string_value.ptr); 62 62 break; 63 63 case BITHENGE_NODE_INTERNAL: -
uspace/app/bithenge/tree.h
rd5070ef rda0fef6 39 39 40 40 #include <assert.h> 41 #include <bool.h>42 41 #include <inttypes.h> 43 42 #include <sys/types.h> 43 #include "os.h" 44 44 45 45 #ifdef INTMAX_MAX
Note:
See TracChangeset
for help on using the changeset viewer.