ifm2i7 (source code)

Convert raw format files generated by ifm (Interactive Fiction Mapper) to Inform 7 source code.

.

Author: Sergey Goldgaber http://code.wetash.com

Summary
Convert raw format files generated by ifm (Interactive Fiction Mapper) to Inform 7 source code.
Please do not modify.
Please do not modify.
Checks to see if two directions are opposites of one another.
Standardize direction names
Exit with an error if ifm could not be found in the user’s $PATH, or if it isn’t executable.
Set the default file name, if there isn’t one, or use the first command line argument, if it’s there.
Set the default file name, if there isn’t one, or use the first command line argument, if it’s there.
Remove illegal characters from the room, region, and item names, converting some of them to spaces as needed.
Redirects the output filehandle back to STDOUT and closes the open output file.
Enable debugging messages.
Disable warnings.
Silence almost all errors and warnings by redirecting STDERR to /dev/null
Print an error on STDERR and exit.
Complain and exit if the given file does not exist, or is not a “-”.
Given two positions on the map, find which direction the second is from the first.
Inform 7 build 5J39 has a bug where using the “called” directive can cause rooms to not be connected to one another, in certain cases.
Get a height, if it exists, from a line
Get a item, if it exists, from a line
Get a link or join, if it exists, from a line
Gets the link’s ‘go:’ direction, if there is one.
Gets the link’s ‘oneway:’ value, if there is one.
Get a link position, if it exists, from a line
Handle exceptions from Getopt::Lucid->getopt() threw
Use the Getopt::Lucid module to get and return options.
Get a room number, if it exists, from a line
Get a room name, if it exists, from a line
Get a room position, if it exists, from a line
Get a region (“section” in ifm lingo), if it exists, from a line
Get a task, if it exists, from a line
Get a title, if it exists, from a line
Get a width, if it exists, from a line
Calculate a path to/from two linked rooms if there is no explicit path in the just ended link/join block.
Calls ifm to convert ifm format files to ifm’s raw format.
Increment the number at the end of a word, or add “2” at the end if there is no existing number at the end of that word.
Main program
Redirect STDOUT to the output file.
Return the opposite of the given direction.
Populates %links and prints Inform 7 source code for the direction relation(s) between two rooms.
Populate %regions if we just exited a region block Then clear temporary region variables.
Populates %rooms and prints Inform 7 source code that creates a room and places it in a region.
Print to STDERR.
Prints Inform 7 source code for putting all possible exits from a room on to the status line.
Print Inform 7 source code for every link in %links
Print Inform 7 source code for every region in %regions
Print rooms
Read raw format in to internal format
Print usage
Handle --verbosity options

Includes

Internal includes

Please do not modify.

CarpWarns of errors.  http://search.cpan.org- /author- /RGARCIA- /perl-5.10.0- /lib- /Carp.pm
Carp::Assert::MoreAssures certain conditions never happen.  http://search.cpan.org- /author- /PETDANCE- /Carp-Assert-More-1.12- /More.pm
diagnosticsProduce verbose warning diagnostics.  http://search.cpan.org- /author- /RGARCIA- /perl-5.10.0- /lib- /diagnostics.pm
EnglishUse nice English words for ugly punctuation variables.  http://search.cpan.org- /~rgarcia- /perl-5.10.0- /lib- /English.pm
FatalReplaces functions with equivalents which succeed or die.  http://search.cpan.org- /author- /RGARCIA- /perl-5.10.0- /lib- /Fatal.pm
Getopt::LucidClear, readable syntax for option processing http://search.cpan.org- /~dagolden- /Getopt-Lucid-0.16- /lib- /Getopt- /Lucid.pm
ReadonlyFacility for creating read-only scalars, arrays, hashes.  http://search.cpan.org- /author- /ROODE- /Readonly-1.03- /Readonly.pm
strictPerl pragma to restrict unsafe constructs http://search.cpan.org- /author- /RGARCIA- /perl-5.10.0- /lib- /strict.pm
SwitchAn alternative to if/elsif http://search.cpan.org- /~rgarcia- /Switch-2.13- /Switch.pm
warningsPerl pragma to control optional warnings.  http://search.cpan.org- /author- /RGARCIA- /perl-5.10.0- /lib- /warnings.pm

User configurable includes

Smart::CommentsUse comments for diagnostics.  Feel free to (un)comment it if you feel the need.  http://search.cpan.org- /author- /DCONWAY- /Smart-Comments-v1.0.2- /lib- /Smart- /Comments.pm

Variables

Summary
Please do not modify.
Checks to see if two directions are opposites of one another.
Standardize direction names
Exit with an error if ifm could not be found in the user’s $PATH, or if it isn’t executable.
Set the default file name, if there isn’t one, or use the first command line argument, if it’s there.
Set the default file name, if there isn’t one, or use the first command line argument, if it’s there.
Remove illegal characters from the room, region, and item names, converting some of them to spaces as needed.
Redirects the output filehandle back to STDOUT and closes the open output file.
Enable debugging messages.
Disable warnings.
Silence almost all errors and warnings by redirecting STDERR to /dev/null
Print an error on STDERR and exit.
Complain and exit if the given file does not exist, or is not a “-”.
Given two positions on the map, find which direction the second is from the first.
Inform 7 build 5J39 has a bug where using the “called” directive can cause rooms to not be connected to one another, in certain cases.
Get a height, if it exists, from a line
Get a item, if it exists, from a line
Get a link or join, if it exists, from a line
Gets the link’s ‘go:’ direction, if there is one.
Gets the link’s ‘oneway:’ value, if there is one.
Get a link position, if it exists, from a line
Handle exceptions from Getopt::Lucid->getopt() threw
Use the Getopt::Lucid module to get and return options.
Get a room number, if it exists, from a line
Get a room name, if it exists, from a line
Get a room position, if it exists, from a line
Get a region (“section” in ifm lingo), if it exists, from a line
Get a task, if it exists, from a line
Get a title, if it exists, from a line
Get a width, if it exists, from a line
Calculate a path to/from two linked rooms if there is no explicit path in the just ended link/join block.
Calls ifm to convert ifm format files to ifm’s raw format.
Increment the number at the end of a word, or add “2” at the end if there is no existing number at the end of that word.
Main program
Redirect STDOUT to the output file.
Return the opposite of the given direction.
Populates %links and prints Inform 7 source code for the direction relation(s) between two rooms.
Populate %regions if we just exited a region block Then clear temporary region variables.
Populates %rooms and prints Inform 7 source code that creates a room and places it in a region.
Print to STDERR.
Prints Inform 7 source code for putting all possible exits from a room on to the status line.
Print Inform 7 source code for every link in %links
Print Inform 7 source code for every region in %regions
Print rooms
Read raw format in to internal format
Print usage
Handle --verbosity options

Global, user configurable

$EOLEnd of line character

Global, internal

Please do not modify.

Exit values

$EXIT_ERRORProgram exiting with an error.
$EXIT_NO_ERRORProgram exiting withou an error.

Truth and falseness

$FALSESelf explanatory.
$TRUESelf explanatory.

Directions

$NORTHSelf explanatory.
$SOUTHSelf explanatory.
$EASTSelf explanatory.
$WESTSelf explanatory.
$NORTHEASTSelf explanatory.
$NORTHWESTSelf explanatory.
$SOUTHEASTSelf explanatory.
$SOUTHWESTSelf explanatory.
$UPSelf explanatory.
$DOWNSelf explanatory.
$INSelf explanatory.
$OUTSelf explanatory.

Others

$EXITS_CODEInform 7 source code for printing all possible exits from a room.  Useful for debugging maps.
$NOWARNINGSDisables warning messages (false by default)
$USAGEWhat to print to show how this program is used.

Variables

$NOWARNINGS

my $NOWARNINGS

Functions

are_opposite_directions

sub are_opposite_directions

Checks to see if two directions are opposites of one another.

Parameters

scalarfirst direction
scalarsecond direction

Returns

$TRUEgiven directions are opposites
$FALSEgiven directions are not opposites

Output

nothing

canonical_direction

sub canonical_direction

Standardize direction names

Parameters

scalardirection

Returns

standardized direction name

Output

Error when an unknown direction is encountered.

Example

”east” returns “East”

”i” returns “In”

”out” returns “Out”

check_ifm

sub check_ifm

Exit with an error if ifm could not be found in the user’s $PATH, or if it isn’t executable.

Parameters

none

Returns

nothing

Output

Error if above conditions are not met

choose_input_file_name

sub choose_input_file_name

Set the default file name, if there isn’t one, or use the first command line argument, if it’s there.

Parameters

scalarinput file name from the --input command line option

Returns

scalarchosen input file name

Output

nothing

choose_output_file_name

sub choose_output_file_name

Set the default file name, if there isn’t one, or use the first command line argument, if it’s there.

Parameters

scalarinput file name from the --input command line option

Returns

scalarchosen input file name

Output

nothing

clean_name

sub clean_name

Remove illegal characters from the room, region, and item names, converting some of them to spaces as needed.

Parameters

scalarroom name

Returns

scalarroom name

Output

nothing

close_output_file

sub close_output_file

Redirects the output filehandle back to STDOUT and closes the open output file.

Parameters

filehandleoutput filehandle
filehandleold output filehandle (STDOUT)

Returns

nothing

Output

