| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <unistd.h> |
|
|
| #define MAX_NOTES 16 |
| #define MAX_SIZE 0x80 |
|
|
| struct note { |
| char *data; |
| size_t size; |
| int in_use; |
| }; |
|
|
| struct note notes[MAX_NOTES]; |
|
|
| void alloc_note() { |
| int idx, size; |
| if (scanf("%d %d", &idx, &size) != 2) return; |
| if (idx < 0 || idx >= MAX_NOTES || size <= 0 || size > MAX_SIZE) return; |
| if (notes[idx].data != NULL) return; |
|
|
| notes[idx].data = malloc(size); |
| if (!notes[idx].data) return; |
| memset(notes[idx].data, 0, size); |
| notes[idx].size = size; |
| notes[idx].in_use = 1; |
| } |
|
|
| void edit_note() { |
| int idx; |
| if (scanf("%d", &idx) != 1) return; |
| if (idx < 0 || idx >= MAX_NOTES || notes[idx].data == NULL) return; |
|
|
| |
| char buf[MAX_SIZE + 1]; |
| size_t sz = notes[idx].size; |
| if (sz > MAX_SIZE) sz = MAX_SIZE; |
|
|
| |
| char hex[MAX_SIZE * 2 + 4]; |
| if (scanf("%s", hex) != 1) return; |
|
|
| size_t len = strlen(hex) / 2; |
| if (len > sz) len = sz; |
|
|
| for (size_t i = 0; i < len; i++) { |
| unsigned int byte; |
| sscanf(hex + i * 2, "%2x", &byte); |
| buf[i] = (char)byte; |
| } |
|
|
| |
| memcpy(notes[idx].data, buf, len); |
| notes[idx].data[len] = '\0'; |
| } |
|
|
| void show_note() { |
| int idx; |
| if (scanf("%d", &idx) != 1) return; |
| if (idx < 0 || idx >= MAX_NOTES || notes[idx].data == NULL) return; |
|
|
| |
| write(STDOUT_FILENO, notes[idx].data, notes[idx].size); |
| write(STDOUT_FILENO, "\n", 1); |
| } |
|
|
| void delete_note() { |
| int idx; |
| if (scanf("%d", &idx) != 1) return; |
| if (idx < 0 || idx >= MAX_NOTES || notes[idx].data == NULL) return; |
|
|
| free(notes[idx].data); |
| |
| notes[idx].in_use = 0; |
| |
| } |
|
|
| int main() { |
| setbuf(stdin, NULL); |
| setbuf(stdout, NULL); |
| setbuf(stderr, NULL); |
|
|
| int choice; |
| while (1) { |
| if (scanf("%d", &choice) != 1) break; |
| switch (choice) { |
| case 1: alloc_note(); break; |
| case 2: edit_note(); break; |
| case 3: show_note(); break; |
| case 4: delete_note(); break; |
| case 5: return 0; |
| default: break; |
| } |
| } |
| return 0; |
| } |
|
|