home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / src / io / description / dist-varstrip-parser.c < prev    next >
C/C++ Source or Header  |  2008-06-30  |  5KB  |  219 lines

  1. /*
  2.  * (C) 2005 Frederik Grll <frederik.gruell@web.de>
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */
  6.  
  7.  
  8. #include "dist-varstrip-parser.h"
  9. #include "pvfs2-dist-varstrip.h"
  10. #include "gossip.h"
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <errno.h>
  16.  
  17. static int strips_parse_elem(char* inp, const PVFS_offset *prev_offset,
  18.                              const PVFS_size *prev_size, unsigned *server_nr,
  19.                              PVFS_offset *offset, PVFS_size *size);
  20.  
  21. static PINT_dist_strips* strips_alloc_mem(const char* inp);
  22.  
  23. void PINT_dist_strips_free_mem(PINT_dist_strips **strips)
  24. {
  25.     if (*strips)
  26.     {
  27.         free(*strips);
  28.         *strips = 0;
  29.     }
  30.     return;
  31. }
  32.  
  33.  
  34. static int strips_parse_elem(
  35.     char* inp, const PVFS_offset *prev_offset,
  36.     const PVFS_size *prev_size, unsigned *server_nr,
  37.     PVFS_offset *offset, PVFS_size *size)
  38. {
  39.     char *s_server, *s_size;
  40.     unsigned i_server;
  41.     PVFS_size i_size;
  42.  
  43.     if (prev_offset != NULL && prev_size != NULL) 
  44.     {
  45.         s_server = strtok(NULL, ":");
  46.     }
  47.     else
  48.     {
  49.         s_server = strtok(inp, ":");
  50.     }
  51.  
  52.     if (s_server != NULL)
  53.     {
  54.         i_server = atoi(s_server);
  55.         *server_nr = i_server;
  56.     }
  57.     else
  58.     {
  59.         return 1;
  60.     }
  61.  
  62.     if (prev_offset != NULL && prev_size != NULL) 
  63.     {
  64.         *offset = (*prev_offset) + (*prev_size);
  65.     }
  66.     else
  67.     {
  68.         *offset = 0;
  69.     }
  70.  
  71.     s_size = strtok(NULL, ";");
  72.     if (s_size != NULL)
  73.     {
  74.         i_size = atoll(s_size);
  75.         if (i_size > 0)
  76.         {
  77.             if (strlen(s_size) > 1)
  78.             {
  79.                 switch (s_size[strlen(s_size) - 1])
  80.                 {
  81.                     case 'k':
  82.                     case 'K':
  83.                         i_size *= 1024;
  84.                         break;
  85.                     case 'm':
  86.                     case 'M':
  87.                         i_size *= (1024 * 1024);
  88.                         break;
  89.                     case 'g':
  90.                     case 'G':
  91.                         i_size *= (1024 * 1024 * 1024);
  92.                         break;
  93.                 }
  94.             }
  95.             *size = i_size;
  96.         }
  97.         else
  98.         {
  99.             return -1;
  100.         }
  101.     }
  102.     else
  103.     {
  104.         return -1;
  105.     }
  106.  
  107.     return 0;
  108. }
  109.  
  110.  
  111. static PINT_dist_strips* strips_alloc_mem(const char* inp)
  112. {
  113.     int i, count = 0;
  114.     /* count ":" to allocate enough memory */
  115.     for (i = 0; i < strlen(inp); i++)
  116.     {
  117.         if (inp[i] == ':')
  118.         {
  119.             count++;
  120.         }
  121.     }  
  122.  
  123.     if (!count)
  124.     {
  125.         /* no ";" found, abort */
  126.         return (PINT_dist_strips*) NULL;
  127.     }
  128.  
  129.     /* allocate array of struct slicing */
  130.     return (PINT_dist_strips*) (malloc(sizeof(PINT_dist_strips) * count));
  131. }
  132.  
  133. /*
  134.  * parse hint to array of struct slicing
  135.  * input sytax: {<datafile number>:<strip size>[K|M|G];}+
  136.  */
  137. int PINT_dist_strips_parse(
  138.     const char *input, PINT_dist_strips **strips, unsigned *count)  
  139. {
  140.     char inp[PVFS_DIST_VARSTRIP_MAX_STRIPS_STRING_LENGTH];
  141.     PINT_dist_strips *strips_elem;
  142.     PVFS_size *prev_size   = NULL;
  143.     PVFS_offset *prev_offset = NULL;
  144.     int i;
  145.  
  146.     *count = 0;
  147.     *strips = 0;
  148.  
  149.     if(!input || strlen(input) == 0)
  150.     {
  151.         gossip_err("Error: missing manditory parameters to varstrip_dist distribution.\n");
  152.         return(-1);
  153.     }
  154.  
  155.     if (strlen(input) < PVFS_DIST_VARSTRIP_MAX_STRIPS_STRING_LENGTH - 1)
  156.     {
  157.         strcpy(inp, input);
  158.     }
  159.     else
  160.     {
  161.         /* input string too long, abort */
  162.         gossip_err("Error: varstrip_dist distribution parameters too long.\n");
  163.         return -1;
  164.     }
  165.  
  166.     *strips = strips_alloc_mem(inp);
  167.  
  168.     if (!(*strips))        
  169.     {
  170.         /* allocation failed, abort */
  171.         gossip_err("Error: unable to parse varstrip_dist distribution parameters.\n");
  172.         return -1;
  173.     }
  174.  
  175.     for (i = 0;; i++)
  176.     {
  177.         strips_elem = (*strips) + i;
  178.         switch (
  179.             strips_parse_elem(
  180.                 inp, prev_offset, prev_size, &(strips_elem->server_nr),
  181.                 &(strips_elem->offset), &(strips_elem->size)))
  182.         {
  183.             case 0:     
  184.                 /* do next element */
  185.                 prev_offset = &(strips_elem->offset);
  186.                 prev_size   = &(strips_elem->size);
  187.                 break;
  188.             case -1:
  189.                 /* an error occured */
  190.                 PINT_dist_strips_free_mem(strips);
  191.                 *count = 0;
  192.                 return -1;
  193.             case 1:
  194.                 /* finished */
  195.                 *count = i;
  196.                 if (*count == 0)    
  197.                 {         
  198.                     /* 0 elements, abort */
  199.                     PINT_dist_strips_free_mem(strips);
  200.                     return -1;
  201.                 }
  202.                 else
  203.                 {
  204.                     return 0;
  205.                 }
  206.         }
  207.     }
  208. }
  209.  
  210. /*
  211.  * Local variables:
  212.  *  mode: c
  213.  *  c-indent-level: 4
  214.  *  c-basic-offset: 4
  215.  * End:
  216.  *
  217.  * vim: ft=c ts=8 sts=4 sw=4 expandtab
  218.  */
  219.