Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Libreria

items.c

Caricato da: AldoBaldo
Scarica il programma completo

  1. /**=============================================================================
  2. Libreria "items" - versione 1.0.1 - di Aldo Carpanelli, 2/1/2015=>20/7/2017
  3. "items" library - version 1.0.1 - by Aldo Carpanelli, 1/2/2015=>7/20/2017
  4. =============================================================================**/
  5.  
  6. /*==============================================================================
  7. Per una qualche specie di documentazione is veda il file info_ita.pdf .
  8. For some sort of reference material, have a look at the info_en.pdf file.
  9. ==============================================================================*/
  10.  
  11. #include "items.h"
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16.  
  17. long CountItemsInString( const char *data, int sep ) {
  18.     long n = -1;
  19.  
  20.     if( data )
  21.         for( n=1; *data; ++data )
  22.             *data==sep ? ++n : 0;
  23.  
  24.     return n;
  25. }
  26.  
  27.  
  28. long CountItemsInFile( const char *fileName, int sep ) {
  29.     FILE *f;
  30.     long n;
  31.     int c;
  32.  
  33.     if( fileName == NULL ) return -1;
  34.     if( *fileName == '\0' ) return -1;
  35.  
  36.     f = fopen( fileName, "r" );
  37.     if( f == NULL ) return -1;
  38.  
  39.     for( n=1, c=fgetc(f); c!=EOF; c=fgetc(f) )
  40.         if( c == sep ) ++n;
  41.  
  42.     fclose( f );
  43.     return n;
  44. }
  45.  
  46.  
  47. long TokeniseString( char *data, int sep, const char ***ptrs ) {
  48.     long totItems = CountItemsInString( data, sep );
  49.  
  50.     if( totItems > -1 ) {
  51.         long i;   /* tap = [t]emporary [a]rray of [p]ointers */
  52.         const char **tap = calloc( totItems, sizeof(char*) );
  53.  
  54.         for( i=0; i<totItems; ++i ) {
  55.             tap[i] = data;
  56.             while( *data && *data != sep ) ++data;
  57.             *data++ = '\0';
  58.         }
  59.  
  60.         *ptrs = tap;
  61.     }
  62.  
  63.     return totItems;
  64. }
  65.  
  66.  
  67. long GetAllItemsFromString( const char *data, int sep, const char ***ptrs ) {
  68.     long totItems = -1;
  69.     char *copy;
  70.  
  71.     copy = AllocateCopy( data, -1 );
  72.     if( copy == NULL ) return -1;
  73.  
  74.     totItems = TokeniseString( copy, sep, ptrs );
  75.     if( totItems == -1 ) free( copy );
  76.  
  77.     return totItems;
  78. }
  79.  
  80.  
  81. long GetAllItemsFromFile( const char *fileName, int sep, const char ***ptrs ) {
  82.     long totItems = 0;
  83.     char *data = NULL;
  84.  
  85.     if( fileName == NULL ) return -1;
  86.     if( *fileName == '\0' ) return -1;
  87.  
  88.     data = LoadFile( fileName );
  89.         if( data == NULL ) return -1;
  90.  
  91.     totItems = TokeniseString( data, sep, ptrs );
  92.     if( totItems == -1 ) free( data );
  93.  
  94.     return totItems;
  95. }
  96.  
  97.  
  98. char *GetItemFromString( const char *data, int sep, long nItem ) {
  99.     long ci; /* [c]urrent [i]tem */
  100.     char *strItem = NULL;
  101.     char sp[2] = { sep, '\0' };
  102.  
  103.     if( data == NULL ) return strItem;
  104.  
  105.     for( ci=0;
  106.          (ci<nItem)&&(data=strchr(data,sep));
  107.          ++data, ++ci );
  108.  
  109.     if( ci == nItem )
  110.         strItem = AllocateCopy( data, strcspn(data,sp) );
  111.  
  112.     return strItem;
  113. }
  114.  
  115.  
  116. char *GetItemFromFile( const char *fileName, int sep, long nItem ) {
  117.     long i, start, length, c, ci; /* [c]urrent [i]tem */
  118.         char *strItem = NULL;
  119.     FILE *f;
  120.  
  121.     if( fileName == NULL ) return strItem;
  122.     if( *fileName == '\0' ) return strItem;
  123.  
  124.     f = fopen( fileName, "r" );
  125.     if( f == NULL ) return strItem;
  126.  
  127.     for( ci=0, c=fgetc(f); ci!=nItem && c!=EOF; c=fgetc(f) )
  128.         if( c == sep ) ++ci;
  129.  
  130.     if( ci == nItem ) {
  131.         start = ftell(f)-1;
  132.         for( c=fgetc(f); c!=sep&&c!=EOF; c=fgetc(f) );
  133.         length = ftell(f)-1-start;
  134.  
  135.         if( (strItem=malloc(length+1)) != NULL ) {
  136.             fseek( f, start, SEEK_SET );
  137.             for( i=0; i<length; *(strItem+i)=fgetc(f), ++i );
  138.             *(strItem+i) = '\0';
  139.         }
  140.     }
  141.  
  142.     fclose( f );
  143.     return strItem;
  144. }
  145.  
  146.  
  147. char *LoadFile( const char *fileName ) {
  148.     char *auxPtr, *data = NULL;
  149.     long dataDim;
  150.     int c;
  151.     FILE *f;
  152.  
  153.     if( fileName == NULL ) return data;
  154.     if( *fileName == '\0' ) return data;
  155.  
  156.     f = fopen( fileName, "r" );
  157.     if( f == NULL ) return data;
  158.  
  159.     for( dataDim=0; fgetc(f)!=EOF; ++dataDim );
  160.  
  161.     data = malloc( dataDim+1 );
  162.  
  163.     if( data != NULL ) {
  164.         rewind( f );
  165.         auxPtr = data;
  166.  
  167.         while( (c=fgetc(f))!=EOF )
  168.             *auxPtr++ = c;
  169.         *auxPtr = '\0';
  170.     }
  171.  
  172.     fclose( f );
  173.     return data;
  174. }
  175.  
  176.  
  177. char *AllocateCopy( const char *s, long l ) {
  178.     long sl; // [s]tring [l]ength
  179.     char *copy = NULL;
  180.  
  181.     if( s == NULL ) return copy;
  182.  
  183.     sl = l<0 ? strlen(s) : l;
  184.  
  185.     if( (copy=malloc(sl+1)) != NULL )
  186.         { memcpy(copy,s,sl); copy[sl]='\0'; }
  187.  
  188.     return copy;
  189. }
  190.  
  191.  
  192. void FreeStringMemory( char **s ) {
  193.     if( *s!= NULL ) {
  194.         free( (void*)*s );
  195.         *s = NULL;
  196.     }
  197. }
  198.  
  199.  
  200. void FreeStringsMemory( const char ***ptrs ) {
  201.     if( ptrs == NULL ) return;
  202.     if( *ptrs == NULL ) return;
  203.  
  204.     if( **ptrs != NULL ) {
  205.         free( (void*)(**ptrs) );
  206.         **ptrs = NULL;
  207.     }
  208.  
  209.     free( (void*)(*ptrs) );
  210.     (*ptrs) = NULL;
  211. }