#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int vertNum, /* number of vertexes */
    treeNum; /* number of trees */
    
int ***matVector; /* vector with distance matrixes */

/*------------------------------------------------------------
 Open and read binary file.
--------------------------------------------------------------*/
int ReadFile(const char* fileName)
{
   assert(fileName != NULL && "fileName == NULL ?");
   
   FILE* file = NULL;
   file = fopen(fileName, "r");
   
   if (!file) /* File does not exist ?*/
   {
       return 0;
   }  
   else  /* If OK => read file */
   {
     /* Read the first line */
     fscanf(file, "Vertex num. = %d\tTrees num. = %d\n", &vertNum, &treeNum); 
     
     printf("There are T=%d trees with N=%d nodes\n", treeNum, vertNum);   
     
     /* Calculate number of bytes of record */           
     int recordSize = vertNum * (vertNum - 1) / 2;
               
     /* Allocate memory for the record */          
     int *tempTab = (int*)malloc(recordSize * sizeof(int));
     
     if (!tempTab) return 0;
          
     int count = 0;
     int zn, i, j, k;

     /* Allocate memory for vector of matrixes */
     matVector = (int***)malloc(sizeof(int**) * treeNum);     
     
     if (!matVector) return 0;  
     
     for (i = 0; i < treeNum; ++i)
     {
       matVector[i] = (int**)malloc(sizeof(int*) * vertNum);
       
       if (!matVector[i]) return 0;
     } 
     
     for (i = 0; i < treeNum; ++i)
     {
       for (j = 0; j < vertNum; ++j)
       {  
         matVector[i][j] = (int*)malloc(sizeof(int) * vertNum);
         
         if (!matVector[i][j]) return 0;
       }
     }     
     
     /* Initialize vector */
     for (i = 0; i < treeNum; ++i)
     {
       for (j = 0; j < vertNum; ++j)
       {
         for (k = 0; k < vertNum; ++k)
           matVector[i][j][k] = 0;  
       }    
     }    
     
     i = 0;

     /* Read the file */
     while((zn = getc(file)) != EOF)
     {
       if (count < recordSize)
         tempTab[count++] = (int)zn; 
            
       if (count >= recordSize && i < treeNum)
       {
         count = 0;
              
         unsigned int x = 0;
              
         for (j = 0; j < vertNum - 1; ++j)
         {
           for (k = j + 1; k < vertNum; ++k)
           {
             matVector[i][j][k] = matVector[i][k][j] = tempTab[x++];
           }   
         }  
         ++i;                  
       }  
            
     }/* while (zn = fgetc(file))  */
    
     /* Deallocate resources */
     free(tempTab);
     
     fclose(file);   
   } 
      
   return 1;    
} 

int main(int argc, char *argv[])
{
  printf("Name of binary file: ");  
  
  char fileName[256];    
  scanf("%s", fileName);
  
  int i, j, k, m;
  
  if (ReadFile(fileName))
  {
    char txt_buff[100];
    
    /* convert binary file to .net */
    for (i = 0; i < treeNum; ++i)
    {
      sprintf(txt_buff, "tree_%d_%d.net", vertNum, i + 1);
      
      FILE *file;
      file = fopen(txt_buff, "w");
      
      fprintf(file, "*Vertices %d\n", vertNum);
      
      for (j = 0; j < vertNum; ++j)
      {
        fprintf(file, " %d \"nod%d\"\n", j + 1, j + 1);
      }  
      
      fprintf(file, "*Edges\n");
      
      for (k = 0; k < vertNum - 1; ++k)
      {
        for (m = k + 1; m < vertNum; ++m)
        {
          if (matVector[i][k][m] == 1)
            fprintf(file, " %d %d\n", k + 1, m + 1);
        }   
      }  
      
      fclose(file);
    }      
    
    for (i = 0; i < treeNum; ++i)
      for (j = 0; j < vertNum; ++j)
        free(matVector[i][j]);
    
    for (i = 0; i < treeNum; ++i)
      free(matVector[i]);        
    
    free(matVector);
  }
  else
  {
    printf("I/O error <%s>\n", fileName);
  }    
  
  return 0;
}