nothing

enable_debug

sub enable_debug

Enable debugging messages.

Parameters

none

Returns

nothing

Output

nothing

enable_quiet

sub enable_quiet

Disable warnings.

Parameters

none

Returns

nothing

Output

nothing

enable_silence

sub enable_silence

Silence almost all errors and warnings by redirecting STDERR to /dev/null

Parameters

none

Returns

nothing

Output

nothing

exiterr

sub exiterr

Print an error on STDERR and exit.

Parameters

scalarError to print

Returns

nothing

Output

Error on STDERR

file_exists

sub file_exists

Complain and exit if the given file does not exist, or is not a “-”.

Parameters

scalarfile name

Returns

nothing

Output

Error when the file does not exist.

find_direction

sub find_direction

Given two positions on the map, find which direction the second is from the first.

Parameters

scalar (two integers, separated by a space)source position
scalar (two integers, separated by a space)destination position

Returns

direction

Output

Errors when positions are not integers, or when source and destination positions are the same.

Examples

Given: “0 0” and “0 1”, the direction is “North”

Given: “0 1” and “1 1”, the direction is “East”

Given: “1 1” and “1 0”, the direction is “South”

Given: “1 0” and “0 0”, the direction is “West”

Given: “1 1” and “0 0”, the direction is “Southwest”

fix_5J39_room_bug

sub fix_5J39_room_bug

Inform 7 build 5J39 has a bug where using the “called” directive can cause rooms to not be connected to one another, in certain cases.  See the README file for more details.

So in order to avoid this bug and still generate code that the Inform 7 build 5J39 compiler likes, we’re going to have to change some room names.

Parameters

scalardirty room name

Returns

scalarcleaned room name

Output

Warnings when the room name has been changed.

Example

”to House” returns “to_House”

get_height

sub get_height

Get a height, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_item

sub get_item

Get a item, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_link_and_join

sub get_link_and_join

Get a link or join, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_link_go

sub get_link_go

Gets the link’s ‘go:’ direction, if there is one.

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_link_oneway

sub get_link_oneway

Gets the link’s ‘oneway:’ value, if there is one.

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_link_position

sub get_link_position

Get a link position, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

getopt_exception_handler

sub getopt_exception_handler

Handle exceptions from Getopt::Lucid->getopt() threw

Parameters

exceptionthe exception that Getopt::Lucid->getopt() threw

Returns

nothing

Output

A descriptive message about the exception.

get_options

sub get_options

Use the Getopt::Lucid module to get and return options.

Parameters

none

Returns

scalarResult->get_noexits
scalarInput_File
scalarOutput_File

Output

Errors when Getopt::Lucid has been called incorrectly or invalid options were used.

Usage and/or version, if the user requests either.

get_room_number

sub get_room_number

Get a room number, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_room_name

sub get_room_name

Get a room name, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_room_position

sub get_room_position

Get a room position, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_region

sub get_region

Get a region (“section” in ifm lingo), if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_task

sub get_task

Get a task, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_title

sub get_title

Get a title, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

get_width

sub get_width

Get a width, if it exists, from a line

Parameters

scalarline

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

handle_empty_links

sub handle_empty_links

Calculate a path to/from two linked rooms if there is no explicit path in the just ended link/join block.

Parameters

scalarline

Returns

nothing

Output

This subroutine directly modifies variables in raw2internal

ifm2raw

sub ifm2raw

Calls ifm to convert ifm format files to ifm’s raw format.

Parameters

scalarpath to ifm file

Returns

arrayraw format file

Output

nothing

increment_word

sub increment_word

Increment the number at the end of a word, or add “2” at the end if there is no existing number at the end of that word.

Parameters

scalarword

Returns

word

Output

nothing

main

sub main

Main program

Parameters

scalarnone

Returns

nothing

Output

nothing

open_output_file

sub open_output_file

Redirect STDOUT to the output file.

Parameters

scalaroutput file name

Returns

file handleold file handle (save this to restore it later)

Output

nothing

opposite_direction

sub opposite_direction

Return the opposite of the given direction.

Parameters

scalaroriginal direction

Returns

opposite direction

Output

Error if given an invalid direction.

populate_links

sub populate_links

Populates %links and prints Inform 7 source code for the direction relation(s) between two rooms.

Parameters

none

Returns

nothing

Output

Inform 7 source code.

NOTE

This subroutine directly modifies variables in raw2internal

populate_regions

sub populate_regions

Populate %regions if we just exited a region block Then clear temporary region variables.

Parameters

none

Returns

nothing

Output

nothing

NOTE

This subroutine directly modifies variables in raw2internal

populate_rooms

sub populate_rooms

Populates %rooms and prints Inform 7 source code that creates a room and places it in a region.

