+/*****************************************************************************
+ *Function name
+ * parse_fmt : parses the format string
+ *Input params
+ * fmt : format string specified in the xml file
+ * header : points to the extracted header string
+ * separator : points to the extracted separator string
+ * footer : points to the extracted footer string
+ *
+ * Parses the format string in order to extract the header,
+ * the separator and the footer.
+ * The default fmt string is: { %S, %S }
+ * In this case:
+ * The header is: {
+ * The separator is: ,
+ * The footer
+ *
+ ****************************************************************************/
+
+parse_fmt(char *fmt, char **header, char **separator, char **footer){
+ int i;
+ unsigned int num = 0;
+
+ int header_index = 0;//header index
+ int separator_index = 0;//separator index
+ int footer_index = 0;//footer index
+ int fmt_length = strlen(fmt);
+
+ for(i=0; i<fmt_length; i++)
+ {
+ if(fmt[i]=='%')
+ {
+ num++;
+ if(num>=3)
+ g_error("More than 2 '%%' chars were encountered: %s\n",fmt);
+
+ //Detecting identifier
+ if(fmt[++i]!='S')
+ g_error("Unexpected format: %s\n",fmt);
+
+ if(i+1<strlen(fmt))
+ i = i+1;
+ else
+ g_error("Unexpected format: %s\n",fmt);
+
+ //Need to have at least on char after the %S
+ if(fmt[i]=='%')
+ g_error("Missing separator: %s\n",fmt);
+
+ if(separator_index==0)
+ separator_index = i;//index of separator inside the original string.
+ else if(footer_index==0)//the 'else' gives the priority to set the separator index first.
+ footer_index = i;//no break since an error should be generated if more than 2 '%' were encountered
+ }
+ }
+
+
+ //Create header String
+ num = separator_index-header_index-2;//(-2 due to '%S')
+ if(num<0)
+ g_error("Unexpected format");
+ //*header = malloc(num+1);//(+1 for EOS).
+ *header = g_new(gchar,num+1);//(+1 for EOS).
+ strncpy(*header,fmt,num);
+ (*header)[num] = '\0';//need EOS, not handled by strncopy
+
+ //Create seperator String
+ num =footer_index - separator_index - 2;
+ if(num<0)
+ g_error("Unexpected format");
+ //*separator = malloc(num+1);//+1 for EOS
+ *separator = g_new(gchar, num+1);//+1 for EOS
+ strncpy(*separator,fmt+separator_index*sizeof(char),num);
+ (*separator)[num] = '\0';//need EOS, not handled by strncopy
+
+ //Create footer String
+ num = strlen(fmt)-footer_index;
+ //*footer = malloc(num+1);
+ *footer = g_new(gchar, num+1);
+ strncpy(*footer,fmt+footer_index*sizeof(char),num);
+ (*footer)[num] = '\0';
+}
+
+/*****************************************************************************
+ *Function name
+ * verify_fmt_syntax : verifies the syntax of the format string
+ *Input params
+ * fmt : format string specified in the xml file
+ * fmt_type : points to the detected type in the fmt string
+ *
+ * Verifies the syntax of the format string sepcified in the xml file
+ * It allows fmt strings having the following syntax:
+ * "..header text... %12.20d ...footer text..."
+ * It sets the fmt_type accordingly. In the previous example
+ * fmt_type would point to the character 'd' inside fmt.
+ *
+ *returns 1 on success, and -1 or -2 on error.
+ *
+ ****************************************************************************/
+
+int verify_fmt_syntax(char *fmt, char **fmt_type){
+ int i;
+ int dot = 0;//number of dots encountered.
+ unsigned short counter = 0;
+ *fmt_type=NULL;
+
+
+ for(i=0; i<strlen(fmt); i++)
+ {
+ if(fmt[i]=='%')
+ {
+ if(++counter==2)
+ return -1;//should generate an error.
+
+ i = i+1;//go to next character
+
+ if(fmt[i]=='-' && i<(strlen(fmt)-1))
+ i++;//allow this character after the '%'
+
+ if(fmt[i]=='#' && i<(strlen(fmt)-1))
+ i++;//allow this character
+ }
+
+ if(counter ==1 && !isdigit(fmt[i]) && *fmt_type==NULL)
+ switch (fmt[i]){
+ case 'd':
+ case 'i':
+ case 'u':
+ case 'o':
+ case 'x':
+ case 'X':
+ case 'f':
+ case 'e':
+ case 'E':
+ case 's':
+ case 'p':
+ *fmt_type=&fmt[i];
+ break;
+ //do not return yet. may encounter another '%'
+ case '.':
+ if(++dot==2)
+ return -2;//generate error
+ break;
+ default:
+ return -1;
+ }
+
+ }
+
+ return 1;
+
+}
+
+/*****************************************************************************
+ *Function name
+ * append_ll : appends "ll" to the format string
+ *Input params
+ * fmt : format string specified in the xml file
+ * fmt_type : address of the format char inside fmt
+ *
+ * inserts "ll" just before the fmt_type
+ *
+ ****************************************************************************/
+
+void append_ll (char **fmt, char **fmt_type){
+ char *new_fmt;
+ int i;
+ int num;
+
+
+ //new_fmt = malloc(strlen(*fmt)*sizeof(char)+2);//the +2 corresponds the the "ll";
+ new_fmt = g_new(gchar, strlen(*fmt)+2);
+
+ num = *fmt_type - *fmt;
+
+ for(i=0; i<num;i++)
+ new_fmt[i] =(*fmt)[i];
+
+ new_fmt[i++] = 'l';
+ new_fmt[i++] = 'l';
+
+
+ while(i-2<strlen(*fmt))
+ {
+ new_fmt[i]=(*fmt)[i-2];
+ i++;
+ }
+
+ *fmt = new_fmt;
+ (*fmt)[i] = '\0';
+
+}
+
+