IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Free Pascal Discussion :

[Débutant] Variable pointant sur partie variable d'enregistrement [Free Pascal]


Sujet :

Free Pascal

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    137
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 137
    Points : 163
    Points
    163
    Par défaut [Débutant] Variable pointant sur partie variable d'enregistrement
    Bonjour,

    voilà, j'essaie de convertir un type faisant partie d'un entête C sous Linux que j'ai convertit avec H2Pas :

    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    typedef struct
      {
        SANE_String_Const name;	/* name of this option (command-line name) */
        SANE_String_Const title;	/* title of this option (single-line) */
        SANE_String_Const desc;	/* description of this option (multi-line) */
        SANE_Value_Type type;	/* how are values interpreted? */
        SANE_Unit unit;		/* what is the (physical) unit? */
        SANE_Int size;
        SANE_Int cap;		/* capabilities */
     
        SANE_Constraint_Type constraint_type;
        union
          {
    	const SANE_String_Const *string_list;	/* NULL-terminated list */
    	const SANE_Word *word_list;	/* first element is list-length */
    	const SANE_Range *range;
          }
        constraint;
      }
    SANE_Option_Descriptor;

    ce qui me pose problème c'est l'union. H2Pas l'a convertit en :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    SANE_Option_Descriptor = record
          name : SANE_String_Const;
          title : SANE_String_Const;
          desc : SANE_String_Const;
          option_type : SANE_Value_Type;
          option_unit : SANE_Unit;
          size : SANE_Int;
          cap : SANE_Int;
          constraint_type : SANE_Constraint_Type;
          constraint : record
              case longint of
                 0 : ( string_list : ^SANE_String_Const );
                 1 : ( word_list : ^SANE_Word );
                 2 : ( range : ^SANE_Range );
              end;
       end;
    Ailleurs dans le code je n'arrive pas à récupérer 'constraint' quand je sais qu'il contient ^SANE_Range :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    PSANE_Option_Descriptor = ^SANE_Option_Descriptor;
    saneoption: PSANE_Option_Descriptor;
    range: ^SANE_Range;
     
    range := saneoption^.constraint;
    me donne :
    demomain.pas(167,63) Error: Incompatible types: got "<record type>" expected "^SANE_Range"
    Dans le record, j'ai modifié contraint par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    SANE_Option_Descriptor = record
          name : SANE_String_Const;
          title : SANE_String_Const;
          desc : SANE_String_Const;
          option_type : SANE_Value_Type;
          option_unit : SANE_Unit;
          size : SANE_Int;
          cap : SANE_Int;
          constraint_type : SANE_Constraint_Type;
          case constraint: longint of
             0 : ( string_list : ^SANE_String_Const );
             1 : ( word_list : ^SANE_Word );
             2 : ( range : ^SANE_Range );
       end;
    et j'obtiens :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    demomain.pas(167,63) Error: Incompatible types: got "LongInt" expected "^SANE_Range"
    Maintenant il me renvoie un Longint au lieu d'un Record. Comment faire pour récupérer la valeur de 'contraint' dans le record ?

    Qeul type

  2. #2
    ALT
    ALT est déconnecté
    Membre émérite
    Avatar de ALT
    Homme Profil pro
    Retraité
    Inscrit en
    Octobre 2002
    Messages
    1 234
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Service public

    Informations forums :
    Inscription : Octobre 2002
    Messages : 1 234
    Points : 2 338
    Points
    2 338
    Par défaut
    Salut

    Apparemment, tu ne récupères pas constraint, mais, suivant sa valeur, ton enregistrement aura une des trois structures proposées (instruction case).

    Pour le reste, je ne sais même pas de quoi il s'agit !
    « Un peuple qui est prêt à sacrifier un peu de liberté contre un peu de sécurité, ne mérite ni l'une, ni l'autre, et finira par perdre les deux. »
    Attribué indistinctement à :
    Thomas Jefferson
    Benjamin Franklin
    Albert Einstein !

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    137
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 137
    Points : 163
    Points
    163
    Par défaut
    Salut,

    le problème c'est que je ne sais pas comment affecter une valeur à constraint. j'ai contourné le problème en définissant contraint: Pointer.

  4. #4
    Membre éprouvé
    Avatar de Dr.Who
    Inscrit en
    Septembre 2009
    Messages
    980
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Septembre 2009
    Messages : 980
    Points : 1 294
    Points
    1 294
    Par défaut
    range := saneoption^.constraint.range;


    constraint est un record qui possede les champs suivant :

    string_list
    word_list
    range


    donc
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    case SaneOD^.constraint_type of
      {string list} : ptrSaneSL := SaneOD^.constraint.string_list;
      {word list}  : ptrSaneWL := SaneOD^.constraint.word_list;
      {range}     : ptrSaneRG := SaneOD^.constraint.range;
    end;

    quand on inclus un CASE dans une record, les champs sont "supperposés".
    c'est a dire que la modification d'un champ affecte les autres.

    exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    TColor = record case integer of
      0: (Color: integer);
      1: (R,G,B,A: byte);
    end;
     
    var
      Color : TColor;
     
    begin
      Color.Color := 0;
      // Color.Color = $00000000 = noir
     
      Color.Red := $FF;
      // Color.Color = $000000FF = rouge
     
      Color.Green := $99;
      // Color.Color = $000099FF = orange
     
      Color.Color := 0;
      // Color.Color = $00000000 = noir
     
      Color.Blue := $FF;
      // Color.Color = $00FF0000 = bleu
     
      Color.Red := $FF;
      Color.Green := $FF;
      // Color.Color = $00FFFFFF = blanc
    end;

    dans ton code, string_list, word_list et range sont de type pointeur donc tous de la même taille (pointeur = 4 octets)
    il permet donc de recuperer selon le type fournis un pointeur sur le bon type.
    on peu egalement remplacer constraint par un pointeur.
    sauf qu'il faudrat transtyper le pointeur dans le bon type.

    si tu comprend, c'est que je me suis mal exprimé.
    [ Sources et programmes de Dr.Who | FAQ Delphi | FAQ Pascal | Règlement | Contactez l'équipe ]
    Ma messagerie n'est pas la succursale du forum... merci!

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    137
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 137
    Points : 163
    Points
    163
    Par défaut
    merci,

    ça marche.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Variable pointant sur une partie du tableau
    Par huître dans le forum C
    Réponses: 6
    Dernier message: 17/04/2015, 16h48
  2. Réponses: 14
    Dernier message: 22/09/2009, 16h36
  3. Variable pointant sur un objet fermé
    Par azur668 dans le forum VBA Access
    Réponses: 2
    Dernier message: 03/08/2009, 12h27
  4. Réponses: 2
    Dernier message: 19/10/2005, 10h34
  5. Transtyper element d'1 variable pointant sur TList?
    Par Cornell dans le forum Langage
    Réponses: 2
    Dernier message: 25/02/2003, 22h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo