lang:cpp:preprocesseur
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| lang:cpp:preprocesseur [2020/03/06 19:39] – Précision sur l'utilisation des crochets root | lang:cpp:preprocesseur [2022/08/18 10:31] (Version actuelle) – [Désactiver certains warnings pour certains compilateurs] : déplacement côté C root | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | |||
| ===Utiliser les std:: | ===Utiliser les std:: | ||
| - | Le préprocesseur, | + | Le préprocesseur, |
| - | <code cpp> | + | {{gh>https:// |
| - | void f(std:: | + | |
| - | #define F(XXXX) f(XXXX) | + | |
| - | int main() | + | Rendu : |
| - | { | + | |
| - | std:: | + | |
| - | f(c); | + | |
| - | f({" | + | |
| - | + | ||
| - | // OK. La parenthèse encadre tout l' | + | |
| - | F((std:: | + | |
| - | // | + | |
| - | | + | {{gh> |
| - | F(std:: | + | |
| - | // | + | |
| - | // Pas OK | + | On voit bien que le préprocesseur respecte les parenthèses mais pas les crochets. |
| - | // F({ " | + | |
| - | // f({ " | + | |
| - | | + | [[https://stackoverflow.com/ |
| - | F(({ " | + | |
| - | f(({ " | + | |
| - | // Pas OK | + | ===Remplacer les #define par #pragma once=== |
| - | // F(std:: | + | |
| - | // f(std:: | + | |
| - | } | + | |
| - | </ | + | |
| - | On voit bien que le préprocesseur respecte les parenthèses mais pas les crochets. | + | On peut lancer |
| - | [[https:// | + | Il remplace le premier ''# |
| + | |||
| + | La méthode n'est pas parfaite. Elle peut poser des problèmes sur les fichiers n' | ||
| + | |||
| + | <file python remove_header.py> | ||
| + | import re | ||
| + | from pathlib import Path | ||
| + | |||
| + | def my_function(fname): | ||
| + | with open(fname, ' | ||
| + | lines = infile.readlines() | ||
| + | |||
| + | pifn = re.compile(' | ||
| + | pdefinc = re.compile(' | ||
| + | pendif = re.compile(' | ||
| + | i = len(lines)-2 | ||
| + | for i in range(len(lines)-1): | ||
| + | if lines[i] == '# | ||
| + | return | ||
| + | if pifn.search(lines[i]) and pdefinc.search(lines[i+1]): | ||
| + | break | ||
| + | |||
| + | if i != len(lines)-2: | ||
| + | print(i, lines[i]) | ||
| + | lines[i] = "# | ||
| + | del lines[i+1] | ||
| + | |||
| + | for i in range(len(lines)-1, -1, -1): | ||
| + | if pendif.search(lines[i]): | ||
| + | print(i, lines[i]) | ||
| + | del lines[i] | ||
| + | if lines[i-1]==" | ||
| + | del lines[i] | ||
| + | with open(fname, ' | ||
| + | outfile.write('' | ||
| + | break | ||
| + | |||
| + | |||
| + | for path in Path(' | ||
| + | try: | ||
| + | my_function(path) | ||
| + | except UnicodeDecodeError: | ||
| + | pass | ||
| + | except PermissionError: | ||
| + | pass | ||
| + | </file> | ||
lang/cpp/preprocesseur.1583519948.txt.gz · Dernière modification : de root
