LLOOP Index | GSP Language | GSP Library | Framework Classes | Component Classes

fileunix.gsp

This is the verbatim text of the file "fileunix.gsp" part of the LLOOP package. The copyright remains with Michel MEHL. All rights reserved.



/**
 * Reads a unix file/directory name or path. 
 *
 * Names are separated with slash chars '/'. 
 *
 * Any ASCII char can be used for the names, but the first encountered white space char marks the end of the path.
 *
 * Whitespaces inside file paths must be banalized using backslash chars '\', or alternatively the full path can be enclosed by quotes. Escape chars are allowed. 
 */

import quote;

token FileUnixToken 
  extends token QuoteToken() 
          class universal::File()
  include <stdio.h>, 
          <ctype.h>, 
           "universal__File.h"
  alias "fileunix"
  parse
  {{ 
    /* Set explicitly the file system type for the file object so that it
     * is managed the proper way afterwards by the file object.
     */

    m_fs = FS_UNIX;

    char c = 0;        		
    QuoteToken *pReturnToken = this; // Is not modified by QuoteToken

    /* To avoid that information about eating whitespace be reset by
     * quote token parsing, it is disabled.
     */

    disableWhitespaceEating(true);

    if (!QuoteToken::parseSymbol(parser, pReturnToken, getParent()))
      {      
	/* A quote couldn't be parsed. Reset position in stream
	 * and try to parse a normal file path.
	 */

	is.clear();
	is.seekg(getOffset());
	is.get(c);

	while (!isspace(c) && !is.fail()) 
	  {        
	    if (c == '\\')
	      {
		if (!QuoteToken::getEscapeSequence(is, c))
		  abort("");
	      }

	    append(c);
	    is.get(c);
	  }

	/* Set pos. to the immediate following char
	 * If there were a failure or EOF, this does nothing.
	 */
	
	is.seekg(-1, ios::cur); 
      }

    disableWhitespaceEating(false);

    /* Set the file name by passing the const char* of the result string, 
     * since ending null char may have been extracted and accepted!
     * This ensures a clean string is constructed from that C string.
     * If the name is empty, it is an error !
     */

    setName(str()); 

    if (name().empty())
      abort("");

  }}
  expand
  {{
    universal::String sEscapeSequence = name().escapeSequence();
    sEscapeSequence.replace(" ", "\\ ", true);
    os << sEscapeSequence;
  }}
  test
  {{
    "empty name"                     : fail {}
    "only one char"                  : pass {z}
    "only one valid escape char "    : pass {\f}
    "unbanalized ending space"       : fail {/home/mehl } {/home/mehl }
    "name with \\02 escape char "    : pass {/home/mehl\02} "/home/mehl\\2"
    "name with ending space (octal)" : pass {/home/mehl\40}  {/home/mehl\ }
    "name with 2 ending space (hexa)": pass {/home/mehl\x20\x20}  {/home/mehl\ \ }
    "name with tab and new line "    : pass {/home/mehl\n\t/dev}
    "normal path"                    : pass {/home/mehl/lloop/src}
    "path with banalized spaces"     : pass {dir\ with\ space\ chars/file\ with\ space\ chars}
    "path with unbanalized spaces"   : fail {/home/mehl/lloop parser/src}
    "path banalized with quotes"     : pass {"/home/mehl/lloop parser/src"} {/home/mehl/lloop\ parser/src}
    "short path within quotes"       : pass {"/sbin"} {/sbin}
  }}

This file is part of the LLOOP Reversible Object-Oriented Parser Generator. Copyright (c) 2005-2006 Michel MEHL, France. All rights reserved. LLOOP is distributed by the company ERSA SaRL.


Copyright (c) 2005-2006 Michel MEHL, Haguenau, France
LLOOP version 1.1