[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [Java] læse \n ind



On Tue, 06 Jul 2004 14:29:36 +0200, Rasmus Wätjen wrote:
> Jeg har en lille programstump som skal kunne skrive Strings efterfulgt af
> en delimiter.
> Ofte vil denne delimiter være newline, men den kan være en hvilken som
> helst streng, så det skal være konfigurerbart.
> Så jeg læser delimiteren ind fra fil.
> Her er problemet så at hvis jeg i filen har stående f.eks. delim="\r\n",
> så kommer der i den indlæste delimiter String til at stå bogstaveligt
> "\r\n" (altså "backslash r backslash n").
> 
> Er der nogen måde jeg kan undgå det på?
> Eller er der en smart måde at rette det tilbage til newline uden brug af
> regexp eller String.replaceAll (fordi det skal kunne køre på 1.3)?

Hvis du finder ud af at den nemmeste måde er at parse det selv, kan det
være at nedenstående programstump i C kan hjælpe dig. I Java ville jeg
tro at en StringBuffer er det mest effektive til at opbygge sådan en ny
String. Nedenstående understøtter \r\n og lignende escapes samt \xNN hex
escapes.


/** Returns the value (0-15) of a single hex digit. Returns 0 on error. */
int hex2num(unsigned char c)
{
    return  (c >= '0' && c <= '9' ? c-'0' :
            (c >= 'a' && c <= 'f' ? c-'a'+10 :
            (c >= 'A' && c <= 'F' ? c-'A'+10 : 0)));
}

/** Initializes a struct string from a 0-terminated string, parsing escape
 * sequences. The struct string will not be 0-terminated. */
void string_init(struct string *dst, const unsigned char *src)
{
    const size_t slen = strlen(src);
    size_t i;
    dst->l = 0;
    dst->s = malloc(slen);

    for (i = 0; i < slen; i++) {
        if (src[i] == '\\') {
            if (src[i+1] == 'x' && i + 3 < slen) {
                dst->s[dst->l++] = (hex2num(src[i+2])<<4) | hex2num(src[i+3]);
                i += 3;

            } else {
                i++;
                dst->s[dst->l++] = (
                        src[i] == 'a' ? '\a' : (
                        src[i] == 'b' ? '\b' : (
                        src[i] == 'f' ? '\f' : (
                        src[i] == 'n' ? '\n' : (
                        src[i] == 'r' ? '\r' : (
                        src[i] == 't' ? '\t' : (
                        src[i] == 'v' ? '\v' : (
                        src[i] ))))))));
            }

        } else {
            dst->s[dst->l++] = src[i];
        }
    }

    /* dst->s = realloc(dst->s, dst->l); // who cares? */
}



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:44 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *