Jump to content
em

Scoate comentariile din sursa C/C++ [linux]

Recommended Posts

Posted

S? spunem c? face?i rost de o sursa de C dar nu vre?i ?i comentariile din ea.


#! /bin/sed -nf

# Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com)

# Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org)
# Works its way through the line, copying to hold space the text up to the
# first special character (/, ", '). The original version went exactly a
# character at a time, hence the greater speed of this one. But the concept
# and especially the trick of building the line in hold space are entirely
# merit of Brian.



# ./script.sed < input.c
# ./script.sed < input.c > output.c
# for c in *.c; do script.sed < $c > /tmp/zyzcc.c; /bin/cp -f /tmp/zyzcc.c $c; done

:loop

# This line is sufficient to remove C++ comments!
/^\/\// s,.*,,

/^$/{
x
p
n
b loop
}
/^"/{
:double
/^$/{
x
p
n
/^"/b break
b double
}

H
x
s,\n\(.[^\"]*\).*,\1,
x
s,.[^\"]*,,

/^"/b break
/^\\/{
H
x
s,\n\(.\).*,\1,
x
s/.//
}
b double
}

/^'/{
:single
/^$/{
x
p
n
/^'/b break
b single
}
H
x
s,\n\(.[^\']*\).*,\1,
x
s,.[^\']*,,

/^'/b break
/^\\/{
H
x
s,\n\(.\).*,\1,
x
s/.//
}
b single
}

/^\/\*/{
s/.//
:ccom
s,^.[^*]*,,
/^$/ n
/^\*\//{
s/..//
b loop
}
b ccom
}

:break
H
x
s,\n\(.[^"'/]*\).*,\1,
x
s/.[^"'/]*//
b loop

Cum se foloseste?

./removeCommends.sed fisierInitial.cpp > fisierFaraComentarii.cpp

Bonus:

Nu strica indentarea.

  • Upvote 1
Posted

Prea simplu

Sa presupunem ca ai o sursa source.c

Dai in terminal comanda asta

cat source.c |grep -v "^#" > newsource.c

Si apoi vei avea fisieru newsource.c fara linile cu comentarii de genu #Comment ;-)

Scuze asta e daca ai o sursa de bash scripting, cu care eu sunt obisnuit :|

Exista si asa:

cat source.c |grep -v "^//" > newsource.c

Pentru a sterge linile care incep cu '//' (daca asta vroiai)

Pentru '/*' e putin mai complicat dar se poate

Edit: Am perfectionat umpic script-ul pentru a scoate liniile care incep cu # (pastrand prima linie - pentru bash scripting) si totodata eliminand liniile goale

1. head -1 source.c > newsource.c

2. cat source.c |grep -v "^#" |sed '/^$/d' >> newsource.c

1. Copiaza prima linie in fisierul newsource.c

2. Elimina liniile care incep cu # si liniile goale

Exemplu:

source.c

#! /bin/sed -nf

# Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com)

# Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org)
# Works its way through the line, copying to hold space the text up to the
sdgsdfg

//dsfgs
dfh
#
//r12
3t

W
/*Qd
g
23
g
#@!%@!#T
dg
sdg
s
gw
e

Si am obtinut:

newsource.c

#! /bin/sed -nf
sdgsdfg
//dsfgs
dfh
//r12
3t
W
/*Qd
g
23
g
dg
sdg
s
gw
e

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...