How can i use the getline fn in c to find and display a file path/directory on a unix machine - getline

I'm researching on how to use the getline() function in C on a Unix machine to find a file directory and also display the path. The info I'm getting so far is just for using the getline() function to read intext.

Related

Return drive letter for findfile in vim script?

I use findfile() function in Vim to retrieve the full path of a file. It seems the returned path (if found) doesn't include drive letter (and :, like c:) on Windows so it might have some issue to use this path . Is there a way to get driver letter for the returned path, or a dedicated function can do this?
The path returned by findfile() is correct with regards to the current working directory, so as long as you don't change that, using it directly should be fine.
If you need to switch buffers or do other things that might affect the CWD, you can expand the filespec into a full absolute form via:
let absoluteFilespec = fnamemodify(findfile(...), ':p')

Need to get arrow key codes from stdin after reading file using stdin

I am creating a NASM assembly code to read 2d array of numbers present in file from stdin
i am running the executable like this -> ./abc < input.txt .
and after that i will display the read 2d array on terminal then i want to get keys codes of arrow keys (which normal appear in terminal as special characters) i wrote code for it but its not working. ( I did echo off in termios setting for that)
Although it was working when i am taking file name as an argument & reading and not from stdin but using fopen with proper fd.
./abc abc.txt
in this case after displaying the read 2d array i am able to get arrow keys codes in program but not in earlier case.
Please help me in this matter.
By using input redirection you disconnect stdin from your terminal and instead connect it to a pipe that your shell is reading the file into.
You could use cat input.txt - | ./abc, but you would have to pres Enter to flush the line buffer and make cat pipe the current line into your program.
I would suggest not messing with stdin and just taking the input file as an argument, like you already did before.

How to write each line of the result of a a system() call into a list in Vimscript?

I'd like to loop over the files in a directory using Vimscript. Reading usr_41.txt and having searched around, the best I can come up with is something like let dir_contents = system('ls')
But since system() isn't returning a list, I can't loop over it. Is there either a way I can save the results of a system call as a list, or a Vim command or function that does so already?
You can get a list with split(system('ls'), '\n'), which will give you a list of files providing you don't have files with newlines in them.
Try something like
split(system('ls', nr2char(10))
I am currently not on a Unix system, so I can't try it myself, but it works on Windows (and dir), such as
for FILE in split(system('dir /b', nr2char(10))
echo 'File is: ' . FILE
endfor
There is a built-in glob() function. To get a list of files in current directory on windows you could use split(glob('.\\*'), "\n"). On *nix it is much more complicated as
POSIX allows everything except NULL to be in filename. Everything here means that newline ("\n") is also allowed.
glob() function does not return filenames starting with dot unless explicitely requested (using glob('dir/.*')).
When explicitely requested to list filenames starting with dot glob() also shows . (current directory) and .. (parent directory) special directories.
In order to solve this problems you have to use something like this (or use vim with python support and python's own os.listdir function).
If you don't mind having frawor in dependencies, you could do the following:
execute frawor#Setup('0.0', {'#/os': '0.0'})
<...>
let dir_contents=s:_r.os.listdir('.')
About getting list of lines from a shell command: if you know that command you launch won't output NULLs, you can use split(system(cmd), "\n", 1) (maybe without last argument if you don't care about empty lines). If you know that command may output NULLs and you want to keep them, you have to do more work:
noautocmd new
read !cmd
let s:shell_output=getline(2, line('$'))
noautocmd bwipeout!
Note that NULLs in this case will get replaced with newlines inside a s:shell_output list, while actual newlines will be represented as string ends.
I just discovered that there is a subtlety to these answers:
Currently (Jan 2018), vim has systemlist() that will answer your needs quite well:
let l:ls=systemlist("ls")
" or let dir_contents=etc etc
However, if you are doing something besides "ls" you might run into a weird bit of tricky behavior: trailing blank lines will get dropped (as of Jan 2018). If this is not a problem, don't worry, use systemlist() and be happy. If it is a problem, you probably want to do something like this:
let l:myvar=system("myprogram -that -returns -blank -lines")
l:mylist=split(l:myvar, "\n", 1) " the 1 is to keep empty lines
Hope that helps someone!
systemlist added in Vim 7.4:248 does exactly what you want:
:echo systemlist('printf "a\nb\n"')
output:
['a', 'b']
From the docs http://vimhelp.appspot.com/eval.txt.html#eval.txt
systemlist({expr} [, {input}]) systemlist()
Same as system(), but returns a List with lines (parts of
output separated by NL) with NULs transformed into NLs. Output
is the same as readfile() will output with {binary} argument
set to "b".
Returns an empty string on error.

How do I search through MATLAB command history?

I would like to search for a specific command I've previously used. Is it possible to do a free text search on MATLAB command history?
Yes. Matlab stores your command history in a file called history.m in the "preferences folder," a directory containing preferences, history, and layout files. You can find the preferences folder using the prefdir command:
>> prefdir
ans =
/home/tobin/.matlab/R2010a
Then search the history.m file in that directory using the mechanism of your choice. For instance, using grep on unix:
>> chdir(prefdir)
>> !grep plot history.m
plot(f, abs(tf))
doc biplot
!grep plot history.m
You can also simply use the search function in the command history window if you just want to use the GUI.
If you want to accomplish this in a programmatic and platform-independent manner, you can first use MATLAB's Java internals to get the command history as a character array:
history = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
historyText = char(history);
Then you can search through the character array however you like, using functions like STRFIND or REGEXP. You can also turn the character array into a cell array of strings (one line per cell) with the function CELLSTR, since they can sometimes be easier to work with.

How do I actually use the value of an argument to a Vim function?

I'm trying to write a simple Vim function that takes the name of a file as an argument and reads the contents of that file into the current document (related to this question).
Here's my first stab at it:
fun! Tpl(tplfile)
r c:\tpl\a:tplfile
endfun
That just gives me the following error:
E484: Can't open file c:\tpl\a:tplfile
How do I make the function actually use the value of the tplfile argument?
Replace the line with:
exe 'r c:\tpl\' . a:tplfile
The a:tplfile is a string variable, so to include it in a command, you have to combine the whole lot into one string (with the '.' operator) and then use exe to run the command
:help exe
On a related note (and a shameless plug), if you're trying to add templates (as implied by the post you linked to), my file templates plugin has a command AddTemplate to add a template from your vimfiles/templates directory at the current cursor location. However, the documentation is currently rather poor, so if you decide to use it and have any difficulties, feel free to drop me an email at the address on my website.

Resources