We'll start out by defining the internal form for an FCUR value. As I mentioned before, you want to store three pieces of information for each value: the name of the currency (dollars, euros, yen, and so on), the number of units, and the exchange rate at the time the value was created. Why do you need to store the exchange rate with each value? Because exchange rates vary over time, and you need to know the rate at the time the value is created. Because you are going to use the C programming language to implement the required conversion functions, you need to define a structure [4] containing the three components . Here are the first few lines of the implementation file:
1 /* 2 ** File name: fcur.c 3 */ 4 5 #include "postgres.h" 6 #include "fmgr.h" 7 8 typedef struct 9 { 10 char fcur_name[4]; /* Currency name */ 11 float4 fcur_units; /* Units of currency */ 12 float4 fcur_xrate; /* Exchange rate */ 13 } fcur; 14 15 static char * baseCurrencyName = "US$"; 16 static char * unknownCurrencyName = "???"; 17 Start by #including the postgres.h and fmgr.h header files, just like you did for the earlier examples. The fcur structure defines the internal form for your fcur data type. Store the currency name ( fcur_name ) as a three- character, null- terminated string. The fcur_units member store the number of currency units as a floating-point number. The exchange rate is stored as a floating-point number in fcur_xrate . At lines 15 and 16, you define two currency names . The baseCurrencyName is the name of the local currency. When the fcur_name of a value is equal to baseCurrencyName , the value is said to be normalized . A normalized value will always have an exchange rate ( fcur_xrate ) of 1.0 : One U.S. dollar always equals one U.S. dollar. The unknownCurrencyName is used when the user enters a value containing a number of units and an exchange rate, but fails to provide the currency name. We'll use each of these variables in a moment. |