📄 ascii_processor.c
字号:
lines.set_cur_size(idx); lines.set_max_size_and_preserve_cur_size(idx+num_v1+num_v2+2); const double d1= 1.0/double(num_v1); const double d2= 1.0/double(num_v2); for (int i=0; i < num_v2+1; i++) { Line2d & l = lines.tab[idx]; l.p1.x= p_x + i * v2_x * d2 ; l.p1.y= p_y + i * v2_y * d2 ; l.p2.x= l.p1.x + v1_x; l.p2.y= l.p1.y + v1_y; idx++; } for (int i=0; i < num_v1+1; i++) { Line2d & l = lines.tab[idx]; l.p1.x= p_x + i * v1_x * d1 ; l.p1.y= p_y + i * v1_y * d1 ; l.p2.x= l.p1.x + v2_x; l.p2.y= l.p1.y + v2_y; idx++; } if ( strskip(next,DELIMITER_CHAR,next) ) break; } build->set_cmd_insert_lines(fref,id,idx,lines.tab,lay,col); return CMD_OK;}bool AsciiProcessor::ins_string_grid(const char * buf, int fref, BuilderBase * build, const char * & next) { int id= 0, lay= 0, fil= 0; RGBcolor col(0,0,0); if ( ! get_obj_attr(buf,id,lay,col,fil, next) ) return CMD_ERR; while (true) { double p_x, p_y, v1_x, v1_y, v2_x, v2_y, d3, d4, d5; int num_v1= 1, num_v2= 1; if ( ! strskip(next,'(',next) ) return CMD_ERR; if ( get_max5_tuple(next,2,p_x,p_y,d3,d4,d5,next) != 2 ) return CMD_ERR; if ( get_max5_tuple(next,2,v1_x,v1_y,d3,d4,d5,next) != 2 ) return CMD_ERR; if ( strskip(next,':',next) ) { if ( ! str2val(next,num_v1,next) ) return CMD_ERR; } if ( get_max5_tuple(next,2,v2_x,v2_y,d3,d4,d5,next) != 2 ) return CMD_ERR; if ( strskip(next,':',next) ) { if ( ! str2val(next,num_v2,next) ) return CMD_ERR; } const double d1= 1.0/double(num_v1); const double d2= 1.0/double(num_v2); bool early_stop= false; for (int i=0; i < num_v2; i++) { Point2d point; point.x= p_x + i * v2_x * d2 ; point.y= p_y + i * v2_y * d2 ; for (int j=0; j < num_v1; j++) { int offset; if ( strskip(next,')',next) ) { early_stop= true; break; } buf= next; int res= get_string(buf,offset,next); if ( res < 0 ) return CMD_ERR; VisualObject2d * string2d= new VisualString2d(id,lay,col,point,res,buf+offset); build->set_cmd_insert_visobject(fref,string2d); point.x+= v1_x * d1; point.y+= v1_y * d1; } if (early_stop) break; } if ( ! early_stop ) if ( ! strskip(next,')',next) ) return CMD_ERR; if ( strskip(next,DELIMITER_CHAR,next) ) break; } return CMD_OK;}bool AsciiProcessor:: get_obj_attr(const char * buf, int & id, int & lay, RGBcolor & col, int & fil,char const* & next) { next= buf; bool got_attr[4]= { false, false, false, false }; while (next) { if ( strskip(buf,"id",next) ) { if ( got_attr[0] ) return false; got_attr[0]= true; buf= next; if (!strskip(buf,"=",next) ) return false; buf= next; if ( !str2val(buf,id,next) ) return false; buf= next; } else if ( strskip(buf,"lay",next) ) { if ( got_attr[1] ) return false; got_attr[1]= true; buf= next; if (!strskip(buf,"=",next) ) return false; buf= next; if ( !str2val(buf,lay,next) ) return false; buf= next; } else if ( strskip(buf,"col",next) ) { if ( got_attr[2] ) return false; got_attr[2]= true; buf= next; if (!strskip(buf,"=",next) ) return false;#if 0 buf= next; if (!strskip(buf,"#",next) ) return false;#endif buf= next; if ( !get_col(buf,col,next) ) { P_ERROR("wrong color [" << buf << "]"); return false; } buf= next; //if ( !strconv(buf,col,next) ) //return false; buf= next; } else if ( strskip(buf,"fil",next) ) { if ( got_attr[3] ) return false; got_attr[3]= true; buf= next; if (!strskip(buf,"=",next) ) return false; buf= next; bool b; if ( !str2val(buf,b,next) ) return false; fil= b; buf= next; } else { next= buf; return true; } } next= buf; return true;}bool AsciiProcessor:: get_frame_attr(const char * buf, int & id, int & lay, int & vis, char const* & next) { next= buf; bool got_attr[4]= { false, false, false, false }; while (next) { if ( strskip(buf,"id",next) ) { if ( got_attr[0] ) return false; got_attr[0]= true; buf= next; if (!strskip(buf,"=",next) ) return false; buf= next; if ( !str2val(buf,id,next) ) return false; buf= next; } else if ( strskip(buf,"lay",next) ) { if ( got_attr[1] ) return false; got_attr[1]= true; buf= next; if (!strskip(buf,"=",next) ) return false; buf= next; if ( !str2val(buf,lay,next) ) return false; buf= next; } else if ( strskip(buf,"vis",next) ) { bool b; if ( got_attr[1] ) return false; got_attr[1]= true; buf= next; if (!strskip(buf,"=",next) ) return false; buf= next; if ( !str2val(buf,b,next) ) return false; vis= b; buf= next; } else { next= buf; return true; } } next= buf; return true;}bool AsciiProcessor::get_col(const char * buf, RGBcolor & col, const char* & next) { while (*buf == ' ') buf++; if ( buf == '\0' ) return false; unsigned char c[3]; bool res= false; for (int i=0; i<3; i++) { if ( buf[0]!= '\0' && buf[1] != '\0') { char chr[3]; chr[0]= buf[0]; chr[1]= buf[1]; chr[2]= '\0'; char* error_ptr; long tmp_long= strtol(chr, &error_ptr, 16); if (error_ptr!= chr+2 || tmp_long < 0 || tmp_long > 255) { //cerr << "\ntmp_long=" << tmp_long; break; } c[i]= (unsigned char) tmp_long; buf+= 2; if (i==2) res= true; } else break; } if (res== true) { next= buf; col= RGBcolor(c[0],c[1],c[2]); return true; } return false;}/* results: 0 : a empty tuple was received "()" 1 : a one tuple "( <num> )" 2,3,4,5 analogous with 1 -1: the was no tuple at all, i.e. the string doesn't begin with a '(' -2: some failure occured*/int AsciiProcessor::get_max5_tuple(const char *buf, int num, double & d0, double & d1, double & d2, double & d3, double & d4, char const* & next) { if ( !strskip(buf,'(',next) ) return -1; buf= next; if ( !str2val(buf,d0,next) ) { if ( strskip(buf,')',next) ) { return 0; } return -2; } if (num<1) return -2; buf=next; if ( !strskip(buf,',',next) ) { if ( strskip(buf,')',next) ) { return 1; } return -2; } if (num<2) return -2; buf= next; if ( !str2val(buf,d1,next) ) return -2; buf= next; if ( !strskip(buf,',',next) ) { if (strskip(buf,')',next) ) { return 2; } return -2; } if (num<3) return -2; buf= next; if ( !str2val(buf,d2,next) ) return -2; buf= next; if ( !strskip(buf,',',next) ) { if (strskip(buf,')',next) ) { return 3; } return -2; } if (num<4) return -2; buf= next; if ( !str2val(buf,d3,next) ) return -2; buf= next; if ( !strskip(buf,',',next) ) { if (strskip(buf,')',next) ) { return 4; } return -2; } if (num<5) return -2; buf= next; if ( !str2val(buf,d4,next) ) return -2; buf= next; if ( !strskip(buf,')',next) ) return -2; return 5;}int AsciiProcessor::get_frame(const char * buf, Point2d & p, Angle & a, char const* & next) { double d0,d1,d2,d3,d4; int res= get_max5_tuple(buf,3,d0,d1,d2,d3,d4,next); if (res==2) { p.x= d0; p.y= d1; } if (res==3) { p.x= d0; p.y= d1; a= Angle(d2); } if (res==1) a= Angle(d0); return res;}int AsciiProcessor::get_point(const char * buf, Point2d & p, char const* & next) { double d2,d3,d4; return get_max5_tuple(buf,2,p.x,p.y,d2,d3,d4,next);}int AsciiProcessor::get_line(const char * buf, Line2d & l, char const* & next) { double d4; return get_max5_tuple(buf,4,l.p1.x,l.p1.y,l.p2.x,l.p2.y,d4,next);}int AsciiProcessor::get_circle(const char * buf, Circle2d & c, char const* & next) { double d3,d4; return get_max5_tuple(buf,3,c.center.x,c.center.y,c.radius,d3,d4,next);}int AsciiProcessor::get_circlearc(const char * buf, CircleArc2d & c, char const* & next) { double d3,d4; int res= get_max5_tuple(buf,5,c.center.x,c.center.y,c.radius,d3,d4,next); if (res>3) c.ang1= Angle(d3); if (res>4) c.ang2= Angle(d4); return res;}int AsciiProcessor::get_points(const char *buf, Multi<Point2d> & points, char const* & next) { int res; int k=0; while (1) { if ( k>= points.max_size ) { points.set_cur_size(k); //do not loose the points points.set_max_size_and_preserve_cur_size(k+k+1); } res= get_point(buf,points.tab[k],next); if (res == -1) break; if (res != 2) return -1; buf= next; k++; }; if ( ! strskip(buf,DELIMITER_CHAR,next) ) { P_ERROR("wrong delimiter char"); return -1; } points.set_cur_size(k); return k;}int AsciiProcessor::get_lines(const char *buf, Multi<Line2d> & lines, char const* & next) { int res; int k=0; while (1) { if ( k>= lines.max_size ) { lines.set_cur_size(k); //do not loose entries lines.set_max_size_and_preserve_cur_size(k+k+1); } res= get_line(buf,lines.tab[k],next); if (res == -1) break; if (res != 4) return -1; buf= next; k++; }; if ( ! strskip(buf,DELIMITER_CHAR,next) ) { P_ERROR("wrong delimiter char"); return -1; } lines.set_cur_size(k); return k;}int AsciiProcessor::get_circles(const char *buf, Multi<Circle2d> & circles, char const* & next) { int res; int k=0; while (1) { if ( k>= circles.max_size ) { circles.set_cur_size(k); //do not loose entries circles.set_max_size_and_preserve_cur_size(k+k+1); } res= get_circle(buf,circles.tab[k],next); if (res == -1) break; if (res != 3) return -1; buf= next; k++; }; if ( ! strskip(buf,DELIMITER_CHAR,next) ) { P_ERROR("wrong delimiter char"); return -1; } circles.set_cur_size(k); return k;}int AsciiProcessor::get_circlearcs(const char *buf, Multi<CircleArc2d> & circles, char const* & next) { int res; int k=0; while (1) { if ( k>= circles.max_size ) { circles.set_cur_size(k); //do not loose entries circles.set_max_size_and_preserve_cur_size(k+k+1); } res= get_circlearc(buf,circles.tab[k],next); if (res == -1) break; if (res != 5 && res != 3) return -1; buf= next; k++; }; if ( ! strskip(buf,DELIMITER_CHAR,next) ) { P_ERROR("wrong delimiter char"); return -1; } circles.set_cur_size(k); return k;}int AsciiProcessor::get_string(const char * buf, Multi<char> & str, char const* & next) { if ( !strskip(buf,"\"",next) ) return -1; buf= next; int k=0; while ( *buf != '\"' && *buf != '\0') { if ( k>= str.max_size ) { str.set_cur_size(k); //do not loose entries str.set_max_size_and_preserve_cur_size(k+k+1); } str.tab[k]= *buf; buf++; k++; } if ( *buf != '\"' ) return -2; buf++; next= buf; str.set_cur_size(k); return k;}int AsciiProcessor::get_string(const char * buf, int & offset, char const* & next) { if ( !strskip(buf,"\"",next) ) return -1; offset= next-buf; buf= next; int k=0; while ( *buf != '\"' && *buf != '\0') { buf++; k++; } if ( *buf != '\"' ) return -2; buf++; next= buf; return k;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -