Amiga-Development

Please login or register.

Login with username, password and session length
Advanced search  

News:

Created for developers of all Amiga camps

Author Topic: Arrays, Strings and Random string values  (Read 554 times)

0 Members and 1 Guest are viewing this topic.

Zendarion

  • Newbie
  • *
  • Posts: 7
    • View Profile
Arrays, Strings and Random string values
« on: February 23, 2017, 05:24:06 PM »

So this is what I am after this time. I am new at PortablE and may have basic questions.

Define a string
A string value
An array
Populate the array with random string values

I think I am a bit confused, for now, when it comes to arrays and PortablE. By the way, the hint you gave me on the other thread was great. Worked perfectly and in a way everything seems so obvious when I see it like that.

The code is not mine but I would do it like this in C# and now I would like to have a go at it in PortablE

private static string CreateRandomPassword(int passwordLength)
{
 string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
 char[] chars = new char[passwordLength];
 Random rd = new Random();

 for (int i = 0; i < passwordLength; i++)
 {
  chars = allowedChars[rd.Next(0, allowedChars.Length)];
 }
 return new string(chars);
}

// Hans
Logged

ChrisH

  • Moderator
  • Newbie
  • *****
  • Posts: 32
    • View Profile
Re: Arrays, Strings and Random string values
« Reply #1 on: February 24, 2017, 10:49:04 AM »

Define a string
A string value
An array
Populate the array with random string values
PortablE is heavily based on AmigaE (but not fully compatible unless the compatibility mode is used), so some AmigaE guides might help:
http://cshandley.co.uk/amigae/ch_9b.html (this fully applies to PortablE, except that "String()" must be changed to "NewString()".)
http://cshandley.co.uk/amigae/ch_8e.html

http://cshandley.co.uk/amigae/ch_8d.html (note that [100]:ARRAY is not allowed, as it has a different meaning in PortablE, so it needs a type like [100]:ARRAY OF CHAR)
http://cshandley.co.uk/amigae/ch_4g.html (note that "[]" cannot be used as "[ 0 ]", unless OPT POINTER is used)

Jason Hulance wrote a good introduction to AmigaE, some of which you might find helpful:
http://cshandley.co.uk/JasonHulance/beginner_171.html (this should be really great for someone coming from C# or BASIC)
http://cshandley.co.uk/JasonHulance/beginner_130.html (beware that some of his suggestions are no-longer the best way to do it in PortablE, but they should still work)

If some of his code doesn't compile with PortablE, try adding OPT POINTER or else OPT AMIGAE to the top of the program.
* OPT POINTER allows ARRAY/STRING to be treated as PTR (and vice versa).  But without this, arrays & strings are still pointers underneath, and so behave much like pointers (even if their intended usage is slightly different to PTR).
* OPT AMIGAE completely disables all type-checking, and adds back a few old-style things that I removed/renamed.
* e.g. OPT AMIGAE will be needed where he uses WriteF(), because PortablE replaced this by Print().
* Note that his example code "You probably don't want to do any of these" are normally detected as illegal by PortablE, although OPT AMIGAE will probably still allow them to compile!

Quote
I think I am a bit confused, for now, when it comes to arrays and PortablE.
E (PortablE & AmigaE) has a memory, array & string model very similar to that used by C, except that E has more & better string functionality built-in (basically what are called "e-strings").  I believe this is very different to C#, as that uses garbage collection & other stuff that radically changes how memory is handled.
« Last Edit: February 25, 2017, 10:58:19 AM by ChrisH »
Logged

ChrisH

  • Moderator
  • Newbie
  • *****
  • Posts: 32
    • View Profile
Re: Arrays, Strings and Random string values
« Reply #2 on: February 24, 2017, 11:06:28 AM »

I've converted the procedure as literally as I could:
Code: [Select]
PROC createRandomPassword(passwordLength) RETURNS chars:STRING
DEF allowedChars:ARRAY OF CHAR, allowedCharsLen, i

allowedChars := 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-'
allowedCharsLen := StrLen(allowedChars)

NEW chars[passwordLength]
FOR i := 0 TO passwordLength-1 DO chars[i] := allowedChars[Rnd(allowedCharsLen)]

SetStr(chars, passwordLength) ->very important!
FINALLY
IF exception THEN END chars
ENDPROC

Let me know if you have any questions about anything I've done.

You can test it with this:
Code: [Select]
MODULE 'std/pTime'

PROC main()
DEF pass:STRING

->give random number generator a unique seed
Rnd(-Abs(CurrentTime()!!VALUE))

->first test call
pass := createRandomPassword(5)
Print('pass = "\s"\n', pass)
END pass

->second test call
pass := createRandomPassword(5)
Print('pass = "\s"\n', pass)
END pass
FINALLY
PrintException()

END pass
ENDPROC

By the way, if you want to convert an ASCII integer value directly into a CHAR value, then you must use the UnsignedToChar() procedure.  (This is because the C/C++ standard allows compilers to arbitrarily choose whether CHAR is a signed or unsigned value, and since PortablE generates C++ code, it has to detect the signedness of the compiler at run-time & convert appropriately.)
« Last Edit: February 24, 2017, 11:14:45 AM by ChrisH »
Logged

Zendarion

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Arrays, Strings and Random string values
« Reply #3 on: March 17, 2017, 02:32:07 PM »

So here I am again with another question.

How can I copy a, for example, win.addText() value to clipboard? (is it called that?)

Press Button
Copy value from win.addText()

THEN Being able to Paste that value to Notepad or wherever I want.

Where in the manual can I find information about this or do you or anyone else have an example to show?

// Hans
Logged

ChrisH

  • Moderator
  • Newbie
  • *****
  • Posts: 32
    • View Profile
Re: Arrays, Strings and Random string values
« Reply #4 on: March 17, 2017, 06:00:25 PM »

How can I copy a, for example, win.addText() value to clipboard? (is it called that?)
...
Where in the manual can I find information about this or do you or anyone else have an example to show?
This is something I've never even done myself, although I do occasionally wish that I could select such text (for copying to the clipboard).  It should be POSSIBLE to do what you want with PortablE, since I have ported the AmigaOS module 'devices/clipboard' ...  However, it appears to be a very low-level interface, and so will probably seem quite complicated to most programmers (unless they are familiar with sending/receiving Exec messages to/from devices).

I could probably work out how to do it myself, but I don't have the time at the moment to do so, I am afraid.
Logged

magorium

  • Full Member
  • ***
  • Posts: 200
  • Programming is an art form that fights back
    • View Profile
Re: Arrays, Strings and Random string values
« Reply #5 on: March 17, 2017, 06:10:16 PM »

For sure, it should be possible.

In case PortablE does not have native support then you could always try to write a helper module yourself in case ChrisH currently doesn't has time.

There is an example written in c-language here on how to use the clipboard, and indeed it is very lowlevel.

Sorry to say that my experience with portablE is next to none, so i can't help there either but perhaps Zendarion is able to make use of the example.
« Last Edit: March 17, 2017, 06:19:12 PM by magorium »
Logged

ChrisH

  • Moderator
  • Newbie
  • *****
  • Posts: 32
    • View Profile
Re: Arrays, Strings and Random string values
« Reply #6 on: March 17, 2017, 06:39:12 PM »

@magorium
Thanks for the C code link.  It should be fairly straight forward (for me anyway) to translate that to PortablE code, although would still require some time to do it...

@Zendarion
Copying the contents of some text boxes to the clipboard would definitely be useful for eNewsReader, so it's something I'd like to look into at some point.
Logged

ChrisH

  • Moderator
  • Newbie
  • *****
  • Posts: 32
    • View Profile
Re: Arrays, Strings and Random string values
« Reply #7 on: March 18, 2017, 10:12:46 AM »

I discovered that C code is actually a standard Amiga RKM example, which already has an AmigaE version available.  So adapting it to PortablE should be pretty easy, when I get a chance.  (Although AmigaE code in general is very easily used, low-level AmigaOS code like this tends to require adding various bits to satisfy PortablE's type-checking (which AmigaE doesn't have).)
« Last Edit: March 19, 2017, 11:44:30 AM by ChrisH »
Logged

ChrisH

  • Moderator
  • Newbie
  • *****
  • Posts: 32
    • View Profile
Re: Arrays, Strings and Random string values
« Reply #8 on: March 19, 2017, 03:58:32 PM »

Please find attached the 'cbio' module, as well as the shell program 'clipdemo' which demonstrates usage of that module.

While I spent some time cleaning-up the code, it is still a very direct translation of the AmigaE version, which was a fairly direct translation of the C version (except it makes intelligent use of E's exception system).  Most probably I will improve on this code more myself later, for my own uses, but it seems to work as it stands...
Logged