TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

page_config.c

00001  
00002 
00003 #include <libxml/parser.h>
00004 #include <errno.h>
00005 #include <stdio.h>
00006 #include <string.h>
00007 #include <stdlib.h>
00008 #include "page_config.h"
00009 
00010 
00011 /* Don't forget to update PGC_var_type_t  AND var_type_str */
00012 static xmlChar* X_var_type_str[] = { "DOUBLE", "TITLE", "HEXA", "BIN", "STRING", NULL};
00013 static xmlChar* X_widget_type_str[] = { "view", "draw" ,NULL};
00014 
00015 const xmlChar* PGC_LABEL_DOC="page_config";
00016 const xmlChar* PGC_LABEL_TRUE="true";
00017 const xmlChar* PGC_LABEL_FALSE="false";
00018 const xmlChar* PGC_LABEL_TITLE="title";
00019 const xmlChar* PGC_LABEL_X="x";
00020 const xmlChar* PGC_LABEL_Y="y";
00021 const xmlChar* PGC_LABEL_WIDTH="width";
00022 const xmlChar* PGC_LABEL_HEIGHT="height";
00023 const xmlChar* PGC_LABEL_VISIBLE="visible";
00024 const xmlChar* PGC_LABEL_NAME="name";
00025 const xmlChar* PGC_LABEL_TYPE="type";
00026 const xmlChar* PGC_LABEL_LEGEND="legend";
00027 const xmlChar* PGC_LABEL_PERIOD="period";
00028 const xmlChar* PGC_LABEL_DISPLAY_FREQUENCY="display_frequency";
00029 const xmlChar* PGC_LABEL_WIDGET="widget";
00030 const xmlChar* PGC_LABEL_ROWS="rows";
00031 const xmlChar* PGC_LABEL_NO_BORDER="no_border";
00032 const xmlChar* PGC_LABEL_DURATION="duration";
00033 
00034 
00035 struct PGC_instance_t
00036 {
00037   xmlDocPtr doc;
00038   xmlNodePtr root;
00039   xmlNodePtr page_node;
00040   xmlNodePtr var_node;
00041   PGC_error_t last_error;
00042   float display_frequency;
00043 
00044   int global_period;
00045   int page_period;
00046 
00047   float global_duration;
00048   float page_duration;
00049 
00050   PGC_widget_type_t global_widget_type;
00051   PGC_widget_type_t page_widget_type;
00052 
00053   int global_is_visible;
00054 
00055   int global_no_border;
00056 
00057 };
00058 
00059 #define PGC_XMLGETPROP_INT(node, name, value, ret) \
00060 { \
00061       xmlChar* buf = xmlGetProp(node, name); \
00062       if(buf) \
00063         { \
00064           value = atoi(buf); \
00065           xmlFree(buf); \
00066         } \
00067       else \
00068         { \
00069           fprintf(stderr, "unable to find propertie '%s'\n", name); \
00070           return ret; \
00071         } \
00072 }
00073 
00074 #define PGC_XMLGETPROP_INT_OPT(node, name, value, default) \
00075 { \
00076       xmlChar* buf = xmlGetProp(node, name); \
00077       if(buf) \
00078         { \
00079           value = atoi(buf); \
00080           xmlFree(buf); \
00081         } \
00082       else \
00083         { \
00084           value = default; \
00085         } \
00086 }
00087 
00088 
00089 
00090 #define PGC_XMLGETPROP_DOUBLE(node, name, value, ret) \
00091 { \
00092       xmlChar* buf = xmlGetProp(node, name); \
00093       if(buf) \
00094         { \
00095           value = atof(buf); \
00096           xmlFree(buf); \
00097         } \
00098       else \
00099         { \
00100           fprintf(stderr, "unable to find propertie '%s'\n", name); \
00101           return ret; \
00102         } \
00103 }
00104 
00105 
00106 #define PGC_XMLGETPROP_DOUBLE_OPT(node, name, value, default) \
00107 { \
00108       xmlChar* buf = xmlGetProp(node, name); \
00109       if(buf) \
00110         { \
00111           value = atof(buf); \
00112           xmlFree(buf); \
00113         } \
00114       else \
00115         { \
00116           value = default; \
00117         } \
00118 }
00119 
00120 #define PGC_XMLGETPROP_STRING(node, name, value, ret) \
00121 { \
00122       xmlChar* buf = xmlGetProp(node, name); \
00123       if(buf) \
00124         { \
00125           value = strdup(buf); \
00126           xmlFree(buf); \
00127         } \
00128       else \
00129         { \
00130           fprintf(stderr, "unable to find propertie '%s'\n", name); \
00131           return ret; \
00132         } \
00133 }
00134 
00135 #define PGC_XMLGETPROP_STRING_OPT(node, name, value, default) \
00136 { \
00137       xmlChar* buf = xmlGetProp(node, name); \
00138       if(buf) \
00139         { \
00140           value = strdup(buf); \
00141           xmlFree(buf); \
00142         } \
00143       else \
00144         { \
00145           value = default; \
00146         } \
00147 }
00148 
00149 #define PGC_XMLGETPROP_BOOL(node, name, value, ret) \
00150 { \
00151       xmlChar* buf = xmlGetProp(node, name); \
00152       if(buf) \
00153         { \
00154           if(!xmlStrcasecmp(buf, PGC_LABEL_TRUE)) \
00155               { \
00156                 value = PGC_TRUE; \
00157                 xmlFree(buf); \
00158               } \
00159           else if(!xmlStrcasecmp(buf, PGC_LABEL_FALSE)) \
00160              { \
00161                value = PGC_FALSE; \
00162                xmlFree(buf); \
00163              } \
00164           else \
00165              { \
00166                fprintf(stderr, "The '%s' propertie must be 'true' or 'false'\n", name); \
00167                return ret; \
00168              } \
00169         } \
00170       else \
00171         { \
00172           fprintf(stderr, "unable to find propertie '%s'\n", name); \
00173           return ret; \
00174         } \
00175 }
00176 
00177 #define PGC_XMLGETPROP_BOOL_OPT(node, name, value, default, ret) \
00178 { \
00179       xmlChar* buf = xmlGetProp(node, name); \
00180       if(buf) \
00181         { \
00182           if(!xmlStrcasecmp(buf, PGC_LABEL_TRUE)) \
00183               { \
00184                 value = PGC_TRUE; \
00185                 xmlFree(buf); \
00186               } \
00187           else if(!xmlStrcasecmp(buf, PGC_LABEL_FALSE)) \
00188              { \
00189                value = PGC_FALSE; \
00190                xmlFree(buf); \
00191              } \
00192           else \
00193              { \
00194                fprintf(stderr, "The '%s' propertie must be 'true' or 'false'\n", name); \
00195                return ret; \
00196              } \
00197         } \
00198       else \
00199         { \
00200           value = default; \
00201         } \
00202 }
00203 
00204 
00205 #define PGC_XMLGETPROP_VARTYPE(node, name, value, ret) \
00206 { \
00207       xmlChar* buf = xmlGetProp(node, name); \
00208       if(buf) \
00209         { \
00210            if(!var_type_val(buf,&(value))) \
00211            { \
00212               fprintf(stderr, "The '%s' variable type is unknown\n", buf); \
00213               return ret; \
00214            } \
00215         } \
00216       else \
00217         { \
00218           fprintf(stderr, "unable to find propertie '%s'\n", name); \
00219           return ret; \
00220         } \
00221 }
00222 
00223 #define PGC_XMLGETPROP_WIDGETTYPE(node, name, value, ret) \
00224 { \
00225       xmlChar* buf = xmlGetProp(node, name); \
00226       if(buf) \
00227         { \
00228            if(!widget_type_val(buf,&(value))) \
00229            { \
00230               fprintf(stderr, "The '%s' widget type is unknown\n", buf); \
00231               return ret; \
00232            } \
00233         } \
00234       else \
00235         { \
00236           fprintf(stderr, "unable to find propertie '%s'\n", name); \
00237           return ret; \
00238         } \
00239 }
00240 
00241 #define PGC_XMLGETPROP_WIDGETTYPE_OPT(node, name, value, default, ret) \
00242 { \
00243       xmlChar* buf = xmlGetProp(node, name); \
00244       if(buf) \
00245         { \
00246            if(!widget_type_val(buf,&(value))) \
00247            { \
00248               fprintf(stderr, "The '%s' widget type is unknown\n", buf); \
00249               return ret; \
00250            } \
00251         } \
00252       else \
00253         { \
00254            value = default; \
00255         } \
00256 }
00257 
00258 
00259 static int var_type_val (xmlChar *str, PGC_var_type_t* type  ) {
00260   int i;
00261   for (i=0; X_var_type_str[i] != NULL; i++) {
00262     if (!xmlStrcasecmp(str, X_var_type_str[i]))
00263       {
00264         *type = (PGC_var_type_t)i;
00265         return PGC_TRUE;
00266       }
00267   }
00268   return PGC_FALSE;
00269 }
00270 
00271 static int widget_type_val (xmlChar *str, PGC_widget_type_t* type  ) {
00272   int i;
00273   for (i=0; X_widget_type_str[i] != NULL; i++) {
00274     if (!xmlStrcasecmp(str, X_widget_type_str[i]))
00275       {
00276         *type = (PGC_widget_type_t)i;
00277         return PGC_TRUE;
00278       }
00279   }
00280   return PGC_FALSE;
00281 }
00282 
00283 
00284 
00285 
00286 /*void PGC_close(PGC_handle_t* h)
00287 {
00288   
00289 }*/
00290 
00291 PGC_handle_t PGC_open_file(char* filename)
00292 {
00293   PGC_instance_t* h;
00294   h = (PGC_instance_t*)calloc(1, sizeof(PGC_instance_t));
00295   if(h)
00296     {
00297       h->last_error = PGC_STATUS_OK;
00298       h->doc = xmlParseFile(filename);     
00299       if (h->doc) 
00300         {
00301           /* root might not be h->doc->children */
00302           h->root = xmlDocGetRootElement(h->doc);
00303           /* Try to avoid simple error cases */
00304           if (h->root &&
00305               h->root->name &&
00306               !xmlStrcmp(h->root->name, PGC_LABEL_DOC) ) 
00307             {
00308               /* Read properties */
00309               PGC_XMLGETPROP_INT(h->root, PGC_LABEL_PERIOD, h->global_period, NULL);
00310               PGC_XMLGETPROP_DOUBLE(h->root, PGC_LABEL_DISPLAY_FREQUENCY, h->display_frequency, NULL);    
00311               PGC_XMLGETPROP_DOUBLE(h->root, PGC_LABEL_DURATION, h->global_duration, NULL);       
00312               PGC_XMLGETPROP_WIDGETTYPE(h->root, PGC_LABEL_WIDGET, h->global_widget_type, NULL);
00313               PGC_XMLGETPROP_BOOL(h->root, PGC_LABEL_VISIBLE, h->global_is_visible, NULL);
00314               PGC_XMLGETPROP_BOOL(h->root, PGC_LABEL_NO_BORDER, h->global_no_border, NULL);
00315               /* Set first page */
00316               h->page_node = h->root->children;
00317             }
00318           else
00319             {
00320               fprintf(stderr, "File %s data is corrupted, or not a %s file, unable to parse\n", PGC_LABEL_DOC, filename);
00321               xmlFreeDoc(h->doc);
00322               free(h); h = NULL;
00323           }
00324         }
00325       else
00326         {
00327           fprintf(stderr, "Cannot allocate a new xml document\n");
00328           free(h); h = NULL;
00329         }
00330     }
00331   else
00332     {
00333       fprintf(stderr, "memory allocation failed\n");
00334     }
00335 
00336   return h;
00337 }
00338 
00339 int PGC_get_global(PGC_handle_t h, PGC_global_t* global)
00340 {
00341   global->display_frequency = h->display_frequency;
00342 }
00343 
00344 int PGC_get_page_nb_var(PGC_handle_t h, int page)
00345 {
00346   xmlNodePtr page_node = h->root->children;
00347   int current_page = 0;
00348   int nb_var = 0;
00349 
00350   h->last_error = PGC_STATUS_OK;
00351     
00352   /* Iterate till next page */
00353   while( page_node  )
00354     {
00355       if(!xmlIsBlankNode(page_node))
00356         {
00357           if(current_page == page)
00358             {
00359               xmlNodePtr var_node = page_node->children;
00360               while( var_node )
00361                 {
00362                   if(!xmlIsBlankNode(var_node))
00363                     {
00364                       nb_var++;
00365                     }
00366                   var_node = var_node->next;
00367                 }
00368               break;
00369             }
00370           current_page++;
00371         }
00372       page_node = page_node->next;
00373     }
00374   return nb_var;
00375 
00376 }
00377 
00378 int PGC_get_nb_page(PGC_handle_t h)
00379 {
00380   
00381   xmlNodePtr page_node = h->root->children;
00382   int nb_page = 0;
00383 
00384   h->last_error = PGC_STATUS_OK;
00385     
00386   /* Iterate till next page */
00387   while( page_node  )
00388     {
00389       if(!xmlIsBlankNode(page_node))
00390         {
00391           nb_page++;
00392         }
00393       page_node = page_node->next;
00394     }
00395   return nb_page;
00396 }
00397 
00398 int PGC_get_nb_var(PGC_handle_t h)
00399 {
00400 
00401   xmlNodePtr page_node = h->root->children;
00402   xmlNodePtr var_node = NULL;
00403   int nb_var = 0;
00404 
00405   h->last_error = PGC_STATUS_OK;
00406 
00407   /* Iterate till next page */
00408   while( page_node  )
00409     {
00410       if(!xmlIsBlankNode(page_node))
00411         {
00412           var_node = page_node->children;
00413           while( var_node )
00414             {
00415               if(!xmlIsBlankNode(var_node))
00416                 {
00417                   nb_var++;
00418                 }
00419               var_node = var_node->next;
00420             }
00421         }
00422       page_node = page_node->next;
00423     }
00424   return nb_var;
00425 }
00426 
00427 int PGC_get_next_page(PGC_handle_t h, PGC_page_t* page)
00428 { 
00429 
00430    h->last_error = PGC_STATUS_FATAL;
00431    
00432   /* Iterate till next page */
00433   while( h->page_node && xmlIsBlankNode(h->page_node) )
00434     {
00435       h->page_node = h->page_node->next;
00436     }
00437 
00438   if(h->page_node)
00439     {
00440       PGC_XMLGETPROP_STRING(h->page_node, PGC_LABEL_TITLE, page->title, PGC_FALSE);
00441       PGC_XMLGETPROP_INT(h->page_node, PGC_LABEL_X, page->x, PGC_FALSE);
00442       PGC_XMLGETPROP_INT(h->page_node, PGC_LABEL_Y, page->y, PGC_FALSE);
00443       PGC_XMLGETPROP_INT(h->page_node, PGC_LABEL_WIDTH, page->width, PGC_FALSE);
00444       PGC_XMLGETPROP_INT(h->page_node, PGC_LABEL_HEIGHT, page->height, PGC_FALSE);       
00445       PGC_XMLGETPROP_BOOL_OPT(h->page_node, PGC_LABEL_VISIBLE, page->is_visible, h->global_is_visible, PGC_FALSE);      
00446       /* global period is used if page period is not found */
00447       PGC_XMLGETPROP_INT_OPT(h->page_node, PGC_LABEL_PERIOD, h->page_period, h->global_period);  
00448       /* global no_border is used if page no_border is not found */
00449       PGC_XMLGETPROP_BOOL_OPT(h->page_node, PGC_LABEL_NO_BORDER, page->no_border, h->global_no_border, PGC_FALSE);      
00450       /* global widget_type is used if page widget_type not found */
00451       PGC_XMLGETPROP_WIDGETTYPE_OPT(h->page_node, PGC_LABEL_WIDGET, h->page_widget_type, h->global_widget_type, PGC_FALSE);       /* global duration is used if page widget_type not found */
00452       PGC_XMLGETPROP_DOUBLE_OPT(h->page_node, PGC_LABEL_DURATION, h->page_duration, h->global_duration);          
00453 
00454       /* Default value is 0 if rows not found */
00455       PGC_XMLGETPROP_INT_OPT(h->page_node, PGC_LABEL_ROWS, page->rows, 0);  
00456       h->var_node = h->page_node->children;
00457       h->page_node = h->page_node->next;
00458       h->last_error = PGC_STATUS_OK;
00459       return PGC_TRUE;
00460     }
00461   else
00462     {
00463       h->last_error = PGC_STATUS_NO_ITEM;
00464       return PGC_FALSE;
00465     }
00466  
00467 }
00468 
00469 int PGC_get_next_var(PGC_handle_t h, PGC_var_t* var)
00470 {
00471 
00472   h->last_error = PGC_STATUS_FATAL;
00473 
00474   /* Iterate till next var */
00475   while( h->var_node && xmlIsBlankNode(h->var_node) )
00476     {
00477       h->var_node = h->var_node->next;
00478     }
00479 
00480   if(h->var_node)
00481     {
00482       PGC_XMLGETPROP_STRING(h->var_node, PGC_LABEL_NAME, var->name, PGC_FALSE);
00483       PGC_XMLGETPROP_VARTYPE(h->var_node, PGC_LABEL_TYPE, var->type, PGC_FALSE);
00484       PGC_XMLGETPROP_STRING_OPT(h->var_node, PGC_LABEL_LEGEND, var->legend, NULL);
00485       /* page period is used if variable period is not found */
00486       PGC_XMLGETPROP_INT_OPT(h->var_node, PGC_LABEL_PERIOD, var->period, h->page_period);  
00487       /* page widget_type is used if variable widget_type not found */
00488       PGC_XMLGETPROP_WIDGETTYPE_OPT(h->var_node, PGC_LABEL_WIDGET, var->widget_type, h->page_widget_type, PGC_FALSE);
00489       /* page period is used if variable period is not found */
00490       PGC_XMLGETPROP_DOUBLE_OPT(h->var_node, PGC_LABEL_DURATION, var->duration, h->page_duration);  
00491       /* For a title the widget is always a view */
00492       if(PGC_TITLE == var->type) 
00493         {
00494           var->widget_type = PGC_WIDGET_VIEW;
00495         }
00496       h->var_node = h->var_node->next;
00497       h->last_error = PGC_STATUS_OK;
00498       return PGC_TRUE;
00499     }
00500   else 
00501     {
00502       h->last_error = PGC_STATUS_NO_ITEM;
00503       return PGC_FALSE;
00504     }  
00505 
00506  
00507 }
00508 
00509 PGC_error_t PGC_get_last_error(PGC_handle_t h)
00510 {
00511   return h->last_error;
00512 }
00513 
00514 
Framework Home Page.

Beware !! TSP wave is coming...