It seems for me your test program do same thing as PostgreSQL backend
does. So I would say if the program worked well, why not PostgreSQL
works?

What do you think Oleg?

From: Maneerat Sappaso <[EMAIL PROTECTED]>
Subject: Program test strcoll().
Date: Thu, 18 Jan 2001 19:52:39 -0700 (GMT)
Message-ID: <[EMAIL PROTECTED]>

>       Deer sir,
> 
>       Program collsort.c is a test program in THAI locale version
>       th_TH-2.1.1-5.src.tar.gz for testing strcoll(). It sort by 
>       thai dictionary not by ascii. I use to test this program with 
>       thai or english (or thai+english) words and it sorted correctly. 
>       
>       I found that before run this program we must
>       setlocale(LC_COLLATE,"th_TH");
> 
>       When I install PostgreSQL with locale I try to test data by
>       use sql command like this " select * from table order by name"
>       The result are sorted by ascii.
>  
>       regards,
>       maneerat sappaso 

[the test program from Maneerat]

/*
 * collsort.c - a word list sorting tool using strcoll()
 * Created: 26 Nov 1998
 * Author: Theppitak Karoonboonyanan
 */

/*
   Copyright (C) 1999  Theppiak Karoonboonyanan
 
   collsort.c 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.
*/

#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef unsigned char tchar;

/* for qsort() */
typedef int (*CMPFUNC)(const void *, const void *);

static size_t readData(FILE *dataFile, tchar *data[], int maxData)
{
    size_t nData = 0;
    static char wordBuf[128];

    while (nData < maxData && fgets(wordBuf, sizeof wordBuf, dataFile) != NULL)
    {
        int len = strlen(wordBuf);
        if (len == 0) { return nData; }
        /* eliminate terminating '\n' */
        wordBuf[--len] = 0;

        /* allocate & copy the line */
        data[nData] = (tchar*)malloc(len+1);
        if (data[nData] == NULL) {
            printf("Warning: Only %d items were read\n", nData);
            return nData;
        }
        strcpy((char*)data[nData], wordBuf);
        nData++;
    }

    return nData;
}

static void freeData(tchar *data[], size_t nItems)
{
    size_t i;

    for (i=0; i<nItems; i++) {
        free(data[i]);
    }
}

static int dataCmp(const char **pStr1, const char **pStr2)
{
    return strcoll(*pStr1, *pStr2);
}

static void sortData(tchar *data[], size_t nItems)
{
    qsort(data, nItems, sizeof data[0], (CMPFUNC)dataCmp);
}

static void writeData(FILE *outFile, tchar *data[], size_t nItems)
{
    size_t  i;

    for (i = nItems; i > 0; i--) {
        fprintf(outFile, "%s\n", *data);
        data++;
    }
}

#define MAX_DATA  40000
static tchar *data[MAX_DATA];

int main(int argc, char *argv[])
{
    FILE   *dataFile;
    FILE   *outFile;
    size_t  dataRead;
    char    DataFileName[64];
    char    OutFileName[64];
    const char* pPrevLocale;

    pPrevLocale = setlocale(LC_COLLATE, "");
    if (pPrevLocale == 0) {
        fprintf(stderr, "Cannot set locale\n");
        exit(1);
    }

    if (argc == 3) {
        strcpy(DataFileName, argv[1]);
        strcpy(OutFileName, argv[2]);
    } else {
        fprintf(stderr, "Usage: collsort <input file> <output file>\n");
        return 1;
    }

    dataFile = fopen(DataFileName, "rt");
    if (dataFile == NULL) {
        fprintf(stderr, "Can't open file %s\n", DataFileName);
        perror("fopen");
        return 1;
    }

    outFile = fopen(OutFileName, "wt");
    if (outFile == NULL) {
        fprintf(stderr, "Can't open file %s for write\n", OutFileName);
        perror("fopen");
        return 1;
    }

    dataRead = readData(dataFile, data, MAX_DATA);
    sortData(data, dataRead);
    writeData(outFile, data, dataRead);
    freeData(data, dataRead);

    fclose(outFile);
    fclose(dataFile);

    setlocale(LC_COLLATE, pPrevLocale);

    return 0;
}

Reply via email to