Conversion d'un fichier de données de V1 vers V2

// conversion d'un fichier gesbanq pc v1 en v2

#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <ctype.h>
#define FICHIER "b:\\gesbanq.v2\\compte\\livret.cp"
#define FICHIER_des "b:\\gesbanq.v2\\compte\\livret.cp2"

long centimes (char* montant)
// convertit la chaine en nombre et conversion francs -> centimes
// retourne 0 si erreur
{
int i=0;
int signe=+1;
long valeur=0;
int decimal=0;

  while (montant[i] == ' ') i++; // saute les espaces de fin
  if (montant[i] == '-') {
    signe = -1;
    i++;
  }
  else
    if (montant[i] == '+')
      i++;
  while (isdigit(montant[i])) {
    valeur = valeur * 10 + montant[i++] - '0';
  }
  if (montant[i] == '.') { // on passe aux decimales
    i++;
    while ((isdigit(montant[i])) && (decimal < 2)) {
      valeur = valeur * 10 + montant[i++] - '0';
      decimal++;
    }
  }
  while (decimal++ < 2)
    valeur = valeur * 10;
  if ((montant[i] == 0) // fin
    || (isdigit(montant[i])) // ou decimal en trop
    || (montant[i] == ' '))  // fin normale
    return valeur*signe;
  else
    return 0;
}


// v2
#define LONG_ENR0_v2 65
#define LONG_ENR_v2 71
#define LONG_VISU_v2 80

void compacte_v2 (char *chaine)
// 11140
// a$ -> chaine
{
char tampon[LONG_ENR_v2+1];

  strncpy (tampon, chaine, 4);         // numero d'enregistrement
  strncpy (tampon+4, chaine+5, 5);      // date op
  strncpy (tampon+9, chaine+11, 8);      // type op
  strncpy (tampon+17, chaine+20, 25);    // objet
  strncpy (tampon+42, chaine+46, 5);     // credit francs
  strncpy (tampon+47, chaine+52, 2);    // credit centimes
  strncpy (tampon+49, chaine+55, 6);    // debit francs
  strncpy (tampon+55, chaine+62, 2);    // centimes
  strncpy (tampon+57, chaine+65, 9);     // code compta
  if ( strlen (chaine) == LONG_VISU_v2 ) {
    strncpy(tampon+66, chaine+75, 5);
    tampon[71] = 0;
  }
  else
    tampon [66] = 0;
  strcpy (chaine, tampon);
}

void decompacte_v2 (char *chaine)
// 11120
// a$ ->chaine
{
char tampon[LONG_VISU_v2+1];

  strncpy (tampon, chaine, 4);              // numero d'enregistrement
  tampon [4] = ' ';
  strncpy (tampon+5, chaine+4, 5);         // date op
  tampon [10] = ' ';
  strncpy (tampon+11, chaine+9, 8);         // type op
  tampon [19] = ' ';
  strncpy (tampon+20, chaine+17, 25);      // objet
  tampon [45] = ' ';
  strncpy (tampon+46, chaine+42, 5);       // credit francs
  tampon [51] = '.';
  strncpy (tampon+52, chaine+47, 2);      // credit centimes
  tampon [54] = ' ';
  strncpy (tampon+55, chaine+49, 6);       // debit francs
  tampon [61] = '.';
  strncpy (tampon+62, chaine+55, 2);      // debit centimes
  tampon [64] = ' ';
  strncpy (tampon+65, chaine+57, 9);      // code compta
  if ( strlen (chaine) == LONG_ENR_v2 ) {
    tampon [74] = ' ';
    strncpy (tampon+75, chaine+66, 5);    // date marquage
    tampon [80] = 0;
  }
  else
    tampon [74] = 0;
  strcpy (chaine, tampon);
}



void Lecture_ligne_v2 (char* nom, int numero, char* ligne)
// 10300 -> 10310
// A$ = ligne
// FI$ = nom
// I= numero
{
FILE *fichier;

  fichier = fopen (nom, "rb");
  if (numero == 0) {
    fseek (fichier, 0, SEEK_SET);
    fgets (ligne, LONG_ENR0_v2+1, fichier);
  }
  else {
    fseek (fichier, ((long) numero - 1) * (LONG_ENR_v2 + 1)
              + LONG_ENR0_v2 + 1, SEEK_SET);
    fgets (ligne, LONG_ENR_v2+1, fichier);
    decompacte_v2 (ligne);
  }
  fclose (fichier);
}

void Ecriture_ligne_v2 (char *nom, int numero, char* ligne)
// 10250->10280
// A$ = ligne
// FI$ = nom
// I= numero
{
FILE *fichier;

  fichier = fopen (nom, "r+");
  if (numero != 0) {
    compacte_v2 (ligne);
    fseek (fichier, ((long) numero - 1) * (LONG_ENR_v2 + 1)
              + LONG_ENR0_v2 + 1, SEEK_SET);
    fputs (ligne, fichier );
    if (strlen (ligne) < LONG_ENR_v2)
      fputc ('\n', fichier);
  }
  else {
    fseek (fichier, 0, SEEK_SET);
    fputs (ligne, fichier );
  }
  fclose (fichier);
}


// v1
#define LONG_ENR_v1 73
#define LONG_VISU_v1 80


void compacte_v1 (char *chaine)
// 11140
// a$ -> chaine
{
char tampon[LONG_ENR_v1+1];

  strncpy (tampon, chaine, 4);
  strncpy (tampon+4, chaine+5, 5);
  strncpy (tampon+9, chaine+11, 8);
  strncpy (tampon+17, chaine+20, 25);
  strncpy (tampon+42, chaine+46, 8);
  strncpy (tampon+50, chaine+55, 9);
  strncpy (tampon+59, chaine+65, 9);
  if ( strlen (chaine) == LONG_VISU_v1 ) {
    strncpy(tampon+68, chaine+75, 5);
    tampon[73] = 0;
  }
  else
    tampon [68] = 0;
  strcpy (chaine, tampon);
}

void decompacte_v1 (char *chaine)
// 11120
// a$ ->chaine
{
char tampon[LONG_VISU_v1+1];

  strncpy (tampon, chaine, 4);
  tampon [4] = ' ';
  strncpy (tampon+5, chaine+4, 5);
  tampon [10] = ' ';
  strncpy (tampon+11, chaine+9, 8);
  tampon [19] = ' ';
  strncpy (tampon+20, chaine+17, 25);
  tampon [45] = ' ';
  strncpy (tampon+46, chaine+42, 8);
  tampon [54] = ' ';
  strncpy (tampon+55, chaine+50, 9);
  tampon [64] = ' ';
  strncpy (tampon+65, chaine+59, 9);
  if ( strlen (chaine) == LONG_ENR_v1 ) {
    tampon [74] = ' ';
    strncpy (tampon+75, chaine+68, 5);
    tampon [80] = 0;
  }
  else
    tampon [74] = 0;
  strcpy (chaine, tampon);
}



void Lecture_ligne_v1 (char* nom, int numero, char* ligne)
// 10300 -> 10310
// A$ = ligne
// FI$ = nom
// I= numero
{
FILE *fichier;

  fichier = fopen (nom, "rb");
  fseek (fichier, ((long) numero) * (LONG_ENR_v1 + 1), SEEK_SET);
  fgets (ligne, LONG_ENR_v1+1, fichier);
  fclose (fichier);
  if ( numero != 0 )
    decompacte_v1 (ligne);
}

void Ecriture_ligne_v1 (char *nom, int numero, char* ligne)
// 10250->10280
// A$ = ligne
// FI$ = nom
// I= numero
{
FILE *fichier;

  if (numero != 0)
    compacte_v1 (ligne);
  fichier = fopen (nom, "r+");
  fseek (fichier, ((long) numero) * (LONG_ENR_v1 + 1), SEEK_SET);
  fputs (ligne, fichier );
  if (strlen (ligne) < LONG_ENR_v1)
    fputc ('\n', fichier);
  fclose (fichier);
}

// conversion
main (void)
{
char nom[]=FICHIER;
char nomdes[]=FICHIER_des;
char ligne[LONG_VISU_v1+1];
char ligne_des[LONG_VISU_v2+1];
char tampon[LONG_VISU_v2+1];
int suivant,i;
long val;

  Lecture_ligne_v1 (nom, 0, ligne);
  strncpy (ligne_des,ligne,21);
  sprintf(tampon,"%.4s",ligne+17);
  suivant = atoi (tampon);
  sprintf(tampon,"%.13s",ligne+21);  // solde banque
  val = centimes(tampon);
  sprintf(ligne_des+21,"%11li",val);
  sprintf(tampon,"%.13s",ligne+34);  // solde reel
  val = centimes(tampon);
  sprintf(ligne_des+32,"%11li",val);
  sprintf(tampon,"%.13s",ligne+47);  // solde banque init
  val = centimes(tampon);
  sprintf(ligne_des+43,"%11li",val);
  sprintf(tampon,"%.13s",ligne+60);  // solde reel init
  val = centimes(tampon);
  sprintf(ligne_des+54,"%11li",val);
  Ecriture_ligne_v2(nomdes,0,ligne_des);

  for (i=1;i<suivant;i++) {
    Lecture_ligne_v1(nom,i,ligne);
    Ecriture_ligne_v2(nomdes,i,ligne);
  }
  return 0;
}