Get PocketC File Handle Int? - file-io

I'm now taking a look at the PocketC powerful tool, but there is an fileopen function, that generates a integer called filehandle, that is used for most of the File I/O operations of PocketC. How do I use this int filehandle to call the other file manipulation functions?
Here is my example function that I'm using at my program:
fileopen("\test.txt", 0, 0x00000000);
Description of int filehandle: Integer used for file operations, used as a pointer to the fileopen instruction.

What do you mean discover the int filehandle? Your question is very vague.
Do you mean you want this?
int filehandle;
filehandle=fileopen("\test.txt", 0, 0x00000000); //PocketC may not like inline declarations.

The value returned by fileopen on success will be different each time -- that's the point of returning a handle, to uniquely identify a resource. If it returned the same value each time, you would have no way to distinguish the different files you had opened.
You need to save the value like Earlz suggested and then pass the saved variable to the other file manipulation functions.

According to the documentation, fileopen RETURNS the filehandle as an int.
fileopen(string filepath, int type, int flag) : open a file in unicode/ascii.
You can create a new file or simply open one. Please use the flag correctly.
...
Return: Returns an integer as the File Handle if successful,otherwise -1,
Remember to keep this handle value somewhere,
Because you have to use this handle for the rest of file operations.

Related

loading values from `require(file)` into local variables

