// Bibliothèque de grandzebu.net

void DisplayInt(int Value) {
  // V 1.0
  // Affiche un entier / Display an integer
  // Nécessite : IntToString / Need : IntToString
  char Chaine[64];
  MessageBox(NULL, IntToString(Value, Chaine), "Valeur", MB_OK);
}

void DoEvents() {
  // V 1.0
  // Redonne la main au système pour traiter les messages
  // Give again the hand to the system to treat the messages
  MSG msg;
  while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
}

char* Encode64(char* MyString, int MyStringSize) {
  // V 1.1
  // Paramètres :
  //    - Pointeur sur une chaine à encoder
  //    - Taille de la variable chaine - Il FAUT respecter la condition : MyStringSize >= (strlen(MyString) + 3) * 4 / 3
  // Retour : Pointeur sur la chaine passée en paramètre et qui est maintenant encodée en base64 et formatée tel que définit dans la RFC2045 http://jlr31130.free.fr/rfc2045.html
  // Nécessite : Power
  // Parameters :
  //    - Pointer on a string to encode
  //    - Size of the string variable - Condition SHOULD be observed : MyStringSize >= (strlen(MyString) + 3) * 4 / 3
  // Return : Pointer on the string passed in parameter and which is now encoded in base64 and is formatted such as defines in the RFC2045 http://www.faqs.org/rfcs/rfc2045.html
  // Need : Power
  int Pointeur;
  int Pointeur2;
  int CompteurBit;
  int Accumulateur;
  unsigned char Tempo;
  char CharSet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  if (MyStringSize >= (strlen(MyString) + 3) * 4 / 3) {
    CompteurBit = MyStringSize - strlen(MyString);
    // Déplacer la chaine à droite  / Move the string to right
    for (Pointeur = strlen(MyString) - 1; Pointeur >= 0; Pointeur--) {
      MyString[Pointeur + CompteurBit] = MyString[Pointeur];
    }
    Pointeur = CompteurBit;
    Pointeur2 = 0;
    CompteurBit = 0;
    Accumulateur = 0;
    while (Pointeur < MyStringSize) {
      // Traite un caractère de la chaine
      // Décaler ce qui reste dans l'accumulateur de 8 bits à gauche et ajouter un nouveau caractère
      Accumulateur = Accumulateur * 256 + (Tempo = MyString[Pointeur]);   // Tempo évite que les car. > 127 soient négatifs / Tempo prevents that char. > 127 are negative
      CompteurBit = CompteurBit + 8;
      while (CompteurBit >= 6 || (Pointeur == MyStringSize - 1 && CompteurBit > 0)) {
        // Ajouter un caractère (correspondant aux 6 bits de gauche de l'accumulateur) à la chaine produite
        CompteurBit = CompteurBit - 6;
        MyString[Pointeur2] = CharSet[(int) (Accumulateur / Power(2, CompteurBit))];
        Pointeur2++;
        // Retirer les 6 premiers bits de l'accumulateur
        Accumulateur = Accumulateur & (int) (Power(2, CompteurBit) - 1);
      }
      Pointeur = Pointeur + 1;
    }
    // Ajouter 1 ou 2 caractères de fin si le nombre de caractères à encoder n'est pas multiple de 3
    while (Pointeur2 % 4 > 0) {
      MyString[Pointeur2] = '=';
      Pointeur2++;
    }
    MyString[Pointeur2] = 0;
  } else {
    MyString[0] = 0;
  }
  return &MyString[0];
}

char* FrDate(char *MyString) {
  // V 1.0
  // Paramètres :
  //    - Pointeur sur une chaine d'au moins 9 caractères
  // Retour : Pointeur sur la chaine passée en paramètre et qui contient la date courante formatée selon JJ/MM/AA
  // Parameters :
  //    - Pointer on a string of at least 9 characters
  // Return : Pointer on the string passed in parameter and which contain the current date formated according DD/MM/YY
  char Buffer[9];
  _strdate(Buffer);
  MyString[0] = Buffer[3];
  MyString[1] = Buffer[4];
  MyString[2] = '/';
  MyString[3] = Buffer[0];
  MyString[4] = Buffer[1];
  MyString[5] = '/';
  MyString[6] = Buffer[6];
  MyString[7] = Buffer[7];
  MyString[8] = 0;
  return &MyString[0];
}

char* GetAppPath(char *MyString) {
  //V 1.0
  // Paramètres :
  //    - Pointeur sur une chaine de longueur _MAX_PATH
  // Retour : Pointeur sur la chaine passée en paramètre et qui contient le chemin de l'application
  // Parameters :
  //    - Pointer on a string of size _MAX_PATH
  // Return : Pointer on the string passed in parameter and which contain the application path
  int i;
  GetModuleFileName(NULL, MyString, _MAX_PATH);
  for (i = strlen(MyString); i > 0; i--) {
    if (MyString[i] == '\\') {
      MyString[i + 1] = 0;
      i = 0;
    }
  }
  return &MyString[0];
}

char* IntToString(DWORD i, char *MyString) {
  //V 1.0
  // Paramètres :
  //    - Un nombre entier
  //    - Pointeur sur une chaine
  // Retour : Pointeur sur la chaine passée en paramètre et qui contient le nombre
  // Parameters :
  //    - An integer number
  //    - Pointer on a string of size _MAX_PATH
  // Return : Pointer on the string passed in parameter and which contain the number
  sprintf(MyString, "%d", i);
  return &MyString[0];
}

float Power(float Base, float Expo) {
    // V 1.0
    // Paramètres : 2 nombres (e peut être négatif)
    // Retour : la valeur de Base puissance Expo
    // Rarameters : 2 numbers (e can be negative)
    // Return : Value of Base raised to Expo power
  float Accu = 1;
  if (Expo == 0) return Accu;
  if (Expo > 0) {
    while (Expo != 0) {
      Accu = Accu * Base;
      Expo--;
    }
  } else {
    while (Expo != 0) {
      Accu = Accu / Base;
      Expo++;
    }
  }
  return Accu;
}