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

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

merged trunk-jpl and trunk for revision 16135

  • Property svn:executable set to *
File size: 4.6 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){
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#Build EnumDefinitions.py{{{
104cat <<END > $ISSM_DIR/src/m/enum/EnumDefinitions.py
105from StringToEnum import StringToEnum
106
107"""
108
109WARNING: DO NOT MODIFY THIS FILE
110this file has been automatically generated by src/c/shared/Enum/Synchronize.sh
111Please read src/c/shared/Enum/README for more information
112
113"""
114
115END
116#core
117cat temp | awk '{print "def " $1"(): return StringToEnum(\"" substr($1,1,length($1)-4) "\")[0]"}' >> $ISSM_DIR/src/m/enum/EnumDefinitions.py
118#}}}
119
120# go through the lines of temp
121ENUM=0;
122
123for NAMEENUM in $(cat temp); do
124
125 #Get name and enum of the line i
126 NAME=$(echo $NAMEENUM | sed -e "s/Enum//g")
127 #offset Enum by one (Enum begins with 0 and not 1!)
128 let ENUM=$ENUM+1
129
130 #print info {{{
131 if [ $ENUM -lt 10 ]
132 then
133 printf "\r "
134 printf "\r $ENUM/$NUMENUMS Adding "$NAME"..."
135 else
136 if [ $ENUM -lt 100 ]
137 then
138 printf "\r "
139 printf "\r $ENUM/$NUMENUMS Adding "$NAME"..."
140 else
141 printf "\r "
142 printf "\r$ENUM/$NUMENUMS Adding "$NAME"..."
143 fi
144 fi
145 #}}}
146 #Add case to matlabenum file{{{
147 cat <<END > $ISSM_DIR"/src/m/enum/"$(echo $NAMEENUM".m")
148function macro=$(echo $NAMEENUM)()
149%$(echo $NAMEENUM | awk {'print toupper($1)'}) - Enum of $(echo $NAME)
150%
151% WARNING: DO NOT MODIFY THIS FILE
152% this file has been automatically generated by src/c/shared/Enum/Synchronize.sh
153% Please read src/c/shared/Enum/README for more information
154%
155% Usage:
156% macro=$NAMEENUM()
157
158macro=StringToEnum('$NAME');
159END
160#}}}
161
162done
163#clean up{{{
164rm temp
165#}}}
166#print info {{{
167printf "\r "
168printf "\rdone!\n"
169#}}}
Note: See TracBrowser for help on using the repository browser.