how to get the file extension in pike - file

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")

Related

What is the best way to parse a .c file?

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

Grammar file (grammar.txt)

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

Writing multiline text files in Lua

I would like to know the best way to make my script write something into a file (lets say text.txt) in a way that would always add a line break at the end. When I append text using
file = io.open("test.txt", "a")
file:write("hello")
twice, the file looks like:
hellohello
But I want it to look like:
hello
hello
Unlike print, the new line character isn't added automatically when calling io.write, you can add it yourself:
file:write("hello", "\n")
The easiest way to achieve this would be to include a Newline character sequence every time you call the write method like so: file:write("hello\n") or so: file:write("hello", "\n"). This way, a script such as
file = io.open(test.txt, "a")
file:write("hello", "\n")
file:write("hello", "\n")
would result in the desired output:
hello
hello
There are, however, many other solutions to this (some being more elegant than the others). When outputting text in Java, for example, there are special methods such as BufferedWriter#newLine(), which will do the same thing in a more cleaner fashion. So if your interested in a different way of achieving this, I suggest you read up on the Lua docs for analogous methods/solutions.

A old text file with special format

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!

C - Reading multiple files

just had a general question about how to approach a certain problem I'm facing. I'm fairly new to C so bear with me here. Say I have a folder with 1000+ text files, the files are not named in any kind of numbered order, but they are alphabetical. For my problem I have files of stock data, each file is named after the company's respective ticker. I want to write a program that will open each file, read the data find the historical low and compare it to the current price and calculate the percent change, and then print it. Searching and calculating are not a problem, the problem is getting the program to go through and open each file. The only way I can see to attack this is to create a text file containing all of the ticker symbols, having the program read that into an array and then run a loop that first opens the first filename in the array, perform the calculations, print the output, close the file, then loop back around moving to the second element (the next ticker symbol) in the array. This would be fairly simple to set up (I think) but I'd really like to avoid typing out over a thousand file names into a text file. Is there a better way to approach this? Not really asking for code ( unless there is some amazing function in c that will do this for me ;) ), just some advice from more experienced C programmers.
Thanks :)
Edit: This is on Linux, sorry I forgot to metion that!
Under Linux/Unix (BSD, OS X, POSIX, etc.) you can use opendir / readdir to go through the directory structure. No need to generate static files that need to be updated, when the file system has the information you want. If you only want a sub-set of stocks at a given time, then using glob would be quicker, there is also scandir.
I don't know what Win32 (Windows / Platform SDK) functions are called, if you are developing using Visual C++ as your C compiler. Searching MSDN Library should help you.
Assuming you're running on linux...
ls /path/to/text/files > names.txt
is exactly what you want.
opendir(); on linux.
http://linux.die.net/man/3/opendir
Exemple :
http://snippets.dzone.com/posts/show/5734
In pseudo code it would look like this, I cannot define the code as I'm not 100% sure if this is the correct approach...
for each directory entry
scan the filename
extract the ticker name from the filename
open the file
read the data
create a record consisting of the filename, data.....
close the file
add the record to a list/array...
> sort the list/array into alphabetical order based on
the ticker name in the filename...
You could vary it slightly if you wish, scan the filenames in the directory entries and sort them first by building a record with the filenames first, then go back to the start of the list/array and open each one individually reading the data and putting it into the record then....
Hope this helps,
best regards,
Tom.
There are no functions in standard C that have any notion of a "directory". You will need to use some kind of platform-specific function to do this. For some examples, take a look at this post from Cprogrammnig.com.
Personally, I prefer using the opendir()/readdir() approach as shown in the second example. It works natively under Linux and also on Windows if you are using Cygwin.
Approach 1) I would just have a specific directory in which I have ONLY these files containing the ticker data and nothing else. I would then use the C readdir API to list all files in the directory and iterate over each one performing the data processing that you require. Which ticker the file applies to is determined only by the filename.
Pros: Easy to code
Cons: It really depends where the files are stored and where they come from.
Approach 2) Change the file format so the ticker files start with a magic code identifying that this is a ticker file, and a string containing the name. As before use readdir to iterate through all files in the folder and open each file, ensure that the magic number is set and read the ticker name from the file, and process the data as before
Pros: More flexible than before. Filename needn't reflect name of ticker
Cons: Harder to code, file format may be fixed.
but I'd really like to avoid typing out over a thousand file names into a text file. Is there a better way to approach this?
I have solved the exact same problem a while back, albeit for personal uses :)
What I did was to use the OS shell commands to generate a list of those files and redirected the output to a text file and had my program run through them.
On UNIX, there's the handy glob function:
glob_t results;
memset(&results, 0, sizeof(results));
glob("*.txt", 0, NULL, &results);
for (i = 0; i < results.gl_pathc; i++)
printf("%s\n", results.gl_pathv[i]);
globfree(&results);
On Linux or a related system, you could use the fts library. It's designed for traversing file hierarchies: man fts,
or even something as simple as readdir
If on Windows, you can use their Directory Management API's. More specifically, the FindFirstFile function, used with wildcards, in conjunction with FindNextFile

Resources