| 1 |
|
| 2 |
|
| 3 |
def fun2method(inputfile, outputfile_basename, json_path): |
| 4 |
"""converts MIALib C function declarations into JIPLib C++ methods (outputfile_basename.cc file) and C++ method declarations (outputfile_basename.h file). Currently only convert desctuctive functions, i.e., ERROR_TYPE functions, with IMAGE * as first argument (IMAGE ** not yet taken into account). |
| 5 |
|
| 6 |
:param inputfile: string for input file containing extern declarations |
| 7 |
:param outputfile_basename: string for output file basename (i.e. without extension) |
| 8 |
:returns: True on success, False otherwise |
| 9 |
:rtype: |
| 10 |
|
| 11 |
""" |
| 12 |
|
| 13 |
import re |
| 14 |
import json |
| 15 |
import sys |
| 16 |
import os |
| 17 |
with open(json_path, 'r') as f: |
| 18 |
old2newDic = json.load(f) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
ifp=open(inputfile, 'r') |
| 23 |
|
| 24 |
f = open(outputfile_basename+'.cc', 'w') |
| 25 |
fh = open(outputfile_basename+'.h', 'w') |
| 26 |
|
| 27 |
|
| 28 |
lines=ifp.readlines() |
| 29 |
|
| 30 |
for line in lines: |
| 31 |
print(line) |
| 32 |
|
| 33 |
name=re.match(r'extern ERROR_TYPE (.*)\((.*)\);',line) |
| 34 |
args=re.split(',', name.group(2)) |
| 35 |
re.sub('\**', '', re.sub('.* ', '', args[0])) |
| 36 |
|
| 37 |
arglist = [] |
| 38 |
|
| 39 |
for i in args: |
| 40 |
atype=re.match(' *(.* \**)', i).group(1) |
| 41 |
aname=re.sub('\**', '', re.sub('.* ', '', i)) |
| 42 |
arglist.append([atype, aname]) |
| 43 |
|
| 44 |
|
| 45 |
a= { "name" : name.group(1), |
| 46 |
"arguments" : arglist } |
| 47 |
|
| 48 |
print(a) |
| 49 |
|
| 50 |
imDeclare=[] |
| 51 |
imRasterArray=[] |
| 52 |
|
| 53 |
GTDeclare=[] |
| 54 |
GTVars=[] |
| 55 |
|
| 56 |
GDTypes = ['GDT_Byte', 'GDT_Int16', 'GDT_UInt16', 'GDT_Int32', 'GDT_UInt32', 'GDT_Float32', 'GDT_Float64'] |
| 57 |
MIATypes = ['uc_', 's_', 'us_', 'i32_', 'u32_', 'f_', 'd_'] |
| 58 |
CTypes = ['unsigned char', 'short int', 'unsigned short int', 'int', 'unsigned int', 'float', 'double'] |
| 59 |
|
| 60 |
methodDeclaration='void Jim::d_'+old2newDic.get(a.get("name"))+'(' |
| 61 |
print(methodDeclaration) |
| 62 |
|
| 63 |
cSeparator=', ' |
| 64 |
separator = '' |
| 65 |
|
| 66 |
if a.get("arguments")[0][0] != 'IMAGE *': |
| 67 |
print('WARNING: ',line,' does not have IMAGE * as first argument') |
| 68 |
continue |
| 69 |
else: |
| 70 |
imDeclare.append(a.get("arguments")[0][0]+' '+a.get("arguments")[0][1]+'=this->getMIA(iband);') |
| 71 |
cCall=a.get("arguments")[0][1] |
| 72 |
|
| 73 |
imCount=1 |
| 74 |
for idx, arg in enumerate(a.get("arguments")[1:len(a.get("arguments"))]): |
| 75 |
print(methodDeclaration) |
| 76 |
print(idx) |
| 77 |
print(idx) |
| 78 |
print(separator) |
| 79 |
print(arg) |
| 80 |
print(arg[0]) |
| 81 |
|
| 82 |
if (arg[0]=='IMAGE *'): |
| 83 |
methodDeclaration+=separator+'Jim& imRaster_'+arg[1] |
| 84 |
imRasterArray.append('imRaster_'+arg[1]) |
| 85 |
imDeclare.append(arg[0]+' '+arg[1]+' = (this!=&imRaster_'+arg[1]+')? imRaster_'+arg[1]+'.getMIA(iband) : '+a.get("arguments")[0][1]+';') |
| 86 |
elif (arg[0]=='G_TYPE '): |
| 87 |
GTDeclare.append(arg[0]+' '+arg[1]+';') |
| 88 |
GTVars.append(arg[1]) |
| 89 |
methodDeclaration+=separator+'double '+'d_'+arg[1] |
| 90 |
else: |
| 91 |
methodDeclaration+=separator+arg[0]+' '+arg[1] |
| 92 |
|
| 93 |
cCall+=cSeparator+arg[1] |
| 94 |
separator=', ' |
| 95 |
|
| 96 |
fh.write(re.sub(r'Jim::','',methodDeclaration+separator+'int band=-1);\n')) |
| 97 |
|
| 98 |
methodDeclaration+=separator+'int band)' |
| 99 |
|
| 100 |
f.write(methodDeclaration+'{') |
| 101 |
f.write('\n\ttry{') |
| 102 |
f.write('\n\t\tif(nrOfBand()<=band){') |
| 103 |
f.write('\n\t\t\tstd::string errorString=\"Error: band number exceeds number of bands in input image\";') |
| 104 |
f.write('\n\t\t\tthrow(errorString);') |
| 105 |
f.write('\n\t\t}') |
| 106 |
for i in imRasterArray: |
| 107 |
f.write('\n\t\tif('+i+'.nrOfBand()<=band){') |
| 108 |
f.write('\n\t\t\tstd::string errorString=\"Error: band number exceeds number of bands in input image\";') |
| 109 |
f.write('\n\t\t\tthrow(errorString);') |
| 110 |
f.write('\n\t\t}') |
| 111 |
|
| 112 |
f.write('\n\t\tstd::vector<unsigned int> bands;') |
| 113 |
f.write('\n\t\tif(band<0){') |
| 114 |
f.write('\n\t\t\tfor(unsigned int iband=0;iband<nrOfBand();++iband)') |
| 115 |
f.write('\n\t\t\t\tbands.push_back(iband);') |
| 116 |
f.write('\n\t\t}') |
| 117 |
f.write('\n\t\telse') |
| 118 |
f.write('\n\t\t\tbands.push_back(band);') |
| 119 |
f.write('\n\t\tfor(std::vector<unsigned int>::const_iterator bit=bands.begin();bit!=bands.end();++bit){') |
| 120 |
f.write('\n\t\t\tunsigned int iband=*bit;') |
| 121 |
|
| 122 |
f.write('\n\t\t\t'+a.get("arguments")[0][0]+' '+a.get("arguments")[0][1]+' = this->getMIA(iband);') |
| 123 |
llen=len(imDeclare) |
| 124 |
for i in range(1,llen): |
| 125 |
f.write('\n\t\t\t'+imDeclare[i]) |
| 126 |
for i in GTDeclare: |
| 127 |
f.write('\n\t\t\t'+i) |
| 128 |
|
| 129 |
if len(GTDeclare)>0: |
| 130 |
f.write('\n\t\t\tswitch(getDataType()){') |
| 131 |
for idx, GDType in enumerate(GDTypes): |
| 132 |
f.write('\n\t\t\tcase('+GDType+'):') |
| 133 |
for var_idx, GTVar in enumerate(GTVars): |
| 134 |
f.write('\n\t\t\t\t'+GTVar+'.'+MIATypes[idx]+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');') |
| 135 |
if (GDType=='GDT_Byte'): |
| 136 |
f.write('\n\t\t\t\t'+GTVar+'.'+'generic_'+'val=static_cast<'+CTypes[idx]+'>(d_'+GTVars[var_idx]+');') |
| 137 |
f.write('\n\t\t\t\tbreak;') |
| 138 |
f.write(''' |
| 139 |
\t\t\tdefault: |
| 140 |
\t\t\t\tstd::string errorString="Error: data type not supported"; |
| 141 |
\t\t\t\tthrow(errorString); |
| 142 |
\t\t\t\tbreak; |
| 143 |
\t\t\t}''') |
| 144 |
|
| 145 |
|
| 146 |
f.write('\n\t\t\tif(::'+a.get("name")+'('+cCall+') == NO_ERROR){') |
| 147 |
f.write('\n\t\t\t\tthis->setMIA(iband);') |
| 148 |
for i in imRasterArray: |
| 149 |
f.write('\n\t\t\t\tif(this!=&'+i+')') |
| 150 |
f.write('\n\t\t\t\t\t'+i+'.setMIA(iband);') |
| 151 |
|
| 152 |
f.write('\n\t\t\t}') |
| 153 |
|
| 154 |
f.write('\n\t\t\telse{') |
| 155 |
f.write('\n\t\t\t\tthis->setMIA(iband);') |
| 156 |
for i in imRasterArray: |
| 157 |
f.write('\n\t\t\t\tif(this!=&'+i+')') |
| 158 |
f.write('\n\t\t\t\t\t'+i+'.setMIA(iband);') |
| 159 |
f.write('\n\t\t\t\tstd::string errorString="Error: '+a.get("name")+'() function in MIA failed";') |
| 160 |
f.write('\n\t\t\t\tthrow(errorString);') |
| 161 |
f.write('\n\t\t\t}') |
| 162 |
|
| 163 |
f.write('\n\t\t}') |
| 164 |
f.write('\n\t}') |
| 165 |
|
| 166 |
f.write('\n\tcatch(std::string errorString){') |
| 167 |
f.write('\n\t\tstd::cerr << errorString << std::endl;') |
| 168 |
f.write('\n\tthrow;') |
| 169 |
f.write('\n\t}') |
| 170 |
f.write('\n\tcatch(...){') |
| 171 |
f.write('\n\t\tthrow;') |
| 172 |
f.write('\t\t\n}') |
| 173 |
f.write('\n}\n') |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
ifp.close() |
| 178 |
|
| 179 |
f.close() |
| 180 |
fh.close() |
| 181 |
|
| 182 |
return(True) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
import sys, getopt |
| 189 |
|
| 190 |
def main(argv): |
| 191 |
inputfile="miallib_errortype" |
| 192 |
outputfile="fun2method_errortype_d" |
| 193 |
json_path = "old2NewNames.json" |
| 194 |
try: |
| 195 |
opts, args = getopt.getopt(argv,"hi:o:j:",["ifile=","ofile=", "json="]) |
| 196 |
except getopt.GetoptError: |
| 197 |
print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>') |
| 198 |
sys.exit(2) |
| 199 |
for opt, arg in opts: |
| 200 |
if opt == '-h': |
| 201 |
print('fun2method.py -i <inputfile> -o <outputfilebasename> -j <json_path>') |
| 202 |
sys.exit() |
| 203 |
elif opt in ("-i", "--ifile"): |
| 204 |
inputfile = arg |
| 205 |
elif opt in ("-o", "--ofile"): |
| 206 |
outputfile = arg |
| 207 |
elif opt in ("-j", "--json"): |
| 208 |
json_path = arg |
| 209 |
print('Input file is "', inputfile) |
| 210 |
print('Output file is "', outputfile) |
| 211 |
print('JSON file is "', json_path) |
| 212 |
|
| 213 |
fun2method(inputfile, outputfile, json_path) |
| 214 |
|
| 215 |
if __name__ == "__main__": |
| 216 |
main(sys.argv[1:]) |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|