source: issm/trunk/src/c/shared/Enum/Synchronize.sh@ 16560

Last change on this file since 16560 was 16560, checked in by Mathieu Morlighem, 11 years ago

merged trunk-jpl and trunk for revision 16554

  • Property svn:executable set to *
File size: 5.7 KB
Line 
1#!/bin/bash
2#Synchronize EnumToStringx.cpp and StringToEnumx.cpp and matlab Enums
3
4#Get all lines of EnumDefinitions2.h which hold Enum | remove all commas > put everything in file temp
5cat EnumDefinitions.h | grep -e "[0-9]Enum," -e "[a-zA-Z]Enum," -e "MaximumNumberOfDefinitionsEnum" | grep -v include | sed -e "s/,/ /g" | awk '{print $1}' > temp
6
7#Removed existing files
8rm $ISSM_DIR/src/m/enum/*.m
9rm $ISSM_DIR/src/m/enum/*.py
10rm $ISSM_DIR/src/c/shared/Enum/EnumToStringx.cpp
11rm $ISSM_DIR/src/c/shared/Enum/StringToEnumx.cpp
12
13#Get number of enums
14NUMENUMS=$(wc -l temp | awk '{printf("%s",$1);}');
15
16#Build EnumToStringx.cpp {{{
17#Header
18cat <<END > $ISSM_DIR/src/c/shared/Enum/EnumToStringx.cpp
19/*
20* \file EnumToStringx.cpp:
21* \brief: output string associated with enum
22*
23* WARNING: DO NOT MODIFY THIS FILE
24* this file has been automatically generated by Synchronize.sh
25* Please read README for more information
26*/
27
28#include <cstring>
29#include "./Enum.h"
30#include "../Exceptions/exceptions.h"
31#include "../MemOps/MemOps.h"
32
33const char* EnumToStringx(int en){
34
35 switch(en){
36
37END
38#core
39cat temp | awk '{print "\t\t" "case " $1" : return \"" substr($1,1,length($1)-4) "\";"}' >> $ISSM_DIR/src/c/shared/Enum/EnumToStringx.cpp
40#Footer
41cat <<END >> $ISSM_DIR/src/c/shared/Enum/EnumToStringx.cpp
42 default : return "unknown";
43
44 }
45}
46void EnumToStringx(char** pstring,int enum_in){
47 char *string = NULL;
48 int len = 0;
49
50 len=strlen(EnumToStringx(enum_in));
51 string=xNew<char>(len+1);
52 memcpy(string,EnumToStringx(enum_in),(len+1)*sizeof(char));
53
54 /*Assign output pointer*/
55 *pstring=string;
56}
57END
58#}}}
59#Build StringToEnumx.cpp {{{
60#Header
61cat <<END > $ISSM_DIR/src/c/shared/Enum/StringToEnumx.cpp
62/*
63* \file StringToEnumx.cpp:
64* \brief: output enum associated with string
65*
66* WARNING: DO NOT MODIFY THIS FILE
67* this file has been automatically generated by Synchronize.sh
68* Please read README for more information
69*/
70
71#include <cstring>
72#include "./Enum.h"
73#include "../Exceptions/exceptions.h"
74
75int StringToEnumx(const char* name,bool notfounderror){
76
77 int stage=1;
78
79END
80
81#core
82i1=1;
83i2=120;
84for (( i=1 ; i<=100 ; i++ )); do
85 echo " if(stage==$i){" >> $ISSM_DIR//src/c/shared/Enum/StringToEnumx.cpp
86 awk -v i1=$i1 -v i2=$i2 '{if(NR>=i1 && NR<=i2) print $0 }' temp |
87 awk '{print "\t" ((NR==1)?" if":" else if") " (strcmp(name,\"" substr($1,1,length($1)-4) "\")==0) return " $1 ";"}' >> $ISSM_DIR//src/c/shared/Enum/StringToEnumx.cpp
88 echo " else stage=$(($i+1));" >> $ISSM_DIR//src/c/shared/Enum/StringToEnumx.cpp
89 echo " }" >> $ISSM_DIR//src/c/shared/Enum/StringToEnumx.cpp
90
91 if [ $i2 -ge $NUMENUMS ]; then break; fi
92 let i1=$i1+120
93 let i2=$i2+120
94done
95
96#footer
97cat <<END >> $ISSM_DIR/src/c/shared/Enum/StringToEnumx.cpp
98 /*If we reach this point, the string provided has not been found*/
99 if(notfounderror)
100 _error_("Enum " << name << " not found");
101 else
102 return -1;
103}
104END
105#}}}
106##Build EnumToAnalysis.cpp {{{
107##Header
108#cat <<END > $ISSM_DIR/src/c/analyses/EnumToAnalysis.cpp
109#/*
110#* \file EnumToAnalysis.cpp
111#* \brief: output class depending on enum
112#*
113#* WARNING: DO NOT MODIFY THIS FILE
114#* this file has been automatically generated by Synchronize.sh
115#* Please read README for more information
116#*/
117#
118##include "./analyses.h"
119##include "../shared/shared.h"
120#
121#Analysis* EnumToAnalysis(int analysis_enum){
122#
123# switch(analysis_enum){
124#END
125##core
126#cat temp | grep [a-z]Analysis | \
127# grep -v DefaultAnalysis | grep -v FlaimAnalysis | grep -v SurfaceSlopeAnalysis | grep -v BedSlopeAnalysis | \
128# awk '{print "\t\t#ifdef _HAVE_"toupper(substr($1,1,length($1)-12))"_\n\t\t" "case " $1" : return new " substr($1,1,length($1)-4) "();\n\t\t#endif"}' \
129# >> $ISSM_DIR/src/c/analyses/EnumToAnalysis.cpp
130#
131##Footer
132#cat <<END >> $ISSM_DIR/src/c/analyses/EnumToAnalysis.cpp
133#default : _error_("enum provided not supported ("<<EnumToStringx(analysis_enum)<<")");
134# }
135#}
136#END
137##}}}
138#Build EnumDefinitions.py{{{
139cat <<END > $ISSM_DIR/src/m/enum/EnumDefinitions.py
140from StringToEnum import StringToEnum
141
142"""
143
144WARNING: DO NOT MODIFY THIS FILE
145this file has been automatically generated by src/c/shared/Enum/Synchronize.sh
146Please read src/c/shared/Enum/README for more information
147
148"""
149
150END
151#core
152cat temp | awk '{print "def " $1"(): return StringToEnum(\"" substr($1,1,length($1)-4) "\")[0]"}' >> $ISSM_DIR/src/m/enum/EnumDefinitions.py
153#}}}
154
155# go through the lines of temp
156ENUM=0;
157
158for NAMEENUM in $(cat temp); do
159
160 #Get name and enum of the line i
161 NAME=$(echo $NAMEENUM | sed -e "s/Enum//g")
162 #offset Enum by one (Enum begins with 0 and not 1!)
163 let ENUM=$ENUM+1
164
165 #print info {{{
166 if [ $ENUM -lt 10 ]
167 then
168 printf "\r "
169 printf "\r $ENUM/$NUMENUMS Adding "$NAME"..."
170 else
171 if [ $ENUM -lt 100 ]
172 then
173 printf "\r "
174 printf "\r $ENUM/$NUMENUMS Adding "$NAME"..."
175 else
176 printf "\r "
177 printf "\r$ENUM/$NUMENUMS Adding "$NAME"..."
178 fi
179 fi
180 #}}}
181 #Add case to matlabenum file{{{
182 cat <<END > $ISSM_DIR"/src/m/enum/"$(echo $NAMEENUM".m")
183function macro=$(echo $NAMEENUM)()
184%$(echo $NAMEENUM | awk {'print toupper($1)'}) - Enum of $(echo $NAME)
185%
186% WARNING: DO NOT MODIFY THIS FILE
187% this file has been automatically generated by src/c/shared/Enum/Synchronize.sh
188% Please read src/c/shared/Enum/README for more information
189%
190% Usage:
191% macro=$NAMEENUM()
192
193macro=StringToEnum('$NAME');
194END
195#}}}
196
197done
198#clean up{{{
199rm temp
200#}}}
201#print info {{{
202printf "\r "
203printf "\rdone!\n"
204#}}}
Note: See TracBrowser for help on using the repository browser.