Sorry if this is an FAQ but if some lib.lua returns a table of local functions e.g.
return {readCsv=readCsv, sumList=sumList, printHelp=printHelp}
and file2.lua imports it using
local lib=require("lib")
is there some programmatic way to automate the loading of the lib values into the local space? So i DON'T have to keep doing e.g.
local readCsv,sumList,printHelp=lib.readCsv,lib.sumList,lib.printHelp
I know its standard practice in LUA and it properly does not annoy many people. But it happens so often that I was wondering if there was a standard shortcut.
(Note: Just to be clear, I know how to make them globals by loading them into _ENV but that is exactly what I do not want.)
In Lua, local variables are statically declared constructs. They're known at compile-time and cannot (generally) be allocated at runtime. Even the number of locals that an individual function holds is known at compile-time, even if the values held in them are not known until the function object is created.
As such, there is no dynamic mechanism to dump the contents of a table into a runtime-defined number of local variables.
The closest you could do is to parse the Lua script text manually to find all of the require statements, do those require yourself, and parse the tables to generate a sequence of local declarations that you will insert into the appropriate place in your Lua script text. You would then compile that script and use it.
But this is a huge amount of work just to get rid of lib.. It's just not worth it.
Since, it's just a regular table that is being returned by require, you can "unpack" that table to turn it into a list of values:
-- return values as both array and hash
return {readCsv, sumList, printHelp,
readCsv=readCsv, sumList=sumList, printHelp=printHelp}
-- then do
local readCsv,sumList,printHelp = (table.unpack or unpack)(require "lib")
The order of returned/assigned values will obviously matter.
If somebody still wants to use a "regular" syntax with local lib = require "lib", it will continue to work.
Based on the above, I came up with a function that simplifies importing. Now I sort of agree with #NicolBolas that is all a little "cancer of the semi-colon" but heh, its short and optional (only a few lines of code, does not mess with usual LUA module conventions)
get"file" requires the file and unpacks results in alphabetical order. Kind of analogous to the Python command
from file import *
get"file thing1 thing2.." requires the file and unpacks only thing1 thing2. Kind of analogous to the Python command
from file import thing1,thing2
e.g. heres a file that returns some code:
-- file = cc.lua
function fun1() print(1) end
function fun2() print(2) end
function fun3() print(3) end
function fun4() print(4) end
function fun5() print(5) end
return {fun3=fun3, fun1=fun1, fun2=fun2, funs5=fun5, fun4=fun4}
Here's one example of usage (unpack all). Note that the sub-module (cc.lua) can return its things in any order at all and this code will unpack them in alpha order:
-- file = gettest1.lua
local get=require"get"
local fun1,fun2,fun3,fun4,fun5=get"cc"
fun5()
And here's the other usage where we can unpack somethings, in any order we want:
-- file = gettest2.lua
local get=require"get"
local fun3,fun1=get"cc fun3 fun1"
fun1()
And here's the code for the get.lua file that holds the get function
-- file = get.lua
local function get(spec)
local gets, keys,out={},{},{}
for get in string.gmatch(spec, "([^ ]+)") do gets[#gets+1]= get end
local results = require(table.remove(gets,1))
if #gets>0
then keys= gets
else for key,_ in pairs(results) do keys[#keys+1] = key end
table.sort(keys)
end
for _,key in ipairs(keys) do out[#out+1] = results[key] end
return table.unpack(out)
end
return get
Improvements? Suggestions? Comments?

Get file translation mode of stdout

Function int _setmode(int, int) allows to switch stdout among ASCII and UTF (wide) modes.
Is there a function to read the current translation mode of stdout? Something like _getmode? In C# there is the Console.OutputEncoding property.
I'd like to use it for functions that can temporarily change the mode and then set back the original mode.
From the documentation:
Return Value
If successful, returns the previous translation mode.
Using the return value allows to set back the original value.

How to write to a file in Go

I have seen How to read/write from/to file using golang? and http://golang.org/pkg/os/#File.Write but could not get answer.
Is there a way, I can directly write an array of float/int to a file. Or do I have to change it to byte/string to write it. Thanks.
You can use the functions in the encoding/binary package for this purpose.
As far as writing an entire array at once goes, there are no functions for this. You will have to iterate the array and write each element individually. Ideally, you should prefix these elements with a single integer, denoting the length of the array.
If you want a higher level solution, you can try the encoding/gob package:
Package gob manages streams of gobs - binary values exchanged between an Encoder (transmitter) and a Decoder (receiver). A typical use is transporting arguments and results of remote procedure calls (RPCs) such as those provided by package "rpc".

Equivalent of "Dim As String * 1" VB6 to VB.NET

I have some VB6 code that needs to be migrated to VB.NET, and I wanted to inquire about this line of code, and see if there is a way to implement it in .NET
Dim strChar1 As String * 1
Intellisense keeps telling me that an end of statement is expected.
That's known as a "fixed-length" string. There isn't an exact equivalent in VB.NET.
Edit: Well, OK, there's VBFixedStringAttribute, but I'm pretty sure that exists solely so that automated migration tools can more easily convert VB6 code to VB.NET for you, and it's not really the ".NET way" to do things. Also see the warnings in the article for details on why this still isn't exactly the same thing as a fixed-length string in VB6.
Generally, fixed-length strings are only used in VB6 if you are reading fixed-size records from a file or over the network (i.e. parsing headers in a protocol frame).
For example, you might have a file that contains a set of fixed-length records that all have the format (integer, 1-character-string, double), which you could represent in VB6 as a user-defined type:
Public Type Record
anInteger As Integer
aSingleCharacter As String * 1
aDouble As Double
End Type
This way, VB6 code that reads from the file containing records in this format can read each fixed-sized record stored in the file, and in particular, it will only read 1 byte for aSingleCharacter. Without the * 1, VB6 would have no idea how many characters to read from the file, since a String can normally have any number of characters.
In VB.NET, you can do one of the following, depending on your needs:
If the length matters (i.e. you need to read exactly one byte from some data source, for example) consider using an array instead, such as
Dim aSingleByteArray(1) As Byte
Alternatively, you could use one of the Stream classes. In particular, if you are reading data from a network socket or a file, consider using NetworkStream or FileStream, respectively. A Stream is meant for byte-for-byte access (i.e. raw binary access). StreamReader is a related class that simplifies reading data when it is text-based, so that might be good if you are reading a text file, for example. Otherwise (if dealing with binary data), stick with one of the Stream classes.
If the length doesn't matter, you could just use a "normal" String. That is to say:
Dim aNormalString As String
Which answer is "correct" really depends on why it was declared that way in the original VB6 code.
The fixed length strings has been deprecated in VB.NET because there are several better options.
Since your fixed length string is just one character long, you can use the Char type in this case, as Mark suggested.
Dim strChar1 As Char
Seeing as you're doing a VB6 migration, I'd definitely consider VBFixedStringAttribute as well as the other options listed by Mike Spross, but, in this case, because it is a single character, a Char may be an option in this case too.
As mentioned elsewhere VBFixedString is only acknowledged by the Get and Put VB I/O API. So the best solution (other than rewriting your code that references the "fixed length string") is to write your own equivalent of Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString. See this answer for more details.
VBFixedStringAttribute Class
<VBFixedString(1)> Dim strChar1 As String
ALthough this question was asked ages ago, VB.NET actually has a native fixed-length string -- <VbFixedArray(9)> Public fxdString As Char() 'declare 10-char fixed array. Doing this with scalars actually creates VB6-style Static Arrays.

Store an NSString as a fixed length integer?

having a bit of trouble finding a solution to this.
I want to take a large ordered text file of words and create - in the same order - a text file of fixed length numeric values.
For example:
Input File Output File
AAA -> 00000001
AAH -> 00002718
AAZ -> 71827651
Initially it seemed a hash function would do the trick. However they are one way. Also perhaps they are a bit "heavyweight" for this. After all, I don't need any cryptography. Plus, it's a reference file. It will never change.
Any compression is a bonus not essential. That said, I don't want the file to get any bigger than it already is. Which is why I don't just want to write out the words as text but with fixed lengths.
So, bottom line; input is a NSString of variable length, output is an integer of fixed length. And, I must be able to take the integer and figure out the string.
Any help much appreciated!
Thanks!
xj
Well, this would be a bit of a brute force method, but here's my guess.
Start by making a custom function to convert one letter of text to an integer less than 100. (I'm not sure if such a function already exists, if so then great!) You might need to just go to stuff like "if ([input isEqual: #"a"]){ return 1;}
Then, run that function on each letter of text, and get the final integer by combining the previous results.
For example:
int myVal1 = [intConverter firstLetter];
int myVal2 = [intConverter secondLetter];
int myVal3 = [intConverter thirdLetter];
int finalValue =100^3 + 100^2*myVal1 + 100*myVal2 + myVal3;
Then, finalValue would be of the form 1(myVal1)(myVal2)(myVal3), which is what I think you're looking for.
To get back the original string, simply use the mod (%) and division functions to get the individual values back, then run the intConverter function backwards. (This would probably mean writing a new function that basically runs those if statements in reverse, but oh well.)
I hope this helps.