Re: Ruby , regexp et parsing
[ Nouvelle discussion
| Répondre au groupe
|
fr.comp.lang.ruby ]
Sujet: Re: Ruby , regexp et parsing
De: john....@anonymous.org (mosfet)
Groupes: fr.comp.lang.ruby
Organisation: Guest of ProXad - France
Date: 04. Feb 2008, 15:45:57
References: 1 2
|
mosfet a écrit :
> mosfet a écrit :
>> Salut,
>>
>> Je suis un debutant complet en ruby et j'aimerais l'utiliser afin
>> d'extraire des données d'un fichier.
>>
>> En gros j'ai un fichier texte qui contient des definitions COM
>>
>> #define PR_ORIGINAL_ENTRYID PROP_TAG(
>> PT_BINARY, 0x3A12)
>> #define PR_ORIGINAL_DISPLAY_NAME PROP_TAG(
>> PT_TSTRING, 0x3A13)
>>
>>
>> et j'aimerais ouvrir ce fichier pour construire un tableau de
>> propriétés :
>>
>>
>> typedef struct mapiProp_s
>> {
>> ULONG ulPropId;
>> ULONG ulPropType;
>> }mapiProp_t;
>>
>> mapiProp_t mapiPropArray[] =
>> {
>> { PR_ORIGINAL_ENTRYID, "PR_ORIGINAL_ENTRYID", PT_BINARY },
>> { PR_ORIGINAL_DISPLAY_NAME, "PR_ORIGINAL_DISPLAY_NAME", PT_TSTRING },
>> };
>>
>>
>> Si quelqu'un pouvait me donner quelques pistes.
>>
>>
>>
>>
>> Merci
> Pour le moment j'arrive a ca :
>
> File.open('C:/mapitags.h', 'r') do |filein|
> File.open('C:/mapitags_Props.h', 'w') do |fileout|
> regex = Regexp.new('PR_ \"(w+)\"')
> while line = filein.gets
> match = regex.match(line)
> if match
> puts match[1]
> else
> puts "Skip this line"
> end
> end
> end
> end
>
> Mais c'est pour la regexp ou j'ai du mal ...
Bon finalement comme personne ne m'aide j'ai pondu ca :
File.open('C:/mapitags.h', 'r') do |filein|
File.open('C:/mapitags_Props.h', 'w') do |fileout|
fileout.puts "typedef struct mapiProp_s {"
fileout.puts "ULONG ulPropId;"
fileout.puts "TCHAR szPropName[128];"
fileout.puts "ULONG ulPropType;"
fileout.puts "}mapiProp_t; "
fileout.puts ""
fileout.puts ""
fileout.puts "mapiProp_t mapiPropArray[] ={"
regex1 = Regexp.new('PR_[0-9a-zA-Z_]*')
regex2 = Regexp.new('PT_[0-9a-zA-Z_]*')
while line = filein.gets
match1 = regex1.match(line)
match2 = regex2.match(line)
if match1 && match2
fileout.puts "{ #{match1}, _T(\"#{match1}\"), #{match2} },"
end
end
fileout.puts "};"
end
end
LE probleme c'est que certaines données extraites sont fausses et je ne
vois pas pourquoi. Comme si ma regexp faisait n'importe quoi par moment...

|
 cette fonctionnalité est reservée aux membres ayant une session active !
|