⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mal_io.mx

📁 一个内存数据库的源代码这是服务器端还有客户端
💻 MX
📖 第 1 页 / 共 2 页
字号:
@-Formatted print statements are to be broken down into its constituents.This avoids repeated type checking in most cases.@= escaped	switch(*c){	case 'n': stream_printf(fd,"\n");break;	case 't': stream_printf(fd,"\t");break;	case 'r': stream_printf(fd,"\r");break;	case '\\': stream_printf(fd,"\\");break;	default : stream_printf(fd,"\\");stream_printf(fd,"%c",*c);	}@cstrIOprint_str(int *ret, str *s){	stream *fd = GDKout;	str c;	(void) ret;	if (strNil(*s))		stream_printf(fd, "nil");	else {		for (c = *s; *c; c++) {			if (*c == '\\') {				c++;				@:escaped@			} else				stream_printf(fd, "%c", *c);		}	}	return MAL_SUCCEED;}intformatMarker(str format){	int i = 0;	str t = format;	while ((t = strchr(t, '%')) != NULL) {		t++;		i++;	}	return i;}@-Printing format values is limited to a single element each.This to simplify error handling@= formatConversion	for(c= format;*c; c++){		if( *c=='\\'){			c++;			@:escaped@		} else 		if( *c== '%'){			char newformat[1024];			int i=0;			if(*(c+1)=='%'){ 				stream_printf(fd,"%c",*c);				c++; continue;			}			while(*c != '@1' && *c && i<1024)				 newformat[i++]= *c++;			newformat[i++]='@1';			newformat[i]=0;			if(*c!= '@1') 			throw(MAL, "io.print", "Format limitation");			else  stream_printf(fd,newformat,@2);		} else stream_printf(fd,"%c",*c);	}@-@= printFormattedio_export str IOprint_formatted_@1(int *ret, ptr fmt, @1 *s);str IOprint_formatted_@1(int *ret, ptr fmt, @1 *s){	stream *fd = MCgetClient()->fdout;	char *format;	str c;	(void) ret;	/* TODO: check format properly !!! including sizes */	format= *(str *)fmt;	if( format== NULL || format== str_nil) 		throw(MAL, "io.print", "Format missing");	if( formatMarker(format)!=1) 		throw(MAL, "io.print", "Too many %%");	if( *s == @1_nil) 		return IOprint_formatted_nil(fd,* (str*) fmt, '@2');	@:formatConversion(@2,*s)@	return MAL_SUCCEED;}@c@:printFormatted(chr,c)@@:printFormatted(sht,d)@@:printFormatted(int,d)@@:printFormatted(flt,f)@@:printFormatted(dbl,f)@@-String nil testing is slightly different.@cstrIOprint_formatted_nil(stream *fd, str format, char last){	str c;	for (c = format; *c; c++) {		if (*c == '\\') {			c++;			@:escaped@		} else if (*c == '%') {			char newformat[1024];			int i = 0;			if (*(c + 1) == '%') {				stream_printf(fd, "%c", *c);				c++;				continue;			}			while (!isalpha((int) *c) && *c && *c != last && i < 1024)				newformat[i++] = *c++;			newformat[i++] = 's';			newformat[i] = 0;			stream_printf(fd, newformat, "nil");		} else			stream_printf(fd, "%c", *c);	}	return MAL_SUCCEED;}strIOprint_formatted_lng(int *ret, str *format, lng *val){	str c;	stream *fd= MCgetClient()->fdout;	(void) ret;	for (c = *format; *c; c++) {		if (*c == '\\') {			c++;			@:escaped@		} else if (*c == '%') {			char newformat[1024];			int i = 0;			if (*(c + 1) == '%') {				stream_printf(fd, "%c", *c);				c++;				continue;			}			while (!isalpha((int) *c) && *c && i < 1024)			if( *c =='d' ) {				break;			} else			if( *c=='l' && c[1]=='d') {				c++;				break;			} else				newformat[i++] = *c++;			if( *val == lng_nil){				newformat[i++] = 's';				newformat[i] = 0;				stream_printf(fd, newformat, "nil");			} else {				char *f= LLFMT;				strcpy(newformat+i,f+1);				stream_printf(fd, newformat, *val);			}		} else			stream_printf(fd, "%c", *c);	}	return MAL_SUCCEED;}strIOprint_formatted_str(int *ret, ptr fmt, str *s){	stream *fd = MCgetClient()->fdout;	char *format;	str c;	(void) ret;	/* TODO: check format properly !!! including sizes */	format = *(str *) fmt;	if (format == NULL)		throw(MAL, "io.print", "Format missing");	if (formatMarker(format) != 1)		throw(MAL, "io.print", "Too many %%");	if (strNil(*s))		return IOprint_formatted_nil(fd, *(str *) fmt, 's');	@:formatConversion(s,*s)@	return MAL_SUCCEED;}strIOprint_oid(int *ret, ptr fmt, oid *s){	stream *fd = MCgetClient()->fdout;	char *format;	str msg, c;	int len = 50;	format = *(str *) fmt;	if (format == NULL)		throw(MAL, "io.print", "Format missing");	if (formatMarker(format) != 1)		throw(MAL, "io.print", "Too many %%");	if (*s == oid_nil)		msg = GDKstrdup("nil");	else {		msg = (str) GDKmalloc(len);		OIDtoStr(&msg, &len, s);	}	@:formatConversion(s,msg)@	GDKfree(msg);	if (*ret == EOF)		throw(MAL, "io.print", "End-of-file encountered");	return MAL_SUCCEED;}strIOprint_formatted_bit(int *ret, ptr fmt, bit *s){	stream *fd = MCgetClient()->fdout;	char *format;	str msg;	str c;	/* TODO: check format properly !!! including sizes */	format = *(str *) fmt;	if (format == NULL)		throw(MAL, "io.print", "Format missing");	if (formatMarker(format) != 1)		throw(MAL, "io.print", "Too many %%");	if (*s == bit_nil)		return IOprint_formatted_nil(fd, *(str *) fmt, 's');	if (*s)		msg = "true";	else		msg = "false";	@:formatConversion(s,msg)@	if (*ret == EOF)		throw(MAL, "io.print", "End-of-file encountered");	return MAL_SUCCEED;}@-@+ Bulk export/loadingTo simplify conversion between versions and to interface with otherapplications, we use a simple import/export operation.The conversion routine assumes space in the buffer for storing the result.@{@include prelude.mx@-A BAT can be saved in Monet format using the export command.It is of particular use in preparing an ASCII version for migration.The exported file is saved in the context of the directorywhere the server was started unless an absolute file name waspresented.@cstrIOexport(int *ret, int *bid, str *fnme){	BAT *b;	stream *s = open_wastream(*fnme);	if ((b = BATdescriptor(*bid)) == NULL) {		throw(MAL, "io.export", "Cannot access descriptor");	}	if (s == NULL || stream_errnr(s)) {		*ret = FALSE;		throw(MAL, "io.export", "Could not open file");	}	BATprintf(s, b);	stream_close(s);	stream_destroy(s);	*ret = TRUE;	BBPunfix(b->batCacheid);	return MAL_SUCCEED;}@-The import command reads a single BAT from an ASCII file. It assumesa layout compatible with that produced by print or export.@c#define COMMA ','strIOimport(int *ret, int *bid, str *fnme){	BAT *b;	int (*hconvert) (str, int *, ptr *), (*tconvert) (str, int *, ptr *);	size_t bufsize = 2048;	/* NIELS:tmp change used to be 1024 */	char *base, *cur, *end;	char *buf = (char *) GDKmalloc(bufsize);	ptr h = 0, t = 0;	int lh = 0, lt = 0;	FILE *fp = fopen(*fnme, "r");	if ((b = BATdescriptor(*bid)) == NULL) {		throw(MAL, "io.import", "Cannot access descriptor");	}	hconvert = BATatoms[BAThtype(b)].atomFromStr;	tconvert = BATatoms[BATttype(b)].atomFromStr;@-Open the file. Memory map it to minimize buffering problems.@c	if (fp == NULL) {		BBPunfix(b->batCacheid);		throw(MAL, "io.import", "Could not open file");	} else {		int fn;		struct stat st;		if ((fn = fileno(fp)) <= 0) {			BBPunfix(b->batCacheid);			throw(MAL, "io.import", "Fileno fails");		}		if (fstat(fn, &st) != 0) {			BBPunfix(b->batCacheid);			throw(MAL, "io.imports", "Fstat fails");		}		(void) fclose(fp);		if (st.st_size <= 0) {			BBPunfix(b->batCacheid);			throw(MAL, "io.imports", "File is empty or fstat broken");		}		base = cur = (char *) MT_mmap(*fnme, MMAP_SEQUENTIAL, 0, st.st_size);		end = cur + st.st_size;		if (cur == (char *) -1) {			BBPunfix(b->batCacheid);			throw(MAL, "io.mport", "MT_mmap failed");		}	}@-Parse a line. Copy it into a buffer. Concat broken lines with a slash.@c	while (cur < end) {		str dst = buf, src = cur, p = strchr(cur, '\n');		size_t l = p - cur;		if (!p) {			p = end;		} else			while (src[l - 1] == '\\') {				@:memcpy@				dst += l - 1;				src += l + 1;				if ((p = strchr(src, '\n')) == 0) {					p = end;					break;				}				l = p - src;			}		@:memcpy@@-@= memcpy		if (buf+bufsize < dst+l) {			int len = dst - buf;			size_t inc = (size_t) ((dst+l) - buf);			buf = (char*) GDKrealloc((void*) buf, bufsize = MAX(inc,bufsize)*2);			dst = buf + len;		}		memcpy(dst, src, l-1);@c		dst[l] = 0;		cur = p + 1;@-Parse the line, and insert a BUN.@c		for (p = buf; *p && GDKisspace(*p); p++)			;		if (*p == '#')			continue;@-@= parsevalue		for (;*p && *p != @2; p++);		if (*p) for (p++; *p && GDKisspace(*p); p++);		if (*p == 0) {			char msg[BUFSIZ];			BBPunfix(*ret=b->batCacheid);			snprintf(msg,BUFSIZ,"error in input %s",buf);			throw(MAL,  "io.import", msg);		}		p += @1(p, @3, @4);@c		@:parsevalue(hconvert,'[',&lh,(ptr *) &h)@		@:parsevalue(tconvert,COMMA,&lt,(ptr *) &t)@		BUNins(b, h, t, FALSE);@-Unmap already parsed memory, to keep the memory usage low.@c#ifndef WIN32#define MAXBUF 40*MT_pagesize()		if ((unsigned) (cur - base) > MAXBUF) {			MT_munmap(base, MAXBUF);			base += MAXBUF;		}#endif	}@-Cleanup and exit. Return the filled BAT.@c	if (h)		GDKfree(h);	if (t)		GDKfree(t);	GDKfree(buf);	MT_munmap(base, end - base);	BBPkeepref(*ret= b->batCacheid);	return MAL_SUCCEED;}@}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -