test_write.c (2462B)
1 /* 2 * Copy me if you can. 3 * by 20h 4 */ 5 6 #ifndef PLAN9 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <fcntl.h> 10 #endif 11 #ifdef PLAN9 12 #include <u.h> 13 #include <libc.h> 14 #endif 15 #include "xmlpull.h" 16 17 int 18 main(int argc, char *argv[]) 19 { 20 xmlpull *ret; 21 int fd; 22 23 if(argc < 2){ 24 print("usage: filename\n"); 25 exits(0); 26 } 27 28 fd = open(argv[1], 29 #ifndef PLAN9 30 O_WRONLY); 31 #endif 32 #ifdef PLAN9 33 OWRITE); 34 #endif 35 if(fd < 0){ 36 perror("open"); 37 exits(0); 38 } 39 40 ret = openxmlpull(fd); 41 42 ret->nev = START_DOCUMENT; 43 if(writexmlpull(ret) == nil) 44 goto end_main; 45 46 ret->nev = START_TAG; 47 ret->na = "message"; 48 ret->ln = 7; 49 if(writexmlpull(ret) == nil) 50 goto end_main; 51 52 ret->nev = ATTR; 53 ret->na = "to"; 54 ret->ln = 2; 55 ret->va = "user"; 56 ret->lv = 4; 57 if(writexmlpull(ret) == nil) 58 goto end_main; 59 60 ret->nev = ATTR; 61 ret->na = "from"; 62 ret->ln = 4; 63 ret->va = "programmer"; 64 ret->lv = 10; 65 if(writexmlpull(ret) == nil) 66 goto end_main; 67 68 ret->nev = END_TAG_N; 69 if(writexmlpull(ret) == nil) 70 goto end_main; 71 72 if(write(ret->fd, "\n", 1) < 0) 73 goto end_main; 74 75 ret->nev = START_TAG; 76 ret->na = "value"; 77 ret->ln = 5; 78 if(writexmlpull(ret) == nil) 79 goto end_main; 80 81 if(write(ret->fd, "\n", 1) < 0) 82 goto end_main; 83 84 ret->nev = END_TAG_N; 85 if(writexmlpull(ret) == nil) 86 goto end_main; 87 88 ret->nev = START_TAG; 89 ret->na = "!--"; 90 ret->ln = 3; 91 if(writexmlpull(ret) == nil) 92 goto end_main; 93 94 ret->nev = TEXT_C; 95 ret->na = "Here comes the message."; 96 ret->ln = 23; 97 if(writexmlpull(ret) == nil) 98 goto end_main; 99 100 if(write(ret->fd, "\n", 1) < 0) 101 goto end_main; 102 103 ret->nev = TEXT; 104 ret->na = "Thank you for using Libxmlpull.\n"; 105 ret->ln = 32; 106 if(writexmlpull(ret) == nil) 107 goto end_main; 108 109 ret->nev = END_TAG; 110 ret->na = "value"; 111 ret->ln = 5; 112 if(writexmlpull(ret) == nil) 113 goto end_main; 114 115 if(write(ret->fd, "\n", 1) < 0) 116 goto end_main; 117 118 ret->nev = START_TAG; 119 ret->na = "attachments"; 120 ret->ln = 11; 121 if(writexmlpull(ret) == nil) 122 goto end_main; 123 124 ret->nev = ATTR; 125 ret->na = "num"; 126 ret->ln = 3; 127 ret->va = "0"; 128 ret->lv = 1; 129 if(writexmlpull(ret) == nil) 130 goto end_main; 131 132 ret->nev = END_TAG_S; 133 if(writexmlpull(ret) == nil) 134 goto end_main; 135 136 if(write(ret->fd, "\n", 1) < 0) 137 goto end_main; 138 139 ret->nev = END_TAG; 140 ret->na = "message"; 141 ret->ln = 7; 142 if(writexmlpull(ret) == nil) 143 goto end_main; 144 145 if(write(ret->fd, "\n", 1) < 0) 146 goto end_main; 147 148 ret->nev = END_DOCUMENT; 149 if(writexmlpull(ret) == nil) 150 goto end_main; 151 152 end_main: 153 free(ret); 154 exits(0); 155 } 156