I am looking for ideas and examples about how to parse a ".C" file.
Apparently there is no easy way to do this. My goal is to parse a file, find a line and replace the wording of that line.
Any ideas? Thank you in advance!
You shouldn't need to parse a file to replace some text in it. A utility like sed can do it easily with either a literal string or a regular expression.
Otherwise any decent text editor will do it
Related
I'm working on a program in pike and looking for a method like the endswith() in python- a method that gives me the extension of a given file.
Can someone help me with that?
thank you
Python's endswith() is something like Pike's has_suffix(string s, string suffix):
has_suffix("index.html", ".html");
Reference:
http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/has_suffix.html
extract the end of the string, and compare it with the desired extension:
"hello.html"[<4..] == ".html"
(<4 counts from the end of the string/array)
If you want to see what the extension of a file is, just find the last dot and get the substring after it, e.g. (str/".")[-1]
If you just want to check if the file is of a certain extension, using has_suffix() is a good way, e.g. has_suffix(str, ".html")
I am actually working on a grammar file and I am reading the grammar.txt file.
The 20 first lines are new to me.
%s/^\d*\.\s*(\w*)
%s/^\d*\.\s*\(\w*\)
%s/^\d*\.\s*\(\w*\)/<\1>
%s/^\d*\.\s*\(\w*\)/\1
%s/\<\(\w*\)\>
%s/"\w*\"
%s/"\(\w*\)\"/_\1_/g
%s/"\(\w*\)\"/&\1&/g
%s/"\(\w*\)\"/123456\1/g
%s/"\(\w*\)\"/**\1**/g
%s/"\(.*\)\"/$\1$/g
%s/"\(\w*\)\"/$\1$/g
%s/"/'/g
%s/'\(\w*\)'\/$\1$/g
Does anyone know what this lines refers to?
This looks like list of replacement rules someone tried to run in vim.
It seems as the someone didn't know how to use it, so was trying to figure it out.
the proper structure is %s/match/replacement/flags
%s means search through all lines in the entire file,
match is regular expression that you are looking for,
replacement is what the match will be replaced with,
flags are regexp flags, in this case g, which will replace all occurrences at each line.
more info on vim's search and replace
I am working on HP-UX project, there are a old document. Can open it with vim, but there are some special character among text. For example:
.P
"xxxxx"
.AL 1 10
.LI "xxx"
.H 3 "xxxx"
It looks like html but not be html. Is it possible convert it to modern document?
Looks like troff. Install GNU troff (Groff) and try:
groff -Thtml -pet -mm input.mm > output.html
I guess more details are needed - some ideas you may try:
First, issue a file command for the file. It will probably tell you what type of file is.
jim#debian:~$ file foo.bar
foo.bar: ASCII text
Second, search for similar files and see if there's a program to open them in the machine - maybe, they are binary files for some program out there, and you just don't know which one.
Last, but not least, I believe you are right - looks like HTML code to me, so maybe this is used by an application as a kind-of intermediate language, that is parsed later to transform it to real HTML.
I hope this helps!
I have a file with path names to files:
/my/path1
/my/path11
/my/path12
/my/path13
The file structure is that it has individual paths in each line. All I want to do is search for the existence of a string /my/path1 or anyother in the above file many times
I could think of 2 methods.
every time get file contents line by line and then search the string. Advantage is that the file can be of anysize and I dont need to worry about buffer overflow.
Load the contents into a buffer and search it using the buffer. But as I dont have control over the file size I should be cautious here.
What is the best approach? I am working in unix. Is there any in-build library commands in C that I can make use of for this purpose? Or how can I accomplish the same task using awk in C code.
If you use stdio it will do the buffering for you. You can change its operation by using the function setvbuf to buffer more than a single line. getline can by used to check line by line.
I think loading all the file in memory is not a good idea. Using fgets and strcmp is the best way, I guess.
I have a C source code spread over many source files (*.c). For porting reasons say I need to comment out below statements from all source files
fprintf(stderr,"......",....);
The problem is these fprintfs could be multiline statements, meaning broken into two/or more lines spread over two lines in source files with a newline character(carriage returned entered at end of one line).
How can I find such fprintfs scattered across all source files, replace them with a
multiline C comment as:
/*
*/
Since they are multiline, the find and replace command of source editors did not help.
I am trying to read the source file using a PERL script but, parse them to do this but could not.
Any pointers would be useful.
thank you.
-AD.
What you are looking for is named "coccinelle", it's semantic patch tool for C, via this you can easily do this. viz. http://coccinelle.lip6.fr/
Just
#undef fprintf
#define fprintf(stream, format, ...) 42
at the top of your files and be happy.
For a pure perl solution try something like this:
my $all_lines;
{ # (limit scope of $/, if appropriate)
local $/; #slurp entire file in one go
$all_lines = <$file_handle>
$all_lines =~ s|(\bfprintf\s*\(\s*stderr\s*,.*?\)\s*;)|/* $1 */|sg;
}