Author Topic: Request To get all text from single text file function  (Read 7872 times)

kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Request To get all text from single text file function
« on: June 17, 2018, 10:36:38 PM »
Hello, Any one could help to revise current "file_util" plugin. I would like to get all text from single file only. Many Thanks.  :)
« Last Edit: June 17, 2018, 11:14:25 PM by kent79 »

kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Re: Request To get all text from single text file function
« Reply #1 on: June 22, 2018, 10:47:15 PM »
Hello, any one can help me. Thanks


kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Re: Request To get all text from single text file function
« Reply #3 on: June 23, 2018, 06:24:17 AM »
https://github.com/mickelson/attract/blob/master/config/modules/file.nut

class ReadTextFile
  function readline()

What is your goal?

Please help to write a sample layout code.

Below is my goal

Txt file:

abc
def
xyz

AM:

local abc =  fe.add_text( "read txt file 1st line", 1, 0, 0, 1,1 );
local def =  fe.add_text( "read txt file 2nd line", 2, 0, 4, 1,1 );
local xyz =  fe.add_text( "read txt file 3rd line", 3, 0, 8, 1,1 );
« Last Edit: June 25, 2018, 02:29:10 AM by kent79 »

Dal1980

  • Full Member
  • ***
  • Posts: 54
    • View Profile
Re: Request To get all text from single text file function
« Reply #4 on: June 26, 2018, 06:48:27 PM »
Hi kent79

This script I wrote for one of my layouts might help you.

Code: [Select]
# region.ini Dal1980;
# all lines starting with a # symbol or a blank return are ignored

005=World
100lions=World
10yard=World
10yard85=US
10yardj=Japan
11beat=World
1292apvs=World
136094_0072=World
136095_0072=World
1392apvs=World
18w=World
18w2=World

#... my file goes on for many lines

Code: [Select]
_f = file( myConfig["optPath"] + "parts/region.ini", "r" ); //create file resource
_f = file( "c:/emu/attract/layouts/parts/region.ini", "r" ); //create file resource <- you can add the path directly if you want rather than pulling it from layout config options

_blb = _f.readblob( 100000 ); //pulls in 10,000 characters <- lazy way of setting the limit for read in
for(local i = 0; i < _blb.len(); i++){ 
    binChar = _blb.readn( 'b' );
    curChar = binChar.tochar();
    if(curChar == "#") lockLine = true;
    if(curChar == "\n") lockLine = false;
    if(!lockLine){
        if(curChar != "," && curChar != " " && curChar != "\n" && curChar != "\r") fileEntry += curChar;
        if(curChar == "\n"){
            tempArray = split( fileEntry, "=" );
            if(tempArray.len() == 2){
              /*tempObj = {
                "id": tempArray[0],
                "name": tempArray[1]
              }*/
              regionsID.push(tempArray[0]);
              regionsName.push(tempArray[1]);
              //regions.push(tempObj);
              fileEntry = "";
            }
        }
    }
}

Then I used this to call the data

Code: [Select]
local mapExample = fe.add_image( "parts/[!getCurrentRegion]", flw*0.860, flh*0.003, flw*0.140, flh*0.100);
mapExample.preserve_aspect_ratio = true;
function getCurrentRegion( ioffset){
  local romName = fe.game_info(Info.Name, ioffset);
  romName = romName.tolower(); //just in case
  local returnRegion = "";
  for(local i = 0; i < regionsID.len(); i++){
    if(regionsID[i] == romName) returnRegion = regionsName[i];
  }
  if(returnRegion == "") returnRegion = "world";
  returnRegion.tolower();
  return "mame-regions-" + returnRegion + ".png";
}


You will obviously need to adapt the code to your requirements.

The goal for my code was to pull in using an ini style document (similar to catver.ini), I would have an array with all rom names and an array of regions that match. I didn't end up using this in the end but the function I wrote uses the selected rom id to lookup the region data and then throw back a string representing the correct image.

I've also wrote another way to read in the ini style and utilise it a different way if you want to download this script: http://retro.zombiesbyte.com/attract-mode/plugin-system-menu

Hope this helps you



« Last Edit: June 27, 2018, 06:43:34 AM by Dal1980 »

kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Re: Request To get all text from single text file function
« Reply #5 on: June 27, 2018, 04:36:30 AM »
Thank you Dal1980 reply

Sorry, I don't know understand how to write a exactly script. Could you help me ?

Attachment is my text file. I want to read it and then display it on AM.

local aaaaaa =  fe.add_text( "read txt file 1st line", 1, 2, 0, 1,10 );
local bbbbbb =  fe.add_text( "read txt file 2nd line", 1, 12, 4, 1,10 );
local ccccccc =  fe.add_text( "read txt file 3rd line", 1, 22, 8, 1,10 );
local dddddd =  fe.add_text( "read txt file 4th line", 1, 32, 8, 1,10 );

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Request To get all text from single text file function
« Reply #6 on: June 27, 2018, 07:10:37 AM »
Thank you Dal1980 reply

Sorry, I don't know understand how to write a exactly script. Could you help me ?

Attachment is my text file. I want to read it and then display it on AM.

local aaaaaa =  fe.add_text( "read txt file 1st line", 1, 2, 0, 1,10 );
local bbbbbb =  fe.add_text( "read txt file 2nd line", 1, 12, 4, 1,10 );
local ccccccc =  fe.add_text( "read txt file 3rd line", 1, 22, 8, 1,10 );
local dddddd =  fe.add_text( "read txt file 4th line", 1, 32, 8, 1,10 );

Did you get my pm?

kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Re: Request To get all text from single text file function
« Reply #7 on: June 27, 2018, 07:24:45 AM »
Thank you Dal1980 reply

Sorry, I don't know understand how to write a exactly script. Could you help me ?

Attachment is my text file. I want to read it and then display it on AM.

local aaaaaa =  fe.add_text( "read txt file 1st line", 1, 2, 0, 1,10 );
local bbbbbb =  fe.add_text( "read txt file 2nd line", 1, 12, 4, 1,10 );
local ccccccc =  fe.add_text( "read txt file 3rd line", 1, 22, 8, 1,10 );
local dddddd =  fe.add_text( "read txt file 4th line", 1, 32, 8, 1,10 );

Did you get my pm?

Yeah. But my code skill doesn't good enough. could you write a real script to me? Thanks.

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Request To get all text from single text file function
« Reply #8 on: June 27, 2018, 08:12:41 AM »
Thank you Dal1980 reply

Sorry, I don't know understand how to write a exactly script. Could you help me ?

Attachment is my text file. I want to read it and then display it on AM.

local aaaaaa =  fe.add_text( "read txt file 1st line", 1, 2, 0, 1,10 );
local bbbbbb =  fe.add_text( "read txt file 2nd line", 1, 12, 4, 1,10 );
local ccccccc =  fe.add_text( "read txt file 3rd line", 1, 22, 8, 1,10 );
local dddddd =  fe.add_text( "read txt file 4th line", 1, 32, 8, 1,10 );

Did you get my pm?

Yeah. But my code skill doesn't good enough. could you write a real script to me? Thanks.

I got you tonight. You on discord?

Dal1980

  • Full Member
  • ***
  • Posts: 54
    • View Profile
Re: Request To get all text from single text file function
« Reply #9 on: June 27, 2018, 08:15:10 AM »
I'm not sure what you want to achieve but this should be a leap in the right direction for you

filename should be 1.ini
Comments allowed using # symbol and make sure you press return at the end of the last line otherwise the last entry my not be picked up.
You can add as many lines as you want.

Code: [Select]
aaaaaa=Something here
bbbbbb=Another thing here
cccccc=A test title here
dddddd=And now for something completely different



Code: [Select]
local flw = fe.layout.width;
local flh = fe.layout.height;
local bg1 = fe.add_text( "", 0, 0, flw, flh).set_bg_rgb( 255, 0, 14)

local _f = null;
local _blb = null;
local binChar = null;
local curChar = null;
local tempArray = [];
local fileEntry = "";
local myFileIDs = [];
local myFileValues = [];
local lockLine = false; //used for comment handling


////!!!!!UPDATE THIS!!!!!///////
_f = file( "c:/emu/attract/layouts/mytheme/1.ini", "r" ); //create file resource (full path)


_blb = _f.readblob( 100000 ); //pulls in 10,000 characters <- lazy way of setting the limit for read in
for(local i = 0; i < _blb.len(); i++){ 
    binChar = _blb.readn( 'b' );
    curChar = binChar.tochar();
    if(curChar == "#") lockLine = true;
    if(curChar == "\n") lockLine = false;
   
    if(!lockLine){
        if(curChar != "," && curChar != "\n" && curChar != "\r") fileEntry += curChar;
       
        if(curChar == "\n"){
            tempArray = split( fileEntry, "=" );
            if(tempArray.len() == 2){
              myFileIDs.push(tempArray[0]);
              myFileValues.push(tempArray[1]);
              fileEntry = "";
            }
        }
    }
}

for(local i = 0; i < myFileValues.len(); i++){
   local tempTxt = fe.add_text( myFileValues[i], 10, ((i*50) + 2), myFileValues[i].len() * 20, 30 )
   //local tempTxt is only relevant to the value currently active in the loop. Once the loop passes to the next element you'll have lost
   //direct access to manipulating that element.
   
   //myFileValues[i] <- an array containing your list of values read in from the ini. If you need access to the ids then use myFileIDs
   
   //((i*50) + 2) This takes each i in the loop i.e. (0*50)+2 = 2, (1*50)+2 = 52, (2*50)+2 = 102, (3*50)+2 = 152 etc
   
   //myFileValues[i].len() * 20 <- this just uses 20 * the character length to provide enough width otherwise the text will be cut short
   //You may need to alter this based on text size and length as fonts generally aren't going to be fixed widths, you can set this number
   //to the full screen width if you want and just be done with it. Doesn't make much difference unless your trying to align things in different ways
   
   //30 is the font size in pixels. All this is absolute px measurment so bare that in mind when using different resolutions, you might be better off
   //using a percentage based calculation which will help transition between various monitors of the same aspect.
   
   tempTxt.align = Align.Left;
}



If you really don't require the ini to have id=value structure and would prefer just to use single lines then strip this block out:

Code: [Select]
        if(curChar == "\n"){
            tempArray = split( fileEntry, "=" );
            if(tempArray.len() == 2){
              myFileIDs.push(tempArray[0]);
              myFileValues.push(tempArray[1]);
              fileEntry = "";
            }
        }

and replace with

Quote
        if(curChar == "\n"){
            if(fileEntry != "" && fileEntry != " "){
              myFileValues.push(fileEntry);
              fileEntry = "";
            }
        }


kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Re: Request To get all text from single text file function
« Reply #10 on: June 27, 2018, 09:04:54 AM »
Sorry, my English doesn't good enough, may be get you confuse. Let me to explain one.

Below AM theme screen is my target.

1.txt file put in C:\
AM theme will read txt file and then display the text on theme. Anyone can help me to complete layout.nut file to meet my target.  Thanks


 
« Last Edit: June 27, 2018, 09:09:05 AM by kent79 »

Dal1980

  • Full Member
  • ***
  • Posts: 54
    • View Profile
Re: Request To get all text from single text file function
« Reply #11 on: June 27, 2018, 09:54:42 AM »
Code: [Select]
//
// Attract-Mode Front-End - "Basic" sample layout
//
fe.layout.width=1280;
fe.layout.height=720;

local _f = null;
local _blb = null;
local binChar = null;
local curChar = null;
local tempArray = [];
local fileEntry = "";
local myFileIDs = [];
local myFileValues = [];
local lockLine = false;

_f = file( "c:/zombie1/emu/attract/layouts/hello-nintendo/1.txt", "r" );


_blb = _f.readblob( 100000 ); //pulls in 10,000 characters <- lazy way of setting the limit for read in
for(local i = 0; i < _blb.len(); i++){ 
    binChar = _blb.readn( 'b' );
    curChar = binChar.tochar();
    if(curChar == "#") lockLine = true;
    if(curChar == "\n") lockLine = false;
   
    if(!lockLine){
        if(curChar != "," && curChar != "\n" && curChar != "\r") fileEntry += curChar;
       
        if(curChar == "\n"){
            if(fileEntry != "" && fileEntry != " "){
              myFileValues.push(fileEntry);
              fileEntry = "";
            }
        }
    }
}

local l = fe.add_text( myFileValues[0], 100, 100, 290, 56 );
local l2 = fe.add_text( myFileValues[1], 100, 150, 290, 56 );
local l3 = fe.add_text( myFileValues[2], 100, 200, 290, 56 );
local l4 = fe.add_text( myFileValues[3], 100, 250, 290, 56 );





kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Re: Request To get all text from single text file function
« Reply #12 on: June 27, 2018, 03:40:57 PM »
@Dal1980

It works. I am very very appreciate. You help me a lot  :)  :)

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Request To get all text from single text file function
« Reply #13 on: June 27, 2018, 04:15:41 PM »
Way more complicated than need be. Try what I sent in pm. It’s only 4 lines of code to create necessary array of strings.

kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Re: Request To get all text from single text file function
« Reply #14 on: June 27, 2018, 06:09:27 PM »
Way more complicated than need be. Try what I sent in pm. It’s only 4 lines of code to create necessary array of strings.

Yeah, You are right. It works too and using less cpu utilization. Much appreciate   :)