From 5b466460d6527cd7f7ccc81698a2dfca59c0a2ed Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Sun, 2 Oct 2016 00:18:53 +1000 Subject: [PATCH] Gmt module: Conversion function for GMTRegion now using new parameter reading scheme and error reporting. --- modules/gmt/modgmt_func.h | 100 ++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 59 deletions(-) diff --git a/modules/gmt/modgmt_func.h b/modules/gmt/modgmt_func.h index c4d0a61..26b8ca8 100644 --- a/modules/gmt/modgmt_func.h +++ b/modules/gmt/modgmt_func.h @@ -225,95 +225,77 @@ template<> class Convert2Struct { public: - struct gmt_region operator()(const ObjectList* input, bool* issuc) const + struct gmt_region operator()(const ObjectList* input, bool* issuc, std::string& err) const { struct gmt_region r; - auto size=input->Size(); bool suc=true; - if(1==size) // Cases 1, 2 and 3 + if(1==input->Size()) // Cases 1, 2 and 3 { - Base2Region region(input,0); - r=region(&suc); + Base2Region region; + r=region.Convert(input->At(0),&suc,err); if(!suc) goto fail; // Conversion failed return r; } // Case 4 { - std::string type; - bool typeexist=false; - { - Base2String t(input,"type"); - if(t.Exist()) - { - type=t(&suc); - tolower(type); - if(!suc) goto fail; // Conversion failed - typeexist=true; - } - else type="nobbox"; - } - if(r.Convert(type)) return r; // type is one of "global*" string, we can return, becuse upd is irrelevant + ONFPar region("region"); + ONPar type("type","nobbox"); + ONPar xb("xb"),xe("xe"),yb("yb"),ye("ye"); - bool upd; + {ParseNamedParameters params(input,type,region,xb,xe,yb,ye); if(!params) {err=params.Error(); goto fail;} } // Fail to parse by variant 4 + + bool typeexist=type.Exist(); { - BaseMT2Region updarg(input,"r","region"); - r=updarg(&upd,&suc); - if(upd && !suc) goto fail; // Conversion failed or too many arguments - suc=true; + std::string t; + if(r.Convert(type,t)) return r; // type is one of "global*" string, we can return, because upd is irrelevant } - Base2Coord ixb(input,"xb"), ixe(input,"xe"), iyb(input,"yb"), iye(input,"ye"); - if(!(typeexist || ixb.Exist() || ixe.Exist() || iyb.Exist() || iye.Exist() || upd)) goto case5; // No named parameters, no update mode, possibly case 5 - struct gmt_coord xb,yb,xe,ye; + bool upd=region.Exist(); + + if(!(typeexist || xb.Exist() || xe.Exist() || yb.Exist() || ye.Exist() || upd)) goto case5; // No named parameters, no update mode, possibly case 5 + struct gmt_coord cxb,cxe,cyb,cye; bool isbbox=upd?(gmt_region::BBOX==r.type):false; if(typeexist) { - if("bbox"==type) isbbox=true; - if("nobbox"==type) isbbox=false; - if("bbox"!=type && "nobbox"!=type) goto fail; // Unknown type + if("bbox"==type.Value()) isbbox=true; + if("nobbox"==type.Value()) isbbox=false; + if("bbox"!=type.Value() && "nobbox"!=type.Value()) goto fail; // Unknown type } if(upd) { - if(!ixb.Exist()) xb=r.xb; - if(!ixe.Exist()) xe=r.xe; - if(!iyb.Exist()) yb=r.yb; - if(!iye.Exist()) ye=r.ye; + cxb=xb.Exist()?xb:region->xb; + cxe=xe.Exist()?xe:region->xe; + cyb=yb.Exist()?yb:region->yb; + cye=ye.Exist()?ye:region->ye; } - else suc=suc && ixb.Exist() && ixe.Exist() && iyb.Exist() && iye.Exist(); // In "new" mode all parameters must exists - if(ixb.Exist()) xb=ixb(&suc); - if(ixe.Exist()) xe=ixe(&suc); - if(iyb.Exist()) yb=iyb(&suc); - if(iye.Exist()) ye=iye(&suc); - if(!suc) goto fail; // Something wrong - if(!r.Convert(xb,xe,yb,ye,isbbox)) goto fail; // Conversion failed + else + { + suc=suc && xb.Exist() && xe.Exist() && yb.Exist() && ye.Exist(); // In "new" mode all parameters must exists + cxb=xb; cxe=xe; cyb=yb; cye=ye; + } + if(!suc) {err="To specify region all four coordinates must be set"; goto fail;} // Something wrong + if(!r.Convert(cxb,cxe,cyb,cye,isbbox)) {err="Error conversion of coordinates to region"; goto fail;} // Conversion failed return r; // Case 3 with all parameters } case5: // Case 5 - if(4==size || 5==size) { - bool isbbox=false; - if(5==size) - { - Base2String type(input,4); - std::string str=type(&suc); - if(!suc) goto fail; // Wrong parameter - tolower(str); - if("bbox"!=str || "nobbox"!=str) goto fail; // Unknown fifth parameter - if("bbox"==str) isbbox=true; - } - Base2Coord ixb(input,0), ixe(input,(isbbox?2:1)), iyb(input,(isbbox?1:2)), iye(input,3); - struct gmt_coord xb=ixb(&suc),yb=iyb(&suc),xe=ixe(&suc),ye=iye(&suc); - if(suc) - { - if(!r.Convert(xb,xe,yb,ye,isbbox)) goto fail; // Conversion failed - return r; // Case 4 with all parameters - } + OPosPar type("type","nobbox"); + RPosPar p1("p1"),p2("p2"),p3("p3"),p4("p4"); + ParsePositionalParameters params(input,p1,p2,p3,p4,type); + + if(!params) {err=params.Error(); goto fail;} // Fail to parse by variant 5 + + bool isbbox=("bbox"==type.Value()); + if("bbox"!=type.Value() && "nobbox"!=type.Value()) {err="Fifth parameter can be \"bbox\" or \"nobbox\" only"; goto fail;} // Unknown fifth parameter + + if(!r.Convert(p1,isbbox?p3:p2,isbbox?p2:p3,p4,isbbox)) {err="Error conversion of coordinates to region"; goto fail;} // Conversion failed + return r; // Case 4 with all parameters } fail: