Author Topic: How to quote the code?  (Read 3639 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
How to quote the code?
« on: July 16, 2021, 12:25:02 pm »
One more problem to solve!
When my program will be finished, it will be spreaded as executable (.exe), without .bas file, on computers without QB64 intallated in. But I want that the executable file create also a .txt file in which there is the code. Practically, I want that the .exe file can create a .txt file that is the same as the original .bas file. Is it possible?

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: How to quote the code?
« Reply #1 on: July 16, 2021, 01:26:22 pm »
Simply convert the entire .bas file into DATAs and include it at the end of your program.

Here is a FileToDATA converter:
https://www.qb64.org/forum/index.php?topic=790.msg6907#msg6907

it will also create a ready to use writeback function, which you just need to call given with your desired filename.

BTW - In your described case make sure you convert your .bas file to DATAs before adding the $INCLUDE line at the end which embeds the .bas file DATAs.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: How to quote the code?
« Reply #2 on: July 16, 2021, 01:36:55 pm »
I tried to find your LZW library, but the links do not show it.

Also, is this Windows specific?
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

FellippeHeitor

  • Guest
Re: How to quote the code?
« Reply #3 on: July 16, 2021, 02:21:00 pm »
Recent versions of QB64 have zlib builtin if you guys ever need it. See _Inflate$ and _Deflate$.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: How to quote the code?
« Reply #4 on: July 16, 2021, 02:51:29 pm »
I tried to find your LZW library, but the links do not show it.

Also, is this Windows specific?

@George McGinn
Ah yes the library has been moved into my Libraries Collection, which you can download from the Bonus Stuff section here:
https://www.qb64.org/forum/index.php?topic=809.msg100182#msg100182

No, all libs in the collection are platform independent (hopefully...)
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: How to quote the code?
« Reply #5 on: July 16, 2021, 03:21:20 pm »
Thanks, I'll give it a try.

@George McGinn
Ah yes the library has been moved into my Libraries Collection, which you can download from the Bonus Stuff section here:
https://www.qb64.org/forum/index.php?topic=809.msg100182#msg100182

No, all libs in the collection are platform independent (hopefully...)
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: How to quote the code?
« Reply #6 on: July 17, 2021, 11:01:15 am »
Simply convert the entire .bas file into DATAs and include it at the end of your program.

Here is a FileToDATA converter:
https://www.qb64.org/forum/index.php?topic=790.msg6907#msg6907

it will also create a ready to use writeback function, which you just need to call given with your desired filename.

BTW - In your described case make sure you convert your .bas file to DATAs before adding the $INCLUDE line at the end which embeds the .bas file DATAs.

But $include includes a file into the the program.  _deflate$ compresses a string. In both cases I am not sure that they are usefull for my need. Yes, maybe I can make a file copy of the code and than load the code of the copy into the original code as a string, _deflate it, and finally _inflate it and print it as output in file when the program is run.
Or maybe it is also possible to make a code that read it self, than load it into a string and finally print it as output in another file when the program is run. But in all these cases, a text file at least is need. But at the end, the final user of the program, will have only the EXE file, no code. So, if I make a code that read itself in order to load it into a string, I will finally have an EXE file that, when launched, will try to make the same, but no text file will be present to be read. So, the code need somehow to be nested in itself. As to say that into the program, there must be a string (maybe _deflated) that contains the code and that can be printed in a file as output when the program is run by the EXE file. Furthermore, I suppose that this string containing the code, has to not contain itself, if not the program becomes a Chinese box.
sorry if maybe I didn't understand correctly what you said.

Marked as best answer by bartok on July 17, 2021, 09:39:35 pm

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: How to quote the code?
« Reply #7 on: July 17, 2021, 12:07:07 pm »
But $include includes a file into the the program.  _deflate$ compresses a string. In both cases I am not sure that they are usefull for my need.

Then it's often the best not thinking so much about it and instead give it a simple try:

Get the next two codeboxes and save it in your QB64 folder:

Save as: QMC.bas
Code: QB64: [Select]
  1. _TITLE "Quote My Code"
  2.  
  3. PRINT "This will demonstrate, how you can embed the code"
  4. PRINT "and write it back into a .txt/.bas or whatever."
  5. PRINT "Please type the file name to write the code to:"
  6. LINE INPUT "File: ", f$
  7. PRINT "Thank you, the code for this demo was successfully"
  8. PRINT "written into "; WriteQMCData$(f$)
  9.  
  10. '$INCLUDE: 'QMC.bm'
  11.  
  12.  

Save as: QMC.bm
Code: QB64: [Select]
  1. '============================================================
  2. '=== This file was created with MakeDATA.bas by RhoSigma, ===
  3. '=== you must $INCLUDE this at the end of your program.   ===
  4. '============================================================
  5.  
  6. '=====================================================================
  7. 'Function to write the embedded DATAs back to disk. Call this FUNCTION
  8. 'once, before you will access the represented file for the first time.
  9. 'After the call always use the returned realFile$ ONLY to access the
  10. 'written file, as the filename was maybe altered in order to avoid the
  11. 'overwriting of an existing file of the same name in the given location.
  12. '---------------------------------------------------------------------
  13. 'SYNTAX: realFile$ = WriteQMCData$ (wantFile$)
  14. '
  15. 'INPUTS: wantFile$ --> The filename you would like to write the DATAs
  16. '                      to, can contain a full or relative path.
  17. '
  18. 'RESULT: realFile$ --> On success the path and filename finally used
  19. '                      after applied checks, use ONLY this returned
  20. '                      name to access the file.
  21. '                   -> On failure this FUNCTION will panic with the
  22. '                      appropriate ERROR code, you may handle this as
  23. '                      needed with your own ON ERROR GOTO... handler.
  24. '=====================================================================
  25. FUNCTION WriteQMCData$ (file$)
  26. '--- separate filename body & extension ---
  27. FOR po% = LEN(file$) TO 1 STEP -1
  28.     IF MID$(file$, po%, 1) = "." THEN
  29.         body$ = LEFT$(file$, po% - 1)
  30.         ext$ = MID$(file$, po%)
  31.         EXIT FOR
  32.     ELSEIF MID$(file$, po%, 1) = "\" OR MID$(file$, po%, 1) = "/" OR po% = 1 THEN
  33.         body$ = file$
  34.         ext$ = ""
  35.         EXIT FOR
  36.     END IF
  37. NEXT po%
  38. '--- avoid overwriting of existing files ---
  39. num% = 1
  40.     file$ = body$ + "(" + LTRIM$(STR$(num%)) + ")" + ext$
  41.     num% = num% + 1
  42. '--- write DATAs ---
  43. READ numL&, numB&
  44. rawdata$ = SPACE$((numL& * 4) + numB&)
  45. stroffs& = 1
  46. FOR i& = 1 TO numL&
  47.     READ dat&
  48.     MID$(rawdata$, stroffs&, 4) = MKL$(dat&)
  49.     stroffs& = stroffs& + 4
  50. NEXT i&
  51. IF numB& > 0 THEN
  52.     FOR i& = 1 TO numB&
  53.         READ dat&
  54.         MID$(rawdata$, stroffs&, 1) = CHR$(dat&)
  55.         stroffs& = stroffs& + 1
  56.     NEXT i&
  57. ff% = FREEFILE
  58. OPEN file$ FOR OUTPUT AS ff%
  59. PRINT #ff%, rawdata$;
  60. CLOSE ff%
  61. '--- set result ---
  62. WriteQMCData$ = file$
  63.  
  64. '--- DATAs representing the contents of file QMC.bas
  65. '---------------------------------------------------------------------
  66. QMC:
  67. DATA 88,20
  68. DATA &H5449545F,&H2220454C,&H746F7551,&H794D2065,&H646F4320,&H0A0D2265,&H52500A0D,&H20544E49
  69. DATA &H69685422,&H69772073,&H64206C6C,&H6E6F6D65,&H61727473,&H202C6574,&H20776F68,&H20756F79
  70. DATA &H206E6163,&H65626D65,&H68742064,&H6F632065,&H0D226564,&H4952500A,&H2220544E,&H20646E61
  71. DATA &H74697277,&H74692065,&H63616220,&H6E69206B,&H61206F74,&H78742E20,&H622E2F74,&H6F207361
  72. DATA &H68772072,&H76657461,&H222E7265,&H52500A0D,&H0D544E49,&H4952500A,&H2220544E,&H61656C50
  73. DATA &H74206573,&H20657079,&H20656874,&H656C6966,&H6D616E20,&H6F742065,&H69727720,&H74206574
  74. DATA &H63206568,&H2065646F,&H223A6F74,&H494C0A0D,&H4920454E,&H5455504E,&H69462220,&H203A656C
  75. DATA &H66202C22,&H500A0D24,&H544E4952,&H52500A0D,&H20544E49,&H61685422,&H79206B6E,&H202C756F
  76. DATA &H20656874,&H65646F63,&H726F6620,&H69687420,&H65642073,&H77206F6D,&H73207361,&H65636375
  77. DATA &H75667373,&H22796C6C,&H52500A0D,&H20544E49,&H69727722,&H6E657474,&H746E6920,&H3B22206F
  78. DATA &H69725720,&H4D516574,&H74614443,&H66282461,&H0A0D2924,&H4E495250,&H0D0A0D54,&H4924270A
  79. DATA &H4E,&H43,&H4C,&H55,&H44,&H45,&H3A,&H20,&H27,&H51,&H4D,&H43,&H2E,&H62,&H6D,&H27
  80. DATA &H0D,&H0A,&H0D,&H0A
  81.  
  82.  

1.) Now open the QB64 IDE, load QMC.bas and just make EXE (F11), don't actually run it.
2.) Take the EXE and move it into any other place, eg. Downloads, My Files etc. (this shall imitate the distribution of your EXE to sombody else)
3.) Double click the EXE (in its new location), give the file name QMC.txt when ask for it.
4.) Check the contents of the just written QMC.txt, it should be the same contents as QMC.bas in your QB64 folder.

This is for my understanding exactly what you've ask for in your first post.
If this is otherwise not what you want, then you need to refine the description of your problem.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: How to quote the code?
« Reply #8 on: July 17, 2021, 02:22:45 pm »
Hi Bartok,
Hi guys and gals
here my rough solution to the question of Bartok
.....
STOP a moment, this request let me remember the task of writing a code self reproducing....
1. if you put into codefile an '$include:'namefile' you get a compiler error for looping $Include (maybe there is a solution managing line by line the copy of code excluding the $include line of code)
2. if you put DATA statement you must READ it data marking them with a starting label. But DATA statements doesn't support multiple string delimitators on the line and doesn't support  go ahead of text in one DATA statement...
(maybe it is possible managing line by line the copy of code adding for each line a DATA statement)
3. write code that adds itself to the end of file like code, then compile againg the code...
and here we go against a feature of QB64 IDE /Parser... the autocompleting for string delimitator...(maybe it is possible if I can put off the feature of autocompleting string delimitator "

LOL 3 wrong ways to suggest a solution
Good Luck
Programming isn't difficult, only it's  consuming time and coffee

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: How to quote the code?
« Reply #9 on: July 18, 2021, 01:40:27 am »
Then it's often the best not thinking so much about it and instead give it a simple try:

Get the next two codeboxes and save it in your QB64 folder:

Save as: QMC.bas
Code: QB64: [Select]
  1. _TITLE "Quote My Code"
  2.  
  3. PRINT "This will demonstrate, how you can embed the code"
  4. PRINT "and write it back into a .txt/.bas or whatever."
  5. PRINT "Please type the file name to write the code to:"
  6. LINE INPUT "File: ", f$
  7. PRINT "Thank you, the code for this demo was successfully"
  8. PRINT "written into "; WriteQMCData$(f$)
  9.  
  10. '$INCLUDE: 'QMC.bm'
  11.  
  12.  

Save as: QMC.bm
Code: QB64: [Select]
  1. '============================================================
  2. '=== This file was created with MakeDATA.bas by RhoSigma, ===
  3. '=== you must $INCLUDE this at the end of your program.   ===
  4. '============================================================
  5.  
  6. '=====================================================================
  7. 'Function to write the embedded DATAs back to disk. Call this FUNCTION
  8. 'once, before you will access the represented file for the first time.
  9. 'After the call always use the returned realFile$ ONLY to access the
  10. 'written file, as the filename was maybe altered in order to avoid the
  11. 'overwriting of an existing file of the same name in the given location.
  12. '---------------------------------------------------------------------
  13. 'SYNTAX: realFile$ = WriteQMCData$ (wantFile$)
  14. '
  15. 'INPUTS: wantFile$ --> The filename you would like to write the DATAs
  16. '                      to, can contain a full or relative path.
  17. '
  18. 'RESULT: realFile$ --> On success the path and filename finally used
  19. '                      after applied checks, use ONLY this returned
  20. '                      name to access the file.
  21. '                   -> On failure this FUNCTION will panic with the
  22. '                      appropriate ERROR code, you may handle this as
  23. '                      needed with your own ON ERROR GOTO... handler.
  24. '=====================================================================
  25. FUNCTION WriteQMCData$ (file$)
  26. '--- separate filename body & extension ---
  27. FOR po% = LEN(file$) TO 1 STEP -1
  28.     IF MID$(file$, po%, 1) = "." THEN
  29.         body$ = LEFT$(file$, po% - 1)
  30.         ext$ = MID$(file$, po%)
  31.         EXIT FOR
  32.     ELSEIF MID$(file$, po%, 1) = "\" OR MID$(file$, po%, 1) = "/" OR po% = 1 THEN
  33.         body$ = file$
  34.         ext$ = ""
  35.         EXIT FOR
  36.     END IF
  37. NEXT po%
  38. '--- avoid overwriting of existing files ---
  39. num% = 1
  40.     file$ = body$ + "(" + LTRIM$(STR$(num%)) + ")" + ext$
  41.     num% = num% + 1
  42. '--- write DATAs ---
  43. READ numL&, numB&
  44. rawdata$ = SPACE$((numL& * 4) + numB&)
  45. stroffs& = 1
  46. FOR i& = 1 TO numL&
  47.     READ dat&
  48.     MID$(rawdata$, stroffs&, 4) = MKL$(dat&)
  49.     stroffs& = stroffs& + 4
  50. NEXT i&
  51. IF numB& > 0 THEN
  52.     FOR i& = 1 TO numB&
  53.         READ dat&
  54.         MID$(rawdata$, stroffs&, 1) = CHR$(dat&)
  55.         stroffs& = stroffs& + 1
  56.     NEXT i&
  57. ff% = FREEFILE
  58. OPEN file$ FOR OUTPUT AS ff%
  59. PRINT #ff%, rawdata$;
  60. CLOSE ff%
  61. '--- set result ---
  62. WriteQMCData$ = file$
  63.  
  64. '--- DATAs representing the contents of file QMC.bas
  65. '---------------------------------------------------------------------
  66. QMC:
  67. DATA 88,20
  68. DATA &H5449545F,&H2220454C,&H746F7551,&H794D2065,&H646F4320,&H0A0D2265,&H52500A0D,&H20544E49
  69. DATA &H69685422,&H69772073,&H64206C6C,&H6E6F6D65,&H61727473,&H202C6574,&H20776F68,&H20756F79
  70. DATA &H206E6163,&H65626D65,&H68742064,&H6F632065,&H0D226564,&H4952500A,&H2220544E,&H20646E61
  71. DATA &H74697277,&H74692065,&H63616220,&H6E69206B,&H61206F74,&H78742E20,&H622E2F74,&H6F207361
  72. DATA &H68772072,&H76657461,&H222E7265,&H52500A0D,&H0D544E49,&H4952500A,&H2220544E,&H61656C50
  73. DATA &H74206573,&H20657079,&H20656874,&H656C6966,&H6D616E20,&H6F742065,&H69727720,&H74206574
  74. DATA &H63206568,&H2065646F,&H223A6F74,&H494C0A0D,&H4920454E,&H5455504E,&H69462220,&H203A656C
  75. DATA &H66202C22,&H500A0D24,&H544E4952,&H52500A0D,&H20544E49,&H61685422,&H79206B6E,&H202C756F
  76. DATA &H20656874,&H65646F63,&H726F6620,&H69687420,&H65642073,&H77206F6D,&H73207361,&H65636375
  77. DATA &H75667373,&H22796C6C,&H52500A0D,&H20544E49,&H69727722,&H6E657474,&H746E6920,&H3B22206F
  78. DATA &H69725720,&H4D516574,&H74614443,&H66282461,&H0A0D2924,&H4E495250,&H0D0A0D54,&H4924270A
  79. DATA &H4E,&H43,&H4C,&H55,&H44,&H45,&H3A,&H20,&H27,&H51,&H4D,&H43,&H2E,&H62,&H6D,&H27
  80. DATA &H0D,&H0A,&H0D,&H0A
  81.  
  82.  

1.) Now open the QB64 IDE, load QMC.bas and just make EXE (F11), don't actually run it.
2.) Take the EXE and move it into any other place, eg. Downloads, My Files etc. (this shall imitate the distribution of your EXE to sombody else)
3.) Double click the EXE (in its new location), give the file name QMC.txt when ask for it.
4.) Check the contents of the just written QMC.txt, it should be the same contents as QMC.bas in your QB64 folder.

This is for my understanding exactly what you've ask for in your first post.
If this is otherwise not what you want, then you need to refine the description of your problem.

Exactly! It is what I need. Thank's you!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: How to quote the code?
« Reply #10 on: July 18, 2021, 06:28:41 am »
Well, that's good.

The only remaining question for me is why you wanna make it so complicated. If the EXE is able to write out its original code, then you seem to have no problem that other people get the source code. So why not simply spreading the .bas file together with the EXE directly?
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: How to quote the code?
« Reply #11 on: July 19, 2021, 03:03:37 am »
Well, that's good.

The only remaining question for me is why you wanna make it so complicated. If the EXE is able to write out its original code, then you seem to have no problem that other people get the source code. So why not simply spreading the .bas file together with the EXE directly?

Basically, because I don't want to give too much importance to the code. I don't want to spread 2 files, that can be separeted by someone, but one only one file, that, when launched, it makes many output files, including the code. Further more, I was curious to see if it was possible, and, if so, if it were in my ability to make it.

But I have to admit that this, and the trasformation of an image& to BMP file, are ato the moment both out of my skills, so I will use yours subroutines,  quoting them.
« Last Edit: July 19, 2021, 03:05:10 am by bartok »

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: How to quote the code?
« Reply #12 on: July 19, 2021, 05:32:03 am »
No problem, was just curious about your motive behind it.

BTW - when using the MakeDATA.bas program to convert your final code into that .bm file containing the DATAs, then make sure that you also get and incude the lzwpacker.bm mentioned.

If you don't want to include that extra file, then you can also easily change the MakeDATA.bas code to use the QB64 internal _DEFLATE$() and _INFLATE$() instead:

look for the line:
Code: QB64: [Select]
  1. rawdata$ = LzwPack$(filedata$, 20)
change it to:
Code: QB64: [Select]
  1. rawdata$ = _DEFLATE$(filedata$)

and further look for:
Code: QB64: [Select]
  1.     PRINT #2, "filedata$ = LzwUnpack$(rawdata$)"
and change it to:
Code: QB64: [Select]
  1.     PRINT #2, "filedata$ = _INFLATE$(rawdata$)"
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: How to quote the code?
« Reply #13 on: July 19, 2021, 06:52:25 am »
No problem, was just curious about your motive behind it.

BTW - when using the MakeDATA.bas program to convert your final code into that .bm file containing the DATAs, then make sure that you also get and incude the lzwpacker.bm mentioned.

If you don't want to include that extra file, then you can also easily change the MakeDATA.bas code to use the QB64 internal _DEFLATE$() and _INFLATE$() instead:

look for the line:
Code: QB64: [Select]
  1. rawdata$ = LzwPack$(filedata$, 20)
change it to:
Code: QB64: [Select]
  1. rawdata$ = _DEFLATE$(filedata$)

and further look for:
Code: QB64: [Select]
  1.     PRINT #2, "filedata$ = LzwUnpack$(rawdata$)"
and change it to:
Code: QB64: [Select]
  1.     PRINT #2, "filedata$ = _INFLATE$(rawdata$)"

ok, I do that. Thank's.