* * *

Author Topic: [SOLVED] How to load a list of strings without shipping a text file  (Read 372 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 602
    • http://www.quickhash-gui.org
Hi

I have a VERY long list of strings (about 2K) that I need my program to be able to search and lookup. I don't want to have ship a text file with the exe...I want the data to be compiled into the exe.

e.g.
Code: [Select]
Africa
Iceland
Ireland
Norway
UK
....
Zimbabwe

So far, I just have all the values in a text file and my program is reading the text file by loading it into a stringlist.loadfromfile, then I am using sl.index to search it and so on. How can I achieve the same thing except ensuring that the values are all in the sourcode? I seem to think an "inc" file might be needed or something but I seem to think that is for binary assembly etc, not strings?  I don't even know what the term is to achieve what I am doing so don't really know how to search for the answer!
« Last Edit: October 13, 2017, 01:09:37 am by Gizmo »
Lazarus 1.6.4 and fpc 3.0.2 - Linux Mint 17 LTS, Windows 7 and Mac Yosemite
Useful Page to remember : http://wiki.freepascal.org/Cross_compiling#From_Linux_x64_to_Linux_i386

Fungus

  • Sr. Member
  • ****
  • Posts: 330
Re: How to load a list of strings without shipping a text file
« Reply #1 on: October 12, 2017, 07:54:00 pm »

M+AUDIO

  • New member
  • *
  • Posts: 37
Re: How to load a list of strings without shipping a text file
« Reply #2 on: October 12, 2017, 07:55:10 pm »
Hi,
I'm doing such thing this way:
file charencslaz.rc:
Code: [Select]
charencslaz RCDATA charencslaz.txt
file charencslaz.txt:
Code: [Select]
UTF-16
UTF-8
...
ISO-8859-15

the unit of my program:
Code: Pascal  [Select]
  1. unit uCharEnc;
  2. ...
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   ...LCLType...;
  9.  
  10. type
  11.  
  12.   { TSBCharEnc }
  13.  
  14.   TSBCharEnc = class(TForm)
  15.     EncodingName: TComboBox;
  16.     ...
  17.   end;  
  18. ...
  19.  
  20. implementation
  21.  
  22. {$R *.lfm}
  23. {$R charencslaz.rc}
  24.  
  25. ...
  26. procedure TSBCharEnc.FormShow(Sender: TObject);
  27. var
  28.   rs: TResourceStream;
  29.   ...
  30. begin
  31.   ...
  32.   rs := TResourceStream.Create(HInstance, 'charencslaz', RT_RCDATA);
  33.   try
  34.     EncodingName.Items.LoadFromStream(rs); //TComboBox.Items is of type TStrings so TStringList also has it.
  35.     ...
  36.   finally
  37.     rs.Free;
  38.   end;
  39. end;
  40.  
  41.  

adapt it to your needs. if you are on linux you need windres.
« Last Edit: October 12, 2017, 09:08:10 pm by M+AUDIO »

Munair

  • Full Member
  • ***
  • Posts: 173
  • Keep it simple.
    • Ditrianum
Re: How to load a list of strings without shipping a text file
« Reply #3 on: October 12, 2017, 07:58:09 pm »
Lazarus 1.6.2, FPC 3.0.0; Debian 9 KDE 5.8.6 x64; Windows 7 x64

Gizmo

  • Hero Member
  • *****
  • Posts: 602
    • http://www.quickhash-gui.org
Re: How to load a list of strings without shipping a text file
« Reply #4 on: October 12, 2017, 09:53:39 pm »
MAUDIO...thanks for the tip. I followed up your example in the wiki under the section 'Adding resources to your program', as described here http://wiki.freepascal.org/Lazarus_Resources#FPC_resources.

I'm intrigued as to why Windows API includes RT_STRING (String-table entry) when I couldn't get that value to work with a text file of strings. So I went with RT_RCDATA too and that worked. Maybe I was doing something wrong.

Anyway, for Windows, this is a great method. But I can forsee problems when I try to cross compile for Linux and OSX! I know it says "On Linux/FreeBSD/OSX, you might need to make sure you have the windres resource compiler," but I can see that not been straight forward.

So if there's another way to do this, even if it's making some enormous string array or something (TStringArray?) that is kept in a seperate pas file, I'd rather do that. Any ideas?
Lazarus 1.6.4 and fpc 3.0.2 - Linux Mint 17 LTS, Windows 7 and Mac Yosemite
Useful Page to remember : http://wiki.freepascal.org/Cross_compiling#From_Linux_x64_to_Linux_i386

jack616

  • Sr. Member
  • ****
  • Posts: 261
Re: How to load a list of strings without shipping a text file
« Reply #5 on: October 12, 2017, 10:29:43 pm »

So if there's another way to do this, even if it's making some enormous string array or something (TStringArray?) that is kept in a seperate pas file, I'd rather do that. Any ideas?

Something along these lines maybe?
(cut and paste from here - I've included the country codes in case you need those too.)
No need for separate files by the sound of things - just pop this in as part of your project.

john (not jack)
---------------------

Code: Pascal  [Select]
  1.  
  2. countries:  TStringlist;
  3.  
  4.  
  5. countries := TStringlist.Create;
  6. with countries do
  7. begin
  8.   add('ac,Ascension Island');
  9.   add('ad,Andorra');
  10.   add('ae,United Arab Emirates');
  11.   add('af,Afghanistan');
  12.   add('ag,Antigua and Barbuda');
  13.   add('ai,Anguilla');
  14.   add('al,Albania');
  15.   add('am,Armenia');
  16.   add('an,Netherlands Antilles');
  17.   add('ao,Angola');
  18.   add('aq,Antarctica');
  19.   add('ar,Argentina');
  20.   add('as,American Samoa');
  21.   add('at,Austria');
  22.   add('au,Australia');
  23.   add('aw,Aruba');
  24.   add('ax,Aland Islands');
  25.   add('az,Azerbaijan');
  26.   add('ba,Bosnia-Herzegovina');
  27.   add('bb,Barbados');
  28.   add('bd,Bangladesh');
  29.   add('be,Belgium');
  30.   add('bf,Burkina Faso');
  31.   add('bg,Bulgaria');
  32.   add('bh,Bahrain');
  33.   add('bi,Burundi');
  34.   add('bj,Benin');
  35.   add('bm,Bermuda');
  36.   add('bn,Brunei Darussalam');
  37.   add('bo,Bolivia');
  38.   add('br,Brazil');
  39.   add('bs,Bahamas');
  40.   add('bt,Bhutan');
  41.   add('bv,Bouvet Island');
  42.   add('bw,Botswana');
  43.   add('by,Belarus');
  44.   add('bz,Belize');
  45.   add('ca,Canada');
  46.   add('cc,Cocos (Keeling) Islands');
  47.   add('cd,Democratic Republic of Congo');
  48.   add('cf,Central African Republic');
  49.   add('cg,Republic of Congo');
  50.   add('ch,Switzerland');
  51.   add('ci,Cote d`Ivoire');
  52.   add('ck,Cook Islands');
  53.   add('cl,Chile');
  54.   add('cm,Cameroon');
  55.   add('cn,China');
  56.   add('co,Colombia');
  57.   add('cr,Costa Rica');
  58.   add('cs,Serbia - Montenegro');
  59.   add('cu,Cuba');
  60.   add('cv,Cape Verde');
  61.   add('cx,Christmas Island');
  62.   add('cy,Cyprus');
  63.   add('cz,Czech Republic');
  64.   add('de,Germany');
  65.   add('dj,Djibouti');
  66.   add('dk,Denmark');
  67.   add('dm,Dominica');
  68.   add('do,Dominican Republic');
  69.   add('dz,Algeria');
  70.   add('ec,Ecuador');
  71.   add('ee,Estonia');
  72.   add('eg,Egypt');
  73.   add('eh,Western Sahara');
  74.   add('er,Eritrea');
  75.   add('es,Spain');
  76.   add('et,Ethiopia');
  77.   add('eu,European Union');
  78.   add('fi,Finland');
  79.   add('fj,Fiji');
  80.   add('fk,Falkland Islands (British Territories)');
  81.   add('fm,Federal State of Micronesia');
  82.   add('fo,Faroe Islands');
  83.   add('fr,France');
  84.   add('ga,Gabon');
  85.  // add('gb,Great Britain');
  86.   add('gd,Grenada');
  87.   add('ge,Georgia');
  88.   add('gf,French Guiana');
  89.   add('gg,Guernsey');
  90.   add('gh,Ghana');
  91.   add('gi,Gibraltar');
  92.   add('gl,Greenland');
  93.   add('gm,Gambia');
  94.   add('gn,Guinea');
  95.   add('gp,Guadeloupe');
  96.   add('gq,Equatorial Guinea');
  97.   add('gr,Greece');
  98.   add('gs,South Georgia and the South Sandwich Islands');
  99.   add('gt,Guatemala');
  100.   add('gu,Guam');
  101.   add('gw,Guinea-Bissau');
  102.   add('gy,Guyana');
  103.   add('hk,Hong Kong');
  104.   add('hm,Heard and McDonald Islands');
  105.   add('hn,Honduras');
  106.   add('hr,Croatia-Hrvatska');
  107.   add('ht,Haiti');
  108.   add('hu,Hungary');
  109.   add('id,Indonesia');
  110.   add('ie,Ireland');
  111.   add('il,Israel');
  112.   add('im,Isle of Man');
  113.   add('in,India');
  114.   add('io,British Indian Ocean Territory');
  115.   add('iq,Iraq');
  116.   add('ir,Islamic Republic of Iran');
  117.   add('is,Iceland');
  118.   add('it,Italy');
  119.   add('je,Jersey');
  120.   add('jm,Jamaica');
  121.   add('jo,Jordan');
  122.   add('jp,Japan');
  123.   add('ke,Kenya');
  124.   add('kg,Kyrgyzstan');
  125.   add('kh,Cambodia');
  126.   add('ki,Kiribati');
  127.   add('km,Comoros');
  128.   add('kn,Saint Kitts and Nevis');
  129.   add('kp,Democratic Peoples Republic of Korea');
  130.   add('kr,Republic of Korea');
  131.   add('kw,Kuwait');
  132.   add('ky,Cayman Islands');
  133.   add('kz,Kazakhstan');
  134.   add('la,Peoples Democratic Republic of Lao');
  135.   add('lb,Lebanon');
  136.   add('lc,Saint Lucia');
  137.   add('li,Liechtenstein');
  138.   add('lk,Sri Lanka');
  139.   add('lr,Liberia');
  140.   add('ls,Lesotho');
  141.   add('lt,Lithuania');
  142.   add('lu,Luxembourg');
  143.   add('lv,Latvia');
  144.   add('ly,Libyan Arab Jamahiriya');
  145.   add('ma,Morocco');
  146.   add('mc,Monaco');
  147.   add('md,Republic of Moldava');
  148.   add('mg,Madagascar');
  149.   add('mh,Marshall Islands');
  150.   add('mk,Former Yugoslav Republic of Macedonia');
  151.   add('ml,Mali');
  152.   add('mm,Myanmar');
  153.   add('mn,Mongolia');
  154.   add('mo,Macau');
  155.   add('mp,Northern Mariana Islands');
  156.   add('mq,Martinique');
  157.   add('mr,Mauritania');
  158.   add('ms,Montserrat');
  159.   add('mt,Malta');
  160.   add('mu,Mauritius');
  161.   add('mv,Maldives');
  162.   add('mw,Malawi');
  163.   add('mx,Mexico');
  164.   add('my,Malaysia');
  165.   add('mz,Mozambique');
  166.   add('na,Namibia');
  167.   add('nc,New Caledonia');
  168.   add('ne,Niger');
  169.   add('nf,Norfolk Island');
  170.   add('ng,Nigeria');
  171.   add('ni,Nicaragua');
  172.   add('nl,Netherlands');
  173.   add('no,Norway');
  174.   add('np,Nepal');
  175.   add('nr,Nauru');
  176.   add('nu,Niue');
  177.   add('nz,New Zealand');
  178.   add('om,Oman');
  179.   add('pa,Panama');
  180.   add('pe,Peru');
  181.   add('pf,French Polynesia');
  182.   add('pg,Papua New Guinea');
  183.   add('ph,Philippines');
  184.   add('pk,Pakistan');
  185.   add('pl,Poland');
  186.   add('pm,Saint Pierre and Miquelon');
  187.   add('pn,Pitcairn Island');
  188.   add('pr,Puerto Rico');
  189.   add('ps,Palestinian Territories');
  190.   add('pt,Portugal');
  191.   add('pw,Palau');
  192.   add('py,Paraguay');
  193.   add('qa,Qatar');
  194.   add('re,Reunion Island');
  195.   add('ro,Romania');
  196.   add('ru,Russian Federation');
  197.   add('rw,Rwanda');
  198.   add('sa,Saudi Arabia');
  199.   add('sb,Solomon Islands');
  200.   add('sc,Seychelles');
  201.   add('sd,Sudan');
  202.   add('se,Sweden');
  203.   add('sg,Singapore');
  204.   add('sh,Saint Helena');
  205.   add('si,Slovenia');
  206.   add('sj,Svalbard and Jan Mayen Islands');
  207.   add('sk,Slovak Republic');
  208.   add('sl,Sierra Leone');
  209.   add('sm,San Marino');
  210.   add('sn,Senegal');
  211.   add('so,Somalia');
  212.   add('sr,Suriname');
  213.   add('st,Sao Tome and Principe');
  214.   add('sv,El Salvador');
  215.   add('sy,Syrian Arab Republic');
  216.   add('sz,Swaziland');
  217.   add('tc,Turks and Caicos Islands');
  218.   add('td,Chad');
  219.   add('tf,French Southern Territories');
  220.   add('tg,Togo');
  221.   add('th,Thailand');
  222.   add('tj,Tajikistan');
  223.   add('tk,Tokelau');
  224.   add('tl,Timor-Leste');
  225.   add('tm,Turkmenistan');
  226.   add('tn,Tunisia');
  227.   add('to,Tonga');
  228.   add('tp,East Timor');
  229.   add('tr,Turkey');
  230.   add('tt,Trinidad and Tobago');
  231.   add('tv,Tuvalu');
  232.   add('tw,Taiwan');
  233.   add('tz,Tanzania');
  234.   add('ua,Ukraine');
  235.   add('ug,Uganda');
  236.   add('uk,United Kingdom');
  237.   add('um,United States (m) Outlying Islands');
  238.   add('us,United States');
  239.   add('uy,Uruguay');
  240.   add('uz,Uzbekistan');
  241.   add('va,Vatican City');
  242.   add('vc,Saint Vincent and the Grenadines');
  243.   add('ve,Venezuela');
  244.   add('vg,British Virgin Islands');
  245.   add('vi,U.S. Virgin Islands');
  246.   add('vn,Vietnam');
  247.   add('vu,Vanuatu');
  248.   add('wf,Wallis and Futuna Islands');
  249.   add('ws,Samoa');
  250.   add('ye,Yemen');
  251.   add('yt,Mayotte');
  252.   add('yu,Yugoslavia');
  253.   add('za,South Africa');
  254.   add('zm,Zambia');
  255.   add('zw,Zimbabwe')
  256. end;//with
  257.  
  258.  
  259.  

M+AUDIO

  • New member
  • *
  • Posts: 37
Re: How to load a list of strings without shipping a text file
« Reply #6 on: October 12, 2017, 11:35:34 pm »
Gizmo
didn't tried RT_STRING but RT_RCDATA just means raw data and is fine.
about the windres in the wiki, it says you need to have it when you are actually on linux (etc) and you want to compile from .rc to .res file. but once you've compiled to res in windows, you can just use the compiled *.res file afterward. (cut the res file from $(ProjPath)\lib\i386-win32 and paste it in $(ProjPath). then change your directive to {$R charencslaz.res} instead).  it should work just fine.

Noodly

  • New member
  • *
  • Posts: 40
Re: How to load a list of strings without shipping a text file
« Reply #7 on: October 12, 2017, 11:53:55 pm »
Why not just include a constant array and convert it to a stringlist at runtime?

taazz

  • Hero Member
  • *****
  • Posts: 4340
Re: How to load a list of strings without shipping a text file
« Reply #8 on: October 13, 2017, 12:09:24 am »
If you do not want to use the res add an invisible Tmemo in your form put the text in its lines property and just copy them to the string list on creation and delete the memo
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Gizmo

  • Hero Member
  • *****
  • Posts: 602
    • http://www.quickhash-gui.org
Re: How to load a list of strings without shipping a text file
« Reply #9 on: October 13, 2017, 01:09:26 am »
Thanks all...I went with Jacks solution in the end! Don't know why I didn't think of that. Much simpler for when it comes to cross compiling.

 Got myself tangled in over complexity. Thanks Jack.
Lazarus 1.6.4 and fpc 3.0.2 - Linux Mint 17 LTS, Windows 7 and Mac Yosemite
Useful Page to remember : http://wiki.freepascal.org/Cross_compiling#From_Linux_x64_to_Linux_i386

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus