Page 1 of 1 1
Topic Options
#158073 - 2006-03-02 02:55 AM Is there a better way to deal with data in a text file?
IanDubbelboer Offline
Fresh Scripter

Registered: 2004-06-25
Posts: 24
I need some advice from the Kixtart sages.

I am writting a script that does some automated backups to CD and in the course of doing so, I write a log file that looks like this:

Code:

******************************************************************
Results of Backup_for_IDubbelboer_on_January_15_2006
******************************************************************

The backup of files took 0:03:31 to complete.

The following files, if any, were skipped as they were in use.


The compression and encryption of the backup file took 0:19:21 to complete.

The splitting of the compressed backup file took 0:02:26 to complete.
The file was split into 2 parts.

It took 0:03:00 to burn the 2 CD(s).
CD 1 took 0:00:40 to burn.
CD 2 took 0:02:20 to burn.

Backup completed successfully on 2006/01/15




Now I have to read data out of this file at a later date and on a different computer, so I have some code that looks like this:

Code:


$ = OPEN(1,"c:\backup\backup.log",2)

DO

$DoExit = @ERROR
$LogRead = READLINE(1)

SELECT

CASE (NOT INSTR($LogRead, " on ") = 0) AND (NOT $OSInfo[1] = 2)
$BackupDate = SUBSTR($LogRead, 34, 10)

CASE (NOT INSTR($LogRead, " bytes") = 0)
$BackupSpace = SUBSTR($LogRead, 1, (INSTR($LogRead, " bytes") - 1))
$BackupTime = SUBSTR($LogRead, (LEN($LogRead) - 20), 8)

CASE (NOT INSTR($LogRead, "e backup file t") = 0)
$CompEncryptTime = SUBSTR($LogRead, 55, 8)

CASE (NOT INSTR($LogRead, "d backup file t") = 0)
$SplitTime = SUBSTR($LogRead, 49, 8)

CASE (NOT INSTR($LogRead, "t took") = 0)
$CDBurnTime = SUBSTR($LogRead, 8, 8)
$CDBurnCount = SUBSTR($LogRead, 29, 1)

CASE 1

ENDSELECT




This code works great, but now I have to be able to support both French and English log files.

With my current technique, I am forced to write another case statement with the correct values to read from a French text file. I also have to write code to make sure I detect the language of the text file before I read it.

So does anyone have a better way of handling this?

Keeping in mind these requirements:

1. Output has to be written to and read from a unicode text file. (If you don't know how to work with unicode, don't worry I can adjust an ascii solution.)

2. Has to be user friendly as non techies will be reading the file as well.

Anyone worked through this kind of problem before?

Top
#158074 - 2006-03-02 04:15 AM Re: Is there a better way to deal with data in a text file?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Wow - great question dude. Have been thinking much about this since you posted. Had thoughts of using language files (like say, INI files) that had language-sensitive strings to look for, and the different offsets to use etc.

But to be honest - assuming your only ever going to support French and English and no other language - I wouldn't try to over-design this and fret about it. I would say to stick with your first thoughts:

1) Detect the language (read the first line to detect)

2) Have two seperate CASE structures.

It really is the best and most effecient way to go as far as I'm concerned - straight-forward and to the point. If you had more than two languages to support - yeah maybe - else - stick with your first gut design. I guess what I'm saying is - there may be a more eloquent way of doing this, but not necessarily better.

Since this is only my two cents, anyone else got an opinion ?

-Shawn

Top
#158075 - 2006-03-02 07:54 AM Re: Is there a better way to deal with data in a text file?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Quote:

Output has to be written to and read from a unicode text file




Well I tend to agree with Shawn in that the .INI file on Windows XP does support Unicode characters. Notepad may not display them correctly but they will be correct in a full blown editor or DOS console.
This format will much more easily support all types of languages and very easy to maintain.

Code:
$Backup = ReadProfileString(@ScriptDir+'\'+'BACKUPS.INI', 'BACKUP', 'Temp')
'Values are: ' + $Backup ?



Code:
[BACKUP]
Temp=à â ç é è ê ë î ï ô û ù ü




French alphabet
From Wikipedia, the free encyclopedia
http://en.wikipedia.org/wiki/French_alphabet
 

Top
#158076 - 2006-03-02 09:30 AM Re: Is there a better way to deal with data in a text file?
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Ini files are difficult to read by non technical people.
Just putting the language at the first line of the log, read this line , do a check on it and start the English or French portion of the script depending on the outcome of the check. This would keep the file readable also for non technical people.

You would get a logfile like this.


Quote:


English log.

******************************************************************
Results of Backup_for_IDubbelboer_on_January_15_2006
******************************************************************

The backup of files took 0:03:31 to complete.

The following files, if any, were skipped as they were in use.


The compression and encryption of the backup file took 0:19:21 to complete.

The splitting of the compressed backup file took 0:02:26 to complete.
The file was split into 2 parts.

It took 0:03:00 to burn the 2 CD(s).
CD 1 took 0:00:40 to burn.
CD 2 took 0:02:20 to burn.

Backup completed successfully on 2006/01/15



_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#158077 - 2006-03-02 02:42 PM Re: Is there a better way to deal with data in a text file?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Heres another concept - when you create the report, you include an INI section at the bottom of the report - and replicate the details in a machine-readable format. Kinda like this:

Code:

******************************************************************
Results of Backup_for_IDubbelboer_on_January_15_2006
******************************************************************

The backup of files took 0:03:31 to complete.

The following files, if any, were skipped as they were in use.


The compression and encryption of the backup file took 0:19:21 to complete.

The splitting of the compressed backup file took 0:02:26 to complete.
The file was split into 2 parts.

It took 0:03:00 to burn the 2 CD(s).
CD 1 took 0:00:40 to burn.
CD 2 took 0:02:20 to burn.

Backup completed successfully on 2006/01/15

[Data]
Language=English
CDBurnTime=0:03:31
CDBurnCount=2



Then in your script, just read the stuff and the language wouldn't matter.

Code:

break on

$Profile = ".\file.rpt"

$CDBurnTime = ReadProfileString($Profile,"Data","CDBurnTime")
$CDBurnCount = ReadProfileString($Profile,"Data","CDBurnCount")

?"time=" $CDBurnTime
?"count=" $CDBurnCount

exit 0



Just throwing the idea out there - not sure if I would personally take this approach or not.

Top
#158078 - 2006-03-03 01:00 AM Re: Is there a better way to deal with data in a text file?
IanDubbelboer Offline
Fresh Scripter

Registered: 2004-06-25
Posts: 24
Thanks for the input.

I'm not sure what I'm going to settle on yet, but I came up with another possible idea.

In this script I have a messaging function. Basically, I CALLMESSAGE($MESSAGENUMBER, $LANGUAGE, $VAR1, $VAR2...) and then use the result to correctly show a message to the screen or to a log file.

I was thinking I might be able to call the message function to generate predefined search strings that I could look for. I could use a | instead of passing a $VAR and then use split on the resulting message INSTR to figure out where the Variable would be in the string regardless of language. It should also make things more resistent to changes to the wording in my translation file as I would not have to update the log reader function every time I changed wording.


I still have to try it, but even if it doesn't work the INI ideas should. Thanks again.

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1644 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.174 seconds in which 0.113 seconds were spent on a total of 11 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org