Correct function to parse a ansistring - parsing

I'm using C++ Builder Berlin Community edition.
I want to parse a string such as "1234' into a integer array such as N[1] = 1, N[2] = 2, ... .
I've tried using SubString(str, #, #), StrMove(str, #, #), Copy(str, #, #).
They all return "function not found". I've been developing code for over years now, so I know I am not including the correct header (either .h, or .hpp) file in my include statements, or the programmers at Embarcadero in their wisdom have modified C++ Builder to make these functions obsolete.
int Lock;
AnsiString N_Str;
randomize();
Lock = rand() * 1000;
N_Str = AnsiString(Lock);
L[0] = StrToInt(StrMove(N_Str, 1, 1));
L[1] = StrToInt(SubString(N_Str, 2, 1));
L[2] = StrToInt(copy(N_Str, 3, 1));
L[3] = StrToInt(copy(N_Str, 4, 1));

Related

Equivalent for Java System.arraycopy in Dart?

How do I convert the below java code to equivalent dart.
private static final byte[] mIdBytes = new byte[]{(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x7E};
byte[] data;
System.arraycopy(mIdBytes, 2, data, 0, 4);
Is there any Dart method that does a similar kind of operation?
I was looking into this:
https://pub.dev/documentation/ckb_dart_sdk/latest/ckb-utils_number/arrayCopy.html
To match Java's System.arrayCopy(source, sourceOffset, target, targetOffset, length)
you should use
target.setRange(targetOffset, targetOffset + length, source, sourceOffset);
This is more efficient than using List.copyRange for some lists, for example copying between typed-data lists with the same element size (like two Uint8Lists).
Well, I found the way to do it.
you can just use
List.copyRange(data, 0, mIdBytes, 2);
This is a workaround I kinda found to be done in your case. This is called sublist(), this method will take the start index, and an end index.
IDEA:
Use sublist(), and copy the elements to be started from, that sourcePos = you_pos
Source array will be used like sourceArray.sublist(startIndext, endIndex)
The destination array will be initialized with the value using sublist()
Till what length the item should be added would be mentioned in the end index+2, since it will ignore the last item, and copy till the index-1
FINAL CODE
void main() {
List<int> source = [1, 2, 3, 4, 5, 6];
List<int> target = [];
int startPos = 1;
int length = 4;
// to ensure the length doesn't exceeds limit
// length+2 because, it targets on the end index, that is 4 in source list
// but the end result should be length+2 to contain a length of 5 items
if(length+1 <= source.length-1){
target = source.sublist(startPos, length+2);
print(target);
}else{
print('Cannot copy items till $length: index out of bound');
}
}
//OUTPUT
[2, 3, 4, 5, 6]

How to align a designated initializer in C99 with clang-format?

I am using clang-format 4.0.0 to align a personal project of mine.
I am using the following configurations for clang-format.
Language: Cpp
BreakBeforeBraces: Allman
ColumnLimit: 120
TabWidth: 4
IndentWidth: 4
UseTab: ForContinuationAndIndentation
The sample code below is aligned using the above configuration.
struct test
{
int a;
int b;
int c;
};
struct test T = {
.a = 1, .b = 2, .c = 3,
};
Is there any way to align the initialization part like the one shown below.
Basically I am looking for a way to place all the initializers in separate lines.
struct test T =
{
.a = 1,
.b = 2,
.c = 3,
};
Using clang-format 6.0.0, the formatting is what you ask for. In fact, there no longer seems to be any way to get the single-line formatting that you don't like.

Proper implementation of OpenPrinter2 with PRINTER_OPTION_NO_CACHE

I'm modifying some existing code which uses OpenPrinter to instead use OpenPrinter2. It contains one more parameter PPRINTER_OPTIONS which in Delphi is encapsulated as a TPrinterOptions, defined like so:
_PRINTER_OPTIONSW = record
cbSize: Cardinal;
dwFlags: DWORD;
end;
I'm having trouble understanding how to use this struct to encapsulate the flag PRINTER_OPTION_NO_CACHE, and I can't even find that constant in any of the existing VCL/RTL.
var
PD: TPrinterDefaults;
PO: TPrinterOptions;
begin
PO.dwFlags:= ???
if OpenPrinter2(ADevice, #FPrinterHandle, #PD, #PO) then begin
...
I'm not having much luck searching for Delphi implementation of either OpenPrinter2 or TPrinterOptions. How do I make PRINTER_OPTION_NO_CACHE work?
EDIT
Is this correct?
const
PRINTER_OPTION_NO_CACHE = 0;
PRINTER_OPTION_CACHE = 1;
PRINTER_OPTION_CLIENT_CHANGE = 2;
...
PO.dwFlags:= PRINTER_OPTION_NO_CACHE;
The MSDN docs say this:
typedef enum tagPRINTER_OPTION_FLAGS {
PRINTER_OPTION_NO_CACHE,
PRINTER_OPTION_CACHE,
PRINTER_OPTION_CLIENT_CHANGE
} PRINTER_OPTION_FLAGS;
A C enum, as declared here, is really just an int. The first enum value is 0, the next 1, and so on.
But the header file tells a different story, contradicting the documentation. The header file says:
typedef enum _PRINTER_OPTION_FLAGS
{
PRINTER_OPTION_NO_CACHE = 1 << 0,
PRINTER_OPTION_CACHE = 1 << 1,
PRINTER_OPTION_CLIENT_CHANGE = 1 << 2,
PRINTER_OPTION_NO_CLIENT_DATA = 1 << 3,
} PRINTER_OPTION_FLAGS;
So translate to Pascal like this.
const
PRINTER_OPTION_NO_CACHE = 1;
PRINTER_OPTION_CACHE = 2;
PRINTER_OPTION_CLIENT_CHANGE = 4;
PRINTER_OPTION_NO_CLIENT_DATA = 8;
Populate the record like this:
var
Options: TPrinterOptions;
....
Options.cbSize := SizeOf(Options);
Options.dwFlags := PRINTER_OPTION_NO_CACHE;

Cast uint8 to uint8[]

In Vala, some methods require an Array of uint8's (uint8[]) as a parameter. For example see http://valadoc.org/#!api=glib-2.0/GLib.FileStream.write
I have the following code, but no idea how to "cast" my value to an array:
...
uint8 some_integer = 7;
desc.write(???, 1);
...
In C I'd simply do:
...
uint8 some_integer = 7;
fwrite(&some_integer, 1, 1, desc);
...
but the Vala compiler is not amused about the &-operator. What to do?
You can create an array in Vala as int[] b = { 2, 4, 6, 8 };. Hence you should be able to create it with a single variable too as uint8 [] some_array = {some_integer};; in your case desc.write({some_integer}, 1);.
Here's a detailed guide on Vala for further references.

replacement for luaL_getMetaTable

I want to enable Lua-Scripting (Lua 5.1) in my Delphi application. For this purpose I use the header Files of Thomas Lavergne.
Now I try to register a userdata type following this example: http://www.lua.org/pil/28.2.html
At the "new array function" it uses the command *luaL_getmetatable*.
static int newarray (lua_State *L) {
int n = luaL_checkint(L, 1);
size_t nbytes = sizeof(NumArray) + (n - 1)*sizeof(double);
NumArray *a = (NumArray *)lua_newuserdata(L, nbytes);
luaL_getmetatable(L, "LuaBook.array");
lua_setmetatable(L, -2);
a->size = n;
return 1; /* new userdatum is already on the stack */
}
Unfortunately the *luaL_getmetatable* Function is marked al old at my header File and commented out. I tried to activate it again but as expected I will get an error because the dll entrancepoint couldn't be found.
This is the Delphi-translation of that example (using another non array datatype)
Type
tMyType = tWhatever;
pMyType = ^tMyType;
{...}
Function newusertype(aState : pLua_State) : LongInt; cdecl;
Var
NewData : pMyType;
Begin
Result := 0;
NewData := lua_newuserdata(aState, SizeOf(tMyType ));
NewData^ := GetInitValue;
luaL_getMetaTable(aState, 'myexcample.mytype'); // Error/unknown function
lua_setmetatable(aState, -2);
Result := 1;
End;
Now I'm looking for an replacement of luaL_getMetaTable. I haven't found any information about one. In fact I haven't found any information that luaL_getMetaTable is outdated but it seems to be :(.
use lua_newmetatable(aState, 'myexample.mytype'). The thing is (if you only want to continue if the metatable already exists) you'll need to evaluate whether it returns a 0! If it returns 0, then it's wanting to create the metatable... in which case you can lua_pop(aState, 1).
Just remember that lua_newmetatable is a function returning an Integer (which in reality should be a Boolean).
Otherwise you can wait a few weeks for me to release Lua4Delphi version 2, which makes all of this super easy (and the Professional version actually automates the registration of Delphi Types and Instances with Lua)

Resources