JunkBox_Lib++ (for Windows) 1.10.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
buffer.cpp
Go to the documentation of this file.
1
10#include "buffer.h"
11#include "jbxl_state.h"
12
13
24{
25 Buffer* buf;
26
27 buf = (Buffer*)malloc(sizeof(Buffer));
28 if (buf==NULL) return NULL;
29
30 memset(buf, 0, sizeof(Buffer));
31 buf->vldsz = -1;
32 buf->state = JBXL_NORMAL;
33
34 if (sz>0) *buf = make_Buffer(sz + 1);
35 return buf;
36}
37
38
48{
49 Buffer buf;
50
51 memset(&buf, 0, sizeof(Buffer));
52 buf.vldsz = -1;
53 buf.state = JBXL_NORMAL;
54
55 return buf;
56}
57
58
72{
73 Buffer buf;
74
75 memset(&buf, 0, sizeof(Buffer));
76 buf.state = JBXL_NORMAL;
77 if (sz<0) return buf;
78
79 buf.bufsz = sz;
80 buf.buf = (unsigned char*)malloc(buf.bufsz+1);
81
82 if (buf.buf==NULL) {
83 buf.bufsz = 0;
84 buf.vldsz = -1;
85 buf.state = JBXL_ERROR;
86 }
87 else {
88 memset(buf.buf, 0, buf.bufsz+1);
89 }
90
91 return buf;
92}
93
94
108Buffer make_Buffer_bychar(unsigned char cc)
109{
110 Buffer buf;
111
112 buf = make_Buffer(LADDR);
113 if (buf.buf==NULL) return buf;
114
115 buf.buf[0] = cc;
116 buf.vldsz = 1;
117 return buf;
118}
119
120
129{
130 if (buf!=NULL) {
131 if (buf->buf!=NULL) free(buf->buf);
132 buf->buf = NULL;
133 memset(buf, 0, sizeof(Buffer));
134 buf->vldsz = -1;
135 buf->state = JBXL_NORMAL;
136 }
137}
138
139
149{
150 if (buf!=NULL && *buf!=NULL) {
151 if ((*buf)->buf!=NULL) free((*buf)->buf);
152 (*buf)->buf = NULL;
153 free(*buf);
154 *buf = NULL;
155 }
156}
157
158
170Buffer set_Buffer(void* dat, int len)
171{
172 Buffer buf;
173
174 buf = init_Buffer();
175 if (dat==NULL) return buf;
176
177 if (len<0) len = (int)strlen((char*)dat);
178 buf = make_Buffer(len);
179 if (buf.buf==NULL) return buf;
180
181 memcpy(buf.buf, dat, len);
182 buf.vldsz = len;
183 return buf;
184}
185
186
193{
194 char* str = random_str(n);
195
196 Buffer buf = set_Buffer(str, -1);
197 free(str);
198
199 return buf;
200}
201
202
212{
213 Buffer str = buf;
214
215 if (buf.bufsz>=0 && buf.buf!=NULL) {
216 str.buf = (unsigned char*)malloc(buf.bufsz+1);
217 memcpy(str.buf, buf.buf, buf.bufsz);
218 str.buf[buf.bufsz] = '\0';
219 }
220 //
221 return str;
222}
223
224
234Buffer rept_Buffer(unsigned char cc, int n)
235{
236 int i;
237 Buffer str;
238
239 if (n<=0) n = 1;
240 str = make_Buffer(n);
241 for (i=0; i<n; i++) str.buf[i] = cc;
242 str.vldsz = n;
243 return str;
244}
245
246
253void expand_Buffer(Buffer* buf, int len)
254{
255 if (buf->bufsz>len) return;
256
257 Buffer expand = make_Buffer(len);
258 copy_Buffer(buf, &expand);
259 free_Buffer(buf);
260
261 *buf = expand;
262}
263
264
273{
274 if (str->buf!=NULL) memset(str->buf, 0, str->bufsz+1);
275 str->vldsz = 0;
276 str->state = JBXL_NORMAL;
277}
278
279
315int copy_Buffer(Buffer* src, Buffer* dst)
316{
317 int sz, dz;
318
319 if (src==NULL || dst==NULL) return JBXL_NULL_ERROR;
320 if (src->buf==NULL) return JBXL_NULL_ERROR;
321
322 sz = src->vldsz;
323 if (sz<0) sz = (int)strlen((const char*)src->buf);
324 if (sz==0) return 0;
325
326 if (dst->bufsz < sz) {
327 unsigned char* buf;
328 //dz = (int)((src->bufsz+1)*BUFFER_FACT);
329 dz = (int)((sz+1)*BUFFER_FACT);
330 buf = (unsigned char*)malloc(dz+1);
331 if (buf==NULL) return JBXL_MALLOC_ERROR;
332 memset(buf, 0, dz+1);
333
334 free(dst->buf);
335 dst->bufsz = dz;
336 dst->buf = buf;
337 }
338
339 //memset(dst->buf, 0, dst->bufsz+1); too late
340 memcpy(dst->buf, src->buf, sz);
341 dst->buf[sz] = '\0';
342 dst->vldsz = sz;
343
344 return sz;
345}
346
347
384int cat_Buffer(Buffer* src, Buffer* dst)
385{
386 int sz, dz;
387
388 if (src==NULL || dst==NULL) return JBXL_NULL_ERROR;
389 if (src->buf==NULL) return JBXL_NULL_ERROR;
390
391 sz = src->vldsz;
392 if (sz<0) sz = (int)strlen((const char*)src->buf);
393 if (sz==0) return 0;
394
395 dz = dst->vldsz;
396 if (dz<0) {
397 if (dst->buf!=NULL) dz = (int)strlen((const char*)dst->buf);
398 else dz = 0;
399 }
400
401 if (dst->bufsz < sz+dz) {
402 Buffer buf;
403 buf.state = dst->state;
404 buf.bufsz = (int)((dst->bufsz+sz+1)*BUFFER_FACT);
405 buf.vldsz = dz;
406 buf.buf = (unsigned char*)malloc(buf.bufsz+1);
407 if (buf.buf==NULL) return JBXL_MALLOC_ERROR;
408
409 memcpy(buf.buf, dst->buf, dz);
410 buf.buf[dz] = '\0';
411 free_Buffer(dst);
412 *dst = buf;
413/*
414 Buffer buf; // dstの待避
415 buf.bufsz = dst->bufsz;
416 buf.vldsz = dz;
417 buf.buf = (unsigned char*)malloc(buf.bufsz+1);
418 if (buf.buf==NULL) return JBXL_MALLOC_ERROR;
419
420 buf.buf[buf.bufsz] = '\0';
421 memcpy(buf.buf, dst->buf, buf.bufsz);
422 free_Buffer(dst);
423
424 //dst->bufsz = (int)((buf.bufsz+src->bufsz+1)*BUFFER_FACT);
425 dst->bufsz = (int)((buf.bufsz+sz+1)*BUFFER_FACT);
426 dst->buf = (unsigned char*)malloc(dst->bufsz+1);
427 if (dst->buf==NULL) {
428 *dst = buf;
429 return JBXL_MALLOC_ERROR;
430 }
431 memset(dst->buf, 0, dst->bufsz+1);
432 memcpy(dst->buf, buf.buf, buf.vldsz);
433 free_Buffer(&buf);
434*/
435 }
436
437 //memset(dst->buf+dz, 0, dst->bufsz-dz+1); too late
438 memcpy(dst->buf+dz, src->buf, sz);
439 dst->buf[dz+sz] = '\0';
440 dst->vldsz = sz + dz;
441
442 return dst->vldsz;
443}
444
445
458int ins_Buffer(Buffer* src, Buffer* dst)
459{
460 Buffer tmp = make_Buffer(src->bufsz + dst->bufsz);
461
462 int ret = copy_Buffer(src, &tmp);
463 if (ret<0) {
464 free_Buffer(&tmp);
465 return ret;
466 }
467
468 ret = cat_Buffer(dst, &tmp);
469 if (ret<0) {
470 free_Buffer(&tmp);
471 return ret;
472 }
473
474 free_Buffer(dst);
475 *dst = tmp;
476
477 return dst->vldsz;
478}
479
480
518int copy_b2Buffer(void* src, Buffer* dst, int len)
519{
520 int sz, dz;
521
522 if (src==NULL || dst==NULL) return JBXL_NULL_ERROR;
523 if (len<0) sz = (int)strlen((const char*)src);
524 else sz = len;
525 if (sz<=0) return 0;
526
527 if (dst->bufsz < sz) {
528 unsigned char* buf;
529 dz = (int)((sz+1)*BUFFER_FACT);
530 buf = (unsigned char*)malloc(dz+1);
531 if (buf==NULL) return JBXL_MALLOC_ERROR;
532 memset(buf, 0, dz+1);
533
534 if (dst->buf!=NULL) free(dst->buf);
535 dst->bufsz = dz;
536 dst->buf = buf;
537 }
538
539 memcpy(dst->buf, src, sz);
540 dst->buf[sz] = '\0';
541 dst->vldsz = sz;
542 dst->state = JBXL_NORMAL;
543
544 return sz;
545}
546
547
585int cat_b2Buffer(void* src, Buffer* dst, int len)
586{
587 int sz, dz;
588
589 if (src==NULL || dst==NULL) return JBXL_NULL_ERROR;
590 if (len<0) sz = (int)strlen((const char*)src);
591 else sz = len;
592 if (sz<=0) return 0;
593
594 dz = dst->vldsz;
595 if (dz<0) {
596 if (dst->buf!=NULL) dz = (int)strlen((const char*)dst->buf);
597 else dz = 0;
598 }
599
600 if (dst->bufsz < sz+dz) {
601 Buffer buf;
602 buf.state = dst->state;
603 buf.bufsz = (int)((dst->bufsz+sz+1)*BUFFER_FACT);
604 buf.vldsz = dz;
605 buf.buf = (unsigned char*)malloc(buf.bufsz+1);
606 if (buf.buf==NULL) return JBXL_MALLOC_ERROR;
607
608 memcpy(buf.buf, dst->buf, dz);
609 buf.buf[dz] = '\0';
610 free_Buffer(dst);
611 *dst = buf;
612 }
613
614 memcpy(dst->buf+dz, src, sz);
615 dst->buf[dz+sz] = '\0';
616 dst->vldsz = sz + dz;
617
618 return dst->vldsz;
619}
620
621
636int ins_b2Buffer(void* src, Buffer* dst, int len)
637{
638 Buffer tmp = make_Buffer(dst->bufsz+len);
639
640 int ret = copy_b2Buffer(src, &tmp, len);
641 if (ret<0) {
642 free_Buffer(&tmp);
643 return ret;
644 }
645
646 ret = cat_Buffer(dst, &tmp);
647 if (ret<0) {
648 free_Buffer(&tmp);
649 return ret;
650 }
651
652 free_Buffer(dst);
653 *dst = tmp;
654
655 return dst->vldsz;
656}
657
658
664int copy_i2Buffer(int src, Buffer* dst)
665{
666 char num[LEN_INT];
667
668 snprintf(num, LEN_INT-1, "%d", src);
669 return copy_b2Buffer((void*)num, dst, (int)strlen(num));
670}
671
672
678int cat_i2Buffer(int src, Buffer* dst)
679{
680 char num[LEN_INT];
681
682 snprintf(num, LEN_INT-1, "%d", src);
683 return cat_b2Buffer((void*)num, dst, (int)strlen(num));
684}
685
686
692int ins_i2Buffer(int src, Buffer* dst)
693{
694 char num[LEN_INT];
695
696 snprintf(num, LEN_INT-1, "%d", src);
697 return ins_b2Buffer((void*)num, dst, (int)strlen(num));
698}
699
700
706int copy_r2Buffer(float src, Buffer* dst)
707{
708 char num[LEN_REAL];
709
710 snprintf(num, LEN_REAL-1, "%f", src);
711 return copy_b2Buffer((void*)num, dst, (int)strlen(num));
712}
713
714
720int cat_r2Buffer(float src, Buffer* dst)
721{
722 char num[LEN_REAL];
723
724 snprintf(num, LEN_REAL-1, "%f", src);
725 return cat_b2Buffer((void*)num, dst, (int)strlen(num));
726}
727
728
734int ins_r2Buffer(float src, Buffer* dst)
735{
736 char num[LEN_REAL];
737
738 snprintf(num, LEN_REAL-1, "%f", src);
739 return ins_b2Buffer((void*)num, dst, (int)strlen(num));
740}
741
742
762int cmp_Buffer(Buffer src, Buffer dst, int n)
763{
764 int i;
765
766 if (src.buf==NULL || dst.buf==NULL) return JBXL_NULL_ERROR;
767 if (n>src.vldsz || n>dst.vldsz) return JBXL_BUFFER_CMP_ERROR;
768
769 if (n<=0) {
770 if (src.vldsz!=dst.vldsz) return 1;
771 else n = src.vldsz;
772 }
773
774 for (i=0; i<n; i++) {
775 if (src.buf[i]!=dst.buf[i]) return 1;
776 }
777 return 0;
778}
779
780
805{
806 unsigned char* bas;
807 int sz;
808 Buffer ecd = init_Buffer();
809
810 //
811 if (buf.buf==NULL) return ecd;
812 if (buf.vldsz<0) sz = (int)strlen((const char*)buf.buf);
813 else sz = buf.vldsz;
814 if (sz<=0) return ecd;
815
816 bas = encode_base64(buf.buf, sz);
817 if (bas==NULL) return ecd;
818
819 sz = ((sz + 2)/3)*4 + 1;
820 ecd = make_Buffer(sz);
821 if (ecd.buf==NULL) {
822 free(bas);
823 return ecd;
824 }
825
826 copy_s2Buffer((char*)bas, &ecd);
827
828 free(bas);
829 return ecd;
830}
831
832
851{
852 unsigned char* bas;
853 int sz;
854 Buffer dcd = init_Buffer();
855
856 if (str.buf==NULL) return dcd;
857
858 bas = decode_base64(str.buf, &sz);
859 if (bas==NULL) return dcd;
860
861 dcd = make_Buffer(sz);
862 if (dcd.buf==NULL) {
863 free(bas);
864 return dcd;
865 }
866
867 copy_b2Buffer(bas, &dcd, sz);
868 dcd.vldsz = sz;
869
870 free(bas);
871 return dcd;
872}
873
874
881Buffer encode_base64_Buffer_bin(unsigned char* bin, unsigned int sz, int nopad)
882{
883 unsigned char* str;
884 Buffer enc = init_Buffer();
885
886 if (bin==NULL) return enc;
887 if (sz<=0) sz = (unsigned)strlen((char*)bin);
888
889 str = encode_base64(bin, sz);
890 if (str==NULL) return enc;
891
892 if (nopad) {
893 unsigned int len = (unsigned int)strlen((char*)str);
894 if (str[len-2]=='=') str[len-2] = '\0';
895 else if (str[len-1]=='=') str[len-1] = '\0';
896 }
897 enc = make_Buffer_bystr(str);
898 free(str);
899
900 return enc;
901}
902
903
910{
911 if (cc == 0x00) cc = '-';
912
913 Buffer enc = encode_base64_Buffer(buf);
914
915 int i;
916 for (i=0; i<enc.vldsz; i++) {
917 if (enc.buf[i]=='/') enc.buf[i] = cc;
918 }
919
920 return enc;
921}
922
923
930{
931 if (cc == 0x00) cc = '-';
932
933 int i;
934 for (i=0; i<str.vldsz; i++) {
935 if (str.buf[i]==cc) str.buf[i] = '/';
936 }
937
938 Buffer dec = decode_base64_Buffer(str);
939
940 return dec;
941}
942
943
957{
958 int i, j, pos, cnt;
959 unsigned char* buf;
960
961 Buffer ret = init_Buffer();
962 if (str.buf==NULL) return ret;
963
964 buf = str.buf;
965 for(i=0,j=0; j<n-1; j++) {
966 while (buf[i]!=CHAR_LF && buf[i]!=CHAR_CR && buf[i]!='\0') i++;
967 while (buf[i]==CHAR_LF || buf[i]==CHAR_CR) i++;
968 }
969 if (buf[i]=='\0') return ret;
970
971 pos = i;
972 while (buf[i]!=CHAR_LF && buf[i]!=CHAR_CR && buf[i]!='\0') i++;
973 cnt = i - pos;
974
975 if (cnt>=0) {
976 ret = make_Buffer(cnt+1);
977 if (ret.buf==NULL) return str;
978
979 for(i=0; i<cnt; i++) ret.buf[i] = buf[pos+i];
980 ret.buf[cnt] = '\0';
981 }
982
983 ret.vldsz = (int)strlen((const char*)ret.buf);
984
985 return ret;
986}
987
988
1004{
1005 int i, pos, cnt;
1006 unsigned char* buf;
1007
1008 Buffer ret = init_Buffer();
1009
1010 if (str.buf==NULL) return ret;
1011 if (str.vldsz<=0) str.vldsz = (int)strlen((const char*)str.buf);
1012 if (*ptr >= str.vldsz) return ret;
1013
1014 buf = str.buf;
1015 pos = *ptr;
1016 while (buf[*ptr]!=CHAR_LF && buf[*ptr]!=CHAR_CR && buf[*ptr]!='\0') (*ptr)++;
1017 cnt = *ptr - pos;
1018
1019 if (cnt>=0) {
1020 ret = make_Buffer(cnt+1);
1021 if (ret.buf==NULL) return ret;
1022
1023 for(i=0; i<cnt; i++) ret.buf[i] = buf[pos+i];
1024 ret.buf[cnt] = '\0';
1025 }
1026
1027 ret.vldsz = (int)strlen((const char*)ret.buf);
1028
1029 // 空行
1030 while (buf[*ptr]==CHAR_LF || buf[*ptr]==CHAR_CR) (*ptr)++;
1031
1032 return ret;
1033}
1034
1035
1050Buffer awk_Buffer(Buffer str, char cc, int n)
1051{
1052 int i, j, pos, cnt;
1053 unsigned char* buf = str.buf;
1054 Buffer item = init_Buffer();
1055
1056 if (buf==NULL) return item;
1057 if (n<=0) n = 1;
1058
1059 for(i=0,j=0; j<n-1; j++) {
1060 while (buf[i]!='\0' && buf[i]!=cc) i++;
1061 if (buf[i]==cc) i++;
1062 }
1063 if (buf[i]=='\0') return item;
1064
1065 pos = i;
1066 while (buf[i]!='\0' && buf[i]!=cc) i++;
1067 cnt = i - pos;
1068
1069 item = make_Buffer(cnt);
1070 if (item.buf==NULL) return item;
1071
1072 for (i=0; i<cnt; i++) item.buf[i] = buf[pos+i];
1073 item.vldsz = (int)strlen((const char*)item.buf);
1074
1075 return item;
1076}
1077
1078
1094Buffer cawk_Buffer(Buffer str, char cc, int n)
1095{
1096 int i, j, pos, cnt;
1097 unsigned char* buf = str.buf;
1098 Buffer item = init_Buffer();
1099
1100 if (buf==NULL) return item;
1101 if (n<=0) n = 1;
1102
1103 i = 0;
1104 for(j=0; j<n-1; j++) {
1105 while (buf[i]!='\0' && buf[i]!=cc) i++;
1106 while (buf[i]!='\0' && buf[i]==cc) i++;
1107 }
1108 if (buf[i]=='\0') return item;
1109
1110 pos = i;
1111 while (buf[i]!='\0' && buf[i]!=cc) i++;
1112 cnt = i - pos;
1113
1114 item = make_Buffer(cnt);
1115 if (item.buf==NULL) return item;
1116
1117 for (i=0; i<cnt; i++) item.buf[i] = buf[pos+i];
1118 item.vldsz = (int)strlen((const char*)item.buf);
1119
1120 return item;
1121}
1122
1123
1135{
1136 Buffer res = init_Buffer();
1137
1138 if (buf.buf==NULL) return res;
1139
1140 res.buf = (unsigned char*)pack_char((char*)buf.buf, cc);
1141 res.vldsz = (int)strlen((char*)res.buf);
1142 res.bufsz = res.vldsz;
1143
1144 return res;
1145}
1146
1147
1158{
1159 int i;
1160 unsigned char *c1, *c2;
1161
1162 if (KanjiCode==CODE_SJIS) {
1163 i = 0;
1164 while(str->buf[i]!='\0'){
1165 c1 = &(str->buf[i]);
1166 c2 = c1 + 1;
1167 if(*c1>=0xa1 && *c1<=0xfe){
1168 euc2sjis(c1, c2);
1169 i = i + 2;
1170 }
1171 else i++;
1172 }
1173 }
1174 /*
1175 else if (KanjiCode==CODE_EUC) {
1176 i = 0;
1177 while(str->buf[i]!='\0'){
1178 c1 = (unsigned char) str->buf[i];
1179 c2 = (unsigned char) str->buf[i+1];
1180 if((c1>=0x81 && c1<=0x9f) || (c1>=0xe0 && c1<=0xff)) {
1181 sjis2euc(c1, c2);
1182 i = i + 2;
1183 }
1184 else i++;
1185 }
1186 }
1187 */
1188}
1189
1190
1200{
1201 char base[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1202 Buffer pass;
1203 int i, sz;
1204
1205 pass = make_Buffer(n);
1206 if (pass.buf==NULL) return pass;
1207
1208 sz = (int)strlen(base);
1209 for (i=0; i<n; i++) pass.buf[i] = base[rand()%sz];
1210 pass.vldsz = n;
1211
1212 return pass;
1213}
1214
1215
1225{
1226 int i, sz;
1227 Buffer str;
1228
1229 sz = (n+7)/8;
1230 str = make_Buffer(sz);
1231 if (str.buf==NULL) return str;
1232 str.vldsz = sz;
1233
1234 for (i=0; i<n; i++) setBit(str.buf, i, rand()%2);
1235 return str;
1236}
1237
1238
1239#define DUMP_BUFFER_LINE_SIZE 85 // 16*5("%02x "+"%c ") + 5(" "+"\r\n");
1240#define DUMP_BUFFER_DELI_START 48 // 16*3("%02x ")
1241#define DUMP_BUFFER_CHAR_START 51 // 16*3("%02x ") + 3(" ")
1242
1255{
1256 int sz;
1257 char wrkbuf[10];
1258 Buffer str = init_Buffer();
1259
1260 if (buf.buf==NULL) return str;
1261 if (buf.vldsz<0) sz = (int)strlen((const char*)buf.buf) + 1;
1262 else sz = buf.vldsz;
1263
1264 int lineno = (sz + 15)/16;
1265 str = make_Buffer(lineno*DUMP_BUFFER_LINE_SIZE+1);
1266
1267 int l;
1268 for (l=0; l<lineno; l++) {
1269 int sp = l*DUMP_BUFFER_LINE_SIZE;
1270 int i = 0;
1271 while (i<16) {
1272 sprintf(wrkbuf, "%02x ", buf.buf[l*16+i]);
1273 memcpy(str.buf+sp+i*3, wrkbuf, 3);
1274
1275 if (buf.buf[l*16+i]>=0x20 && buf.buf[l*16+i]<=0x7e) {
1276 sprintf(wrkbuf, "%c ", buf.buf[l*16+i]);
1277 memcpy(str.buf+sp+DUMP_BUFFER_CHAR_START+i*2, wrkbuf, 2);
1278 }
1279 else {
1280 memcpy(str.buf+sp+DUMP_BUFFER_CHAR_START+i*2, ". ", 2);
1281 }
1282
1283 if (l*16+i>=sz) {
1284 memcpy(str.buf+sp+i*3, " ", 3);
1285 memcpy(str.buf+sp+DUMP_BUFFER_CHAR_START+i*2, " ", 2);
1286 }
1287
1288 i++;
1289 }
1290 memcpy(str.buf+sp+DUMP_BUFFER_DELI_START, " ", 3);
1291 str.buf[sp+DUMP_BUFFER_LINE_SIZE-2] = '\r';
1292 str.buf[sp+DUMP_BUFFER_LINE_SIZE-1] = '\n';
1293 }
1294
1295 str.vldsz = lineno*DUMP_BUFFER_LINE_SIZE;
1296 return str;
1297}
1298
1299
1311{
1312 int len = (int)strlen((char*)buf->buf);
1313 buf->vldsz = len;
1314 return len;
1315}
1316
1317
1327void add_indent_Buffer(Buffer* buf, char cc, int len)
1328{
1329 char* indent = (char*)malloc(len + 1);
1330 if (indent!=NULL) {
1331 memset(indent, cc, len);
1332 indent[len] = '\0';
1333 cat_s2Buffer(indent, buf);
1334 free(indent);
1335 }
1336}
1337
1338
1348{
1349 int i, len;
1350
1351 if (str->buf==NULL) return;
1352
1353 len = (int)strlen((const char*)str->buf);
1354 for (i=0; i<len; i++) {
1355 if (str->buf[i]==CHAR_LF || str->buf[i]==CHAR_CR) {
1356 str->buf[i] = '\0';
1357 str->vldsz = (int)strlen((const char*)str->buf);
1358 return;
1359 }
1360 }
1361}
1362
1363
1374{
1375 int i;
1376 for (i=0; i<buf.vldsz; i++) {
1377 if (buf.buf[i]<0x20 && buf.buf[i]!=0x0a && buf.buf[i]!=0x0d
1378 && buf.buf[i]!=0x00 && buf.buf[i]!=0x09) return FALSE;
1379 if (buf.buf[i]==0x7f) return FALSE; // DEL
1380 }
1381
1382 //if (buf.vldsz==(int)strlen((const char*)buf.buf)) return TRUE;
1383 return FALSE;
1384}
1385
1386
1387
1389// File I/O
1390//
1391
1402int fgets_Buffer(Buffer* str, FILE* fp)
1403{
1404 char* p;
1405 int n, m;
1406 UNUSED(p);
1407
1408 memset(str->buf, 0, str->bufsz);
1409 p = fgets((char*)str->buf, str->bufsz, fp);
1410 m = (int)strlen((const char*)str->buf);
1411
1412 n = 0;
1413 while(str->buf[n]!=CHAR_LF && str->buf[n]!=CHAR_CR && str->buf[n]!='\0') n++;
1414 str->buf[n] = '\0';
1415 str->vldsz = n;
1416
1417// if (KanjiCode!=US) kanji_convert_Buffer(str);
1418
1419 return m;
1420}
1421
1422
1433int read_lines_Buffer(Buffer* str, FILE* fp)
1434{
1435 Buffer buf;
1436 char* pp;
1437 UNUSED(pp);
1438
1439 buf = make_Buffer(str->bufsz);
1440 if (buf.buf==NULL) return JBXL_BUFFER_MAKE_ERROR;
1441
1442 pp = fgets((char*)buf.buf, buf.bufsz, fp);
1443 buf.vldsz = (int)strlen((const char*)buf.buf);
1444 copy_Buffer(&buf, str);
1445 clear_Buffer(&buf);
1446
1447 while (!feof(fp)) {
1448 pp = fgets((char*)buf.buf, buf.bufsz, fp);
1449 buf.vldsz = (int)strlen((const char*)buf.buf);
1450 cat_Buffer(&buf, str);
1451 clear_Buffer(&buf);
1452 }
1453
1454 free_Buffer(&buf);
1455 return str->vldsz;
1456}
1457
1458
1468{
1469 int sz;
1470 Buffer buf;
1471 FILE* fp;
1472
1473 buf = init_Buffer();
1474
1475 sz = file_size(fn);
1476 if (sz<=0) return buf;
1477
1478 fp = fopen(fn, "rb");
1479 if (fp==NULL) return buf;
1480
1481 buf = read_Buffer_data(fp, sz);
1482 fclose(fp);
1483
1484 return buf;
1485}
1486
1487
1497Buffer read_Buffer_data(FILE* fp, int sz)
1498{
1499 int cc;
1500 Buffer buf;
1501
1502 buf = init_Buffer();
1503 if (sz<0) return buf;
1504
1505 buf = make_Buffer(sz);
1506 if (buf.buf==NULL) return buf;
1507 buf.vldsz = sz;
1508
1509 cc = (int)fread(buf.buf, buf.vldsz, 1, fp);
1510 if (cc!=1) {
1511 free_Buffer(&buf);
1512 return buf;
1513 }
1514
1515 return buf;
1516}
1517
1518
1530int save_Buffer_file(Buffer buf, char* fn)
1531{
1532 int cc;
1533 FILE* fp;
1534
1535 fp = fopen(fn, "wb");
1536 if (fp==NULL) return FALSE;
1537
1538 cc = (int)fwrite(buf.buf, buf.vldsz, 1, fp);
1539 if (cc!=1) return FALSE;
1540 fclose(fp);
1541
1542 return TRUE;
1543}
1544
1545
1546
1548// 文字列操作
1549//
1550
1562Buffer erase_bBuffer(Buffer buf, char* bin, int len)
1563{
1564 int i, j, n;
1565 char cc;
1566
1567 Buffer res = dup_Buffer(buf);
1568 if (bin==NULL) return res;
1569 Buffer wrk = make_Buffer(buf.vldsz + 1);
1570
1571 if (len<0) len = (int)strlen(bin);
1572 for (i=0; i<len; i++) {
1573 cc = bin[i];
1574 n = 0;
1575 for (j=0; j<res.vldsz; j++) {
1576 if (cc!=res.buf[j]) wrk.buf[n++] = res.buf[j];
1577 }
1578 wrk.vldsz = n;
1579
1580 copy_Buffer(&wrk, &res);
1581 clear_Buffer(&wrk);
1582 }
1583
1584 free_Buffer(&wrk);
1585
1586 return res;
1587}
1588
1589
1603Buffer replace_sBuffer_bystr(Buffer buf, const char* frm, const char* tos)
1604{
1605 int i, j, flen, tlen;
1606 Buffer res;
1607
1608 res = init_Buffer();
1609 if (buf.buf==NULL || frm==NULL || tos==NULL) return res;
1610
1611 flen = (int)strlen(frm);
1612 tlen = (int)strlen(tos);
1613 if (buf.vldsz<=0) buf.vldsz = (int)strlen((const char*)buf.buf);
1614
1615 int difsz = tlen - flen;
1616 if (difsz<0) difsz = 0;
1617 else difsz = difsz*100; // 100個まで
1618 res = make_Buffer((int)((buf.vldsz+difsz+1)*BUFFER_FACT));
1619
1620 i = j = 0;
1621 while (i<buf.vldsz && j<res.bufsz) {
1622 if (!strncmp((const char*)(buf.buf+i), frm, flen)){
1623 res.vldsz = (int)strlen((const char*)res.buf);
1624 cat_s2Buffer(tos, &res);
1625 i += flen;
1626 j += tlen;
1627 }
1628 else {
1629 res.buf[j++] = buf.buf[i++];
1630 }
1631 }
1632
1633 if (j==res.bufsz) res.state = JBXL_ERROR;
1634 res.vldsz = (int)strlen((const char*)res.buf);
1635
1636 return res;
1637}
1638
1639
1652int replace_sBuffer_file(char* fn, Buffer frm, Buffer tos)
1653{
1654 int ret;
1655 Buffer buf, res;
1656
1657 buf = read_Buffer_file(fn);
1658 if (buf.buf==NULL) return FALSE;
1659
1660 res = replace_sBuffer(buf, (char*)frm.buf, (char*)tos.buf);
1661 free_Buffer(&buf);
1662 if (res.buf==NULL) return FALSE;
1663
1664 ret = save_Buffer_file(res, fn);
1665 free_Buffer(&buf);
1666
1667 return ret;
1668}
1669
1670
1681void rewrite_sBuffer_bystr(Buffer* buf, const char* frm, const char* tos)
1682{
1683 if (buf==NULL || frm==NULL || tos==NULL) return;
1684
1685 Buffer tmp = replace_sBuffer_bystr(*buf, frm, tos);
1686 if (tmp.state==JBXL_NORMAL) {
1687 free_Buffer(buf);
1688 *buf = tmp;
1689 }
1690 return;
1691}
1692
1693
1703void rewrite_Buffer_bychar(Buffer* buf, const char frm, const char toc)
1704{
1705 if (buf==NULL) return;
1706
1707 int i;
1708 for (i=0; i<buf->vldsz; i++) {
1709 if (buf->buf[i]==frm) buf->buf[i] = toc;
1710 }
1711 return;
1712}
1713
1714
1715
1717// ヘッダ(書式)付特殊ファイルI/O
1718//
1719
1733int save_Buffer2_format_fp(Buffer key, Buffer buf, FILE* fp)
1734{
1735 int cc;
1736
1737 cc = save_Buffer_format_fp(key, fp);
1738 if (!cc) return FALSE;
1739
1740 cc = save_Buffer_format_fp(buf, fp);
1741 if (!cc) return FALSE;
1742
1743 return TRUE;
1744}
1745
1746
1760{
1761 int cc, sz;
1762
1763 sz = htonl(buf.vldsz);
1764 cc = (int)fwrite(&sz, sizeof(sz), 1, fp);
1765 if (cc!=1) return FALSE;
1766
1767 cc = (int)fwrite(buf.buf, buf.vldsz, 1, fp);
1768 if (cc!=1) return FALSE;
1769
1770 return TRUE;
1771}
1772
1773
1784{
1785 int cc, sz;
1786 Buffer buf;
1787
1788 buf = init_Buffer();
1789 cc = (int)fread(&sz, sizeof(sz), 1, fp);
1790 if (cc!=1) return buf;
1791 sz = ntohl(sz);
1792
1793 buf = make_Buffer(sz);
1794 if (buf.buf==NULL) return buf;
1795 buf.vldsz = sz;
1796 cc = (int)fread(buf.buf, buf.vldsz, 1, fp);
1797 if (cc!=1) {
1798 free_Buffer(&buf);
1799 return buf;
1800 }
1801
1802 return buf;
1803}
1804
1805
1819int read_Buffer2_format_fp(Buffer* key, Buffer* buf, FILE* fp)
1820{
1821 if (key==NULL || buf==NULL) return FALSE;
1822
1823 *key = init_Buffer();
1824 *buf = init_Buffer();
1825
1826 *key = read_Buffer_format_fp(fp);
1827 if (key->buf==NULL) return FALSE;
1828
1829 *buf = read_Buffer_format_fp(fp);
1830 if (buf->buf==NULL) {
1831 free_Buffer(key);
1832 return FALSE;
1833 }
1834
1835 return TRUE;
1836}
1837
1838
1839
1841// etc. etc.
1842//
1843
1844//
1845// get_char_ringbuffer() -> pop_char_ringStack()
1846// put_char_ringbuffer() -> push_char_ringStack()
1847//
1848
1849
1856{
1857 buf->vldsz--;
1858 if (buf->vldsz<0) buf->vldsz = buf->bufsz - 1;
1859
1860 return (char)buf->buf[buf->vldsz];
1861}
1862
1863
1869void push_char_ringStack(Buffer* buf, char cc)
1870{
1871 buf->buf[buf->vldsz] = (unsigned char)cc;
1872 buf->vldsz++;
1873 if (buf->vldsz==buf->bufsz) buf->vldsz = 0;
1874
1875 return;
1876}
1877
Buffer encode_base64_Buffer(Buffer buf)
バイナリデータ buf.bufの buf.vldszバイトを Base64にエンコード する
Definition buffer.cpp:804
Buffer get_line_Buffer(Buffer str, int n)
複数行の文字列バッファから任意の行を取り出す.
Definition buffer.cpp:956
int ins_Buffer(Buffer *src, Buffer *dst)
Buffer変数 dst の前に srcを挿入する.
Definition buffer.cpp:458
int ins_i2Buffer(int src, Buffer *dst)
整数 srcを文字列に変換して,dstの前に 挿入する.
Definition buffer.cpp:692
Buffer read_Buffer_format_fp(FILE *fp)
ファイル fp から Buffer型変数の buf部を読み込む
Definition buffer.cpp:1783
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
Definition buffer.cpp:71
Buffer replace_sBuffer_bystr(Buffer buf, const char *frm, const char *tos)
buf.buf中の文字列 frmを tosで置き換えた 新しい Bufferを返す
Definition buffer.cpp:1603
int fgets_Buffer(Buffer *str, FILE *fp)
拡張fgets.文字列の読み込みに使用する.改行コードは削除する
Definition buffer.cpp:1402
void chomp_Buffer(Buffer *str)
最初の改行コード以降を無視する.
Definition buffer.cpp:1347
Buffer set_Buffer(void *dat, int len)
Buffer型変数のバッファ部を新しく作り, そのバッファに bufをコピーする.
Definition buffer.cpp:170
int read_Buffer2_format_fp(Buffer *key, Buffer *buf, FILE *fp)
ファイル fp から 2つのBuffer型変数の keyと bufを読み込む.
Definition buffer.cpp:1819
Buffer encode_base64_filename_Buffer(Buffer buf, unsigned char cc)
バイナリデータ bufを Base64で encodeしてファイル名を作る.ただし '/' は cc として扱う.
Definition buffer.cpp:909
Buffer randstr_Buffer(int n)
英数字を n個生成
Definition buffer.cpp:1199
Buffer decode_base64_filename_Buffer(Buffer str, unsigned char cc)
bufを Base64で decodeしてバイナリデータを取り出す.ただし cc は '/' として扱う.
Definition buffer.cpp:929
int cat_b2Buffer(void *src, Buffer *dst, int len)
任意のバイナリデータsrcを Buffer型変数dstへ lenバイト catする.
Definition buffer.cpp:585
void kanji_convert_Buffer(Buffer *str)
大域変数 KanjiCode (tools.h) に従って漢字コードを変換する.
Definition buffer.cpp:1157
void clear_Buffer(Buffer *str)
Buffer型変数 のバッファ部を 0クリアする.
Definition buffer.cpp:272
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition buffer.cpp:128
Buffer randbit_Buffer(int n)
ビットを n個生成
Definition buffer.cpp:1224
char pop_char_ringStack(Buffer *buf)
簡易 char型 ringStack POP
Definition buffer.cpp:1855
void push_char_ringStack(Buffer *buf, char cc)
簡易 char型 ringStack PUSH
Definition buffer.cpp:1869
Buffer read_Buffer_file(const char *fn)
ファイル fn の内容を Buffer型変数に読み込む.
Definition buffer.cpp:1467
Buffer read_Buffer_data(FILE *fp, int sz)
ファイルポインタ fp から szバイトをBuffer型変数に読み込む.
Definition buffer.cpp:1497
Buffer init_Buffer()
初期化したBuffer型変数を返す.
Definition buffer.cpp:47
void rewrite_Buffer_bychar(Buffer *buf, const char frm, const char toc)
buf.buf中の文字 frmを tocで置き換える
Definition buffer.cpp:1703
int save_Buffer_file(Buffer buf, char *fn)
ファイル fn へ Buffer型変数の buf部を書き込む
Definition buffer.cpp:1530
Buffer dup_Buffer(Buffer buf)
Buffer型変数のコピーをつくる.
Definition buffer.cpp:211
int copy_r2Buffer(float src, Buffer *dst)
実数 srcを文字列に変換して,dstへ copyする.
Definition buffer.cpp:706
int ins_r2Buffer(float src, Buffer *dst)
実数 srcを文字列に変換して,dstの前に 挿入する.
Definition buffer.cpp:734
int read_lines_Buffer(Buffer *str, FILE *fp)
複数文字列行の読み込み.文字列の読み込みに使用する
Definition buffer.cpp:1433
void expand_Buffer(Buffer *buf, int len)
buf のデータ部を lenに拡張した新しい Buffer を返す.
Definition buffer.cpp:253
int save_Buffer_format_fp(Buffer buf, FILE *fp)
ファイル fp へ Buffer型変数の buf部を書き込む
Definition buffer.cpp:1759
Buffer rept_Buffer(unsigned char cc, int n)
文字 ccを n回繰り返したBuffer型データを返す.
Definition buffer.cpp:234
#define DUMP_BUFFER_LINE_SIZE
Definition buffer.cpp:1239
Buffer pack_Buffer(Buffer buf, char cc)
文字列の先頭のcc(複数),終わりのcc(複数),TAB, CR, LF を削除
Definition buffer.cpp:1134
void rewrite_sBuffer_bystr(Buffer *buf, const char *frm, const char *tos)
buf.buf自体を書き換えること以外は replace_sBuffer_bystr() と同じ
Definition buffer.cpp:1681
int isText_Buffer(Buffer buf)
Buffer型変数 bufのバッファ部がテキストかどうか検査する.
Definition buffer.cpp:1373
int copy_i2Buffer(int src, Buffer *dst)
整数 srcを文字列に変換して,dstへ copyする.
Definition buffer.cpp:664
Buffer decode_base64_Buffer(Buffer str)
strのバッファを Base64からデコードする
Definition buffer.cpp:850
int replace_sBuffer_file(char *fn, Buffer frm, Buffer tos)
テキストファイル中の frm の文字列を tos に書き換える
Definition buffer.cpp:1652
int cat_r2Buffer(float src, Buffer *dst)
実数 srcを文字列に変換して,dstへ catする.
Definition buffer.cpp:720
int recalc_strlen_Buffer(Buffer *buf)
Buffer型変数のデータ部を文字列と見なして,その長さを返す.
Definition buffer.cpp:1310
Buffer make_Buffer_bychar(unsigned char cc)
文字から,Buffer型変数のバッファ部をつくり出す.
Definition buffer.cpp:108
int cmp_Buffer(Buffer src, Buffer dst, int n)
バッファ部の比較.
Definition buffer.cpp:762
int cat_Buffer(Buffer *src, Buffer *dst)
Buffer変数 srcから dstへバッファを catする.
Definition buffer.cpp:384
Buffer encode_base64_Buffer_bin(unsigned char *bin, unsigned int sz, int nopad)
sz バイトの バイナリデータ binを Base64にエンコード する.
Definition buffer.cpp:881
int cat_i2Buffer(int src, Buffer *dst)
整数 srcを文字列に変換して,dstへ catする.
Definition buffer.cpp:678
void del_Buffer(Buffer **buf)
new_Buffer()で作り出した Buffer型変数を削除する.
Definition buffer.cpp:148
#define DUMP_BUFFER_DELI_START
Definition buffer.cpp:1240
Buffer * new_Buffer(int sz)
空のBuffer型変数を生成する.
Definition buffer.cpp:23
int copy_Buffer(Buffer *src, Buffer *dst)
Buffer型変数 srcから dstへバッファをコピーする.
Definition buffer.cpp:315
Buffer cawk_Buffer(Buffer str, char cc, int n)
Buffer文字列に対する(変形の)awk.
Definition buffer.cpp:1094
int ins_b2Buffer(void *src, Buffer *dst, int len)
任意のバイナリデータsrcを Buffer型変数dstの前に lenバイト挿入する
Definition buffer.cpp:636
#define DUMP_BUFFER_CHAR_START
Definition buffer.cpp:1241
Buffer get_seq_data_Buffer(Buffer str, int *ptr)
複数行の文字列バッファ内の行データをシーケンシャルに取り出す.
Definition buffer.cpp:1003
Buffer erase_bBuffer(Buffer buf, char *bin, int len)
buf.buf 中にある bin のデータ(各1Byte,順不同)を削除する.
Definition buffer.cpp:1562
int save_Buffer2_format_fp(Buffer key, Buffer buf, FILE *fp)
ファイル fp へ 2つのBuffer型変数 keyと bufを書き込む
Definition buffer.cpp:1733
int copy_b2Buffer(void *src, Buffer *dst, int len)
任意のバイナリデータsrcを Buffer型変数dstへ lenバイト copyする
Definition buffer.cpp:518
Buffer dump_Buffer(Buffer buf)
Buffer型変数のデバッグ用 16進を Buffer型変数に出力する.
Definition buffer.cpp:1254
Buffer awk_Buffer(Buffer str, char cc, int n)
Buffer文字列に対する awk.
Definition buffer.cpp:1050
void add_indent_Buffer(Buffer *buf, char cc, int len)
インデント(cc x num) を付加する.
Definition buffer.cpp:1327
Buffer型サポートヘッダ
#define copy_s2Buffer(src, dst)
copy_b2Buffer()
Definition buffer.h:108
#define make_Buffer_randomstr(sz)
make_Buffer_randomstr()
Definition buffer.h:63
#define replace_sBuffer(buf, f, t)
replace_sBuffer()
Definition buffer.h:175
#define BUFFER_FACT
Buffer 変数の領域確保のための領域確保倍率
Definition buffer.h:43
#define cat_s2Buffer(src, dst)
cat_b2Buffer()
Definition buffer.h:122
#define make_Buffer_bystr(str)
set_Buffer()
Definition buffer.h:57
#define setBit(x, n, i)
Definition common.h:259
#define UNUSED(x)
Definition common.h:264
#define snprintf
Definition common.h:56
#define TRUE
Definition common.h:226
#define FALSE
Definition common.h:223
#define LEN_INT
log 2^64 + '\0' + 1(予備)
Definition common.h:171
#define LEN_REAL
15*2 + '\0' + 1(予備)
Definition common.h:170
#define LADDR
Definition common.h:162
JunkBox_Lib 状態ヘッダ
#define JBXL_ERROR
エラー
Definition jbxl_state.h:34
#define JBXL_BUFFER_MAKE_ERROR
Buffer の作成に失敗
Definition jbxl_state.h:60
#define JBXL_NORMAL
正常
Definition jbxl_state.h:32
#define JBXL_NULL_ERROR
参照先のアドレスが NULL
Definition jbxl_state.h:36
#define JBXL_MALLOC_ERROR
メモリ確保エラー
Definition jbxl_state.h:41
#define JBXL_BUFFER_CMP_ERROR
比較文字数が vldsz より大きい
Definition jbxl_state.h:61
int bufsz
確保してあるバッファの大きさ - 1.
Definition buffer.h:36
int vldsz
データの長さ.バイナリデータの場合も使用可能.文字列の場合は 0x00 を含まない.
Definition buffer.h:37
int state
変数の状態を表す.正常は JBXL_NORMAL
Definition buffer.h:38
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.
Definition buffer.h:39
unsigned long int file_size(const char *fn)
ファイルの大きさを返す.
Definition tools.cpp:2309
int KanjiCode
システムの漢字コード
Definition tools.cpp:20
void euc2sjis(unsigned char *c1, unsigned char *c2)
EUC -> SJIS.
Definition tools.cpp:2687
unsigned char * encode_base64(unsigned char *buf, int sz)
バイナリデータ bufを base64にエンコードする.要 free()
Definition tools.cpp:2848
unsigned char * decode_base64(unsigned char *buf, int *sz)
bufを base64からデコードする.要 free()
Definition tools.cpp:2786
#define random_str(n)
ランダムな文字列を生成.要 free(). see temp_filename()
Definition tools.h:351
#define CHAR_CR
改行
Definition tools.h:78
#define CHAR_LF
ラインフィード
Definition tools.h:79
#define CODE_SJIS
Definition tools.h:85
#define pack_char(s, c)
pack_char_len()
Definition tools.h:236