Parameters

none

Returns

nothing

Output

Inform 7 source code.

NOTE

This subroutine directly modifies variables in raw2internal

printerr

sub printerr

Print to STDERR.  Only print warnings if $NOWARNINGS is false.

Parameters

scalarmessage to print

Returns

nothing

Output

message on STDERR

print_exits

sub print_exits

Prints Inform 7 source code for putting all possible exits from a room on to the status line.  This is very useful for debugging maps.

Parameters

scalarboolean value that controls whether to print the exits code or not.

Returns

nothing

Output

Inform 7 source code.

print_links

sub print_links

Print Inform 7 source code for every link in %links

Parameters

referenceto %links
referenceto %rooms

Returns

nothing

Output

Inform 7 source code for every link

print_regions

sub print_regions

Print Inform 7 source code for every region in %regions

Parameters

referenceto %regions

Returns

nothing

Output

Inform 7 source code for every region.

print_rooms

sub print_rooms

Print rooms

raw2internal

sub raw2internal

Read raw format in to internal format

Parameters

arraylines from input file

Returns

nothing

Output

nothing

NOTE

The lines fed to this routine must be in the raw format generated by IFM.

usage

sub usage

Print usage

Parameters

none

Returns

none

Output

Usage information on STDERR

verbosity_handler

sub verbosity_handler

Handle --verbosity options

Parameters

scalarverbosity option setting

Returns

nothing

Output

Error if an invalid verbosity setting has been used.

my $NOWARNINGS
sub are_opposite_directions
Checks to see if two directions are opposites of one another.
sub canonical_direction
Standardize direction names
sub check_ifm
Exit with an error if ifm could not be found in the user’s $PATH, or if it isn’t executable.
sub choose_input_file_name
Set the default file name, if there isn’t one, or use the first command line argument, if it’s there.
sub choose_output_file_name
Set the default file name, if there isn’t one, or use the first command line argument, if it’s there.
sub clean_name
Remove illegal characters from the room, region, and item names, converting some of them to spaces as needed.
sub close_output_file
Redirects the output filehandle back to STDOUT and closes the open output file.
sub enable_debug
Enable debugging messages.
sub enable_quiet
Disable warnings.
sub enable_silence
Silence almost all errors and warnings by redirecting STDERR to /dev/null
sub exiterr
Print an error on STDERR and exit.
sub file_exists
Complain and exit if the given file does not exist, or is not a “-”.
sub find_direction
Given two positions on the map, find which direction the second is from the first.
sub fix_5J39_room_bug
Inform 7 build 5J39 has a bug where using the “called” directive can cause rooms to not be connected to one another, in certain cases.
sub get_height
Get a height, if it exists, from a line
sub get_item
Get a item, if it exists, from a line
sub get_link_and_join
Get a link or join, if it exists, from a line
sub get_link_go
Gets the link’s ‘go:’ direction, if there is one.
sub get_link_oneway
Gets the link’s ‘oneway:’ value, if there is one.
sub get_link_position
Get a link position, if it exists, from a line
sub getopt_exception_handler
Handle exceptions from Getopt::Lucid->getopt() threw
sub get_options
Use the Getopt::Lucid module to get and return options.
sub get_room_number
Get a room number, if it exists, from a line
sub get_room_name
Get a room name, if it exists, from a line
sub get_room_position
Get a room position, if it exists, from a line
sub get_region
Get a region (“section” in ifm lingo), if it exists, from a line
sub get_task
Get a task, if it exists, from a line
sub get_title
Get a title, if it exists, from a line
sub get_width
Get a width, if it exists, from a line
sub handle_empty_links
Calculate a path to/from two linked rooms if there is no explicit path in the just ended link/join block.
sub ifm2raw
Calls ifm to convert ifm format files to ifm’s raw format.
sub increment_word
Increment the number at the end of a word, or add “2” at the end if there is no existing number at the end of that word.
sub main
Main program
sub open_output_file
Redirect STDOUT to the output file.
sub opposite_direction
Return the opposite of the given direction.
sub populate_links
Populates %links and prints Inform 7 source code for the direction relation(s) between two rooms.
sub populate_regions
Populate %regions if we just exited a region block Then clear temporary region variables.
sub populate_rooms
Populates %rooms and prints Inform 7 source code that creates a room and places it in a region.
sub printerr
Print to STDERR.
sub print_exits
Prints Inform 7 source code for putting all possible exits from a room on to the status line.
sub print_links
Print Inform 7 source code for every link in %links
sub print_regions
Print Inform 7 source code for every region in %regions
sub print_rooms
Print rooms
sub raw2internal
Read raw format in to internal format
sub usage
Print usage
sub verbosity_handler
Handle --verbosity options