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

Last change on this file since 15396 was 14964, checked in by Mathieu Morlighem, 12 years ago

CHG: updated Synchronize.sh

  • Property svn:executable set to *
File size: 6.2 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," | 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){
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 _error_("Enum " << name << " not found");
100}
101END
102#}}}
103
104# go through the lines of temp
105ENUM=0;
106#Add header to pythonenum file{{{
107cat <<END > $ISSM_DIR/src/m/enum/EnumDefinitions.py
108from StringToEnum import StringToEnum
109
110"""
111
112 WARNING: DO NOT MODIFY THIS FILE
113 this file has been automatically generated by src/c/shared/Enum/Synchronize.sh
114 Please read src/c/shared/Enum/README for more information
115
116"""
117
118END
119#}}}
120
121for NAMEENUM in $(cat temp); do
122
123 #Get name and enum of the line i
124 NAME=$(echo $NAMEENUM | sed -e "s/Enum//g")
125 #offset Enum by one (Enum begins with 0 and not 1!)
126 let ENUM=$ENUM+1
127
128 #print info {{{
129 if [ $ENUM -lt 10 ]
130 then
131 printf "\r "
132 printf "\r $ENUM/$NUMENUMS Adding "$NAME"..."
133 else
134 if [ $ENUM -lt 100 ]
135 then
136 printf "\r "
137 printf "\r $ENUM/$NUMENUMS Adding "$NAME"..."
138 else
139 printf "\r "
140 printf "\r$ENUM/$NUMENUMS Adding "$NAME"..."
141 fi
142 fi
143 #}}}
144 #Add case to matlabenum file{{{
145 cat <<END > $ISSM_DIR"/src/m/enum/"$(echo $NAMEENUM".m")
146function macro=$(echo $NAMEENUM)()
147%$(echo $NAMEENUM | awk {'print toupper($1)'}) - Enum of $(echo $NAME)
148%
149% WARNING: DO NOT MODIFY THIS FILE
150% this file has been automatically generated by src/c/shared/Enum/Synchronize.sh
151% Please read src/c/shared/Enum/README for more information
152%
153% Usage:
154% macro=$NAMEENUM()
155
156macro=StringToEnum('$NAME');
157END
158#}}}
159 #Add case to pythonenum file{{{
160 cat <<END >> $ISSM_DIR/src/m/enum/EnumDefinitions.py
161def $(echo $NAMEENUM)():
162 """
163 $(echo $NAMEENUM | awk {'print toupper($1)'}) - Enum of $(echo $NAME)
164
165 WARNING: DO NOT MODIFY THIS FILE
166 this file has been automatically generated by src/c/shared/Enum/Synchronize.sh
167 Please read src/c/shared/Enum/README for more information
168
169 Usage:
170 macro=$NAMEENUM()
171 """
172
173 return StringToEnum('$NAME')[0]
174
175END
176#}}}
177
178done
179#MaximumNumberOfEnums (matlab){{{
180cat <<END > $ISSM_DIR/src/m/enum/MaximumNumberOfEnums.m
181function macro=MaximumNumberOfEnums()
182%$(echo "MaximumNumberOfEnums" | awk {'print toupper($1)'}) - Enum of MaximumNumberOfEnums
183%
184% WARNING: DO NOT MODIFY THIS FILE
185% this file has been automatically generated by src/c/shared/Enum/Synchronize.sh
186% Please read src/c/shared/Enum/README for more information
187%
188% Usage:
189% macro=MaximumNumberOfEnums()
190
191macro=$(cat EnumDefinitions.h | grep -e "[0-9]Enum" -e "[a-zA-Z]Enum" | grep -v include \
192 | awk '{ printf "%s %s\n", NR-1, $0 }' \
193 | grep "MaximumNumberOfEnums" | awk '{print $1}');
194END
195#}}}
196#MaximumNumberOfEnums (python){{{
197cat <<END >> $ISSM_DIR/src/m/enum/EnumDefinitions.py
198def MaximumNumberOfEnums():
199 """
200 $(echo "MaximumNumberOfEnums" | awk {'print toupper($1)'}) - Enum of MaximumNumberOfEnums
201
202 WARNING: DO NOT MODIFY THIS FILE
203 this file has been automatically generated by src/c/shared/Enum/Synchronize.sh
204 Please read src/c/shared/Enum/README for more information
205
206 Usage:
207 macro=MaximumNumberOfEnums()
208 """
209
210 return $(cat EnumDefinitions.h | grep -e "[0-9]Enum" -e "[a-zA-Z]Enum" | grep -v include \
211 | awk '{ printf "%s %s\n", NR-1, $0 }' \
212 | grep "MaximumNumberOfEnums" | awk '{print $1}')
213
214END
215#}}}
216
217#clean up{{{
218rm temp
219#}}}
220#print info {{{
221printf "\r "
222printf "\rdone!\n"
223#}}}
Note: See TracBrowser for help on using the repository browser.