/*   GPDriver2 source escchar.c
 *   $Id: escchar.c,v 1.1 2007/02/20 18:49:35 wuerthne Exp $
 *
 *   GPDriver2 - Gutenprint based printer spooler for RISC OS
 *   Copyright (C) 2007 Martin Wuerthner
 *
 *   This program is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License as published by the Free
 *   Software Foundation; either version 2 of the License, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *   for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *   Martin Wuerthner <ro-printing@mw-software.com>
 *   Mannheimer Str. 18, 67655 Kaiserslautern, Germany
 */

/* This routine maps the short printer name to the filename of the settings file.
   Since the short printer name can contain invalid filename characters, they
   have to be escaped. The escape character is the exclamation mark (which,
   obviously, has to be escaped itself if it occurs in a short printer name). */

#include <string.h>
#include "escchar.h"

/* the special characters plus the exclamation mark used for escaping */
const char* special_characters = ".: $&@^%\\*#!";

/* append the short name to the path escaping any characters not suitable
   as filename characters */
/* buffer_length is the overal buffer size of the path buffer */
void append_adjusted_leaf_name(char* path, const char* name, int buffer_length)
{
  int len = strlen(path);
  char* q = path + len;
  buffer_length -= len + 1;   /* number of free bytes left */
  const char* p = name;
  while(buffer_length && *p) {
    char* found;
    if ((found = strchr(special_characters, *p)) != NULL) {
      *q++ = '!'; *q++ = 'A' + found - special_characters;
    }
    else {
      *q++ = *p;
    }
    buffer_length--;
    p++;
  }
  *q = '\0';
}
