OpenVMS
HP C ランタイム・ライブラリ・
リファレンス・マニュアル (下巻)


前へ 次へ 目次 索引



/* Exercise the wcsftime formating routine.             */ 
/* NOTE: the format string is an "L" (or wide character) */ 
/*       string indicating that this call is NOT in      */ 
/*       the XPG4 format, but rather in ISO C format.    */ 
 
#include <stdlib.h> 
#include <stdio.h> 
#include <time.h> 
#include <wchar.h> 
#include <locale.h> 
#include <errno.h> 
 
#define NUM_OF_DATES  7 
#define BUF_SIZE 256 
 
/* This program formats a number of different dates, once using the    */ 
/* C locale and then using the fr_FR.ISO8859-1 locale.  Date and time  */ 
/* formatting is done using wcsftime().                                */ 
 
main() 
 
{ 
    int count, 
        i; 
    wchar_t buffer[BUF_SIZE]; 
    struct tm *tm_ptr; 
    time_t time_list[NUM_OF_DATES] = 
    {500, 68200000, 694223999, 
     694224000, 704900000, 705000000, 
     705900000}; 
 
    /* Display dates using the C locale */ 
    printf("\nUsing the C locale:\n\n"); 
 
    setlocale(LC_ALL, "C"); 
 
    for (i = 0; i < NUM_OF_DATES; i++) { 
        /* Convert to a tm structure */ 
        tm_ptr = localtime(&time_list[i]); 
 
        /* Format the date and time */ 
        count = wcsftime(buffer, BUF_SIZE, L"Date: %A %d %B %Y%nTime: %T%n%n", 
                        tm_ptr); 
        if (count == 0) { 
            perror("wcsftime"); 
            exit(EXIT_FAILURE); 
        } 
 
        /* Print the result */ 
        printf("%S", buffer); 
    } 
 
    /* Display dates using the fr_FR.ISO8859-1 locale */ 
    printf("\nUsing the fr_FR.ISO8859-1 locale:\n\n"); 
 
    setlocale(LC_ALL, "fr_FR.ISO8859-1"); 
 
    for (i = 0; i < NUM_OF_DATES; i++) { 
        /* Convert to a tm structure */ 
        tm_ptr = localtime(&time_list[i]); 
 
        /* Format the date and time */ 
        count = wcsftime(buffer, BUF_SIZE, L"Date: %A %d %B %Y%nTime: %T%n%n", 
                         tm_ptr); 
        if (count == 0) { 
            perror("wcsftime"); 
            exit(EXIT_FAILURE); 
        } 
 
        /* Print the result */ 
        printf("%S", buffer); 
    } 
} 

この例のプログラムを実行すると,次の結果が生成されます。


Using the C locale: 
 
Date: Thursday 01 January 1970 
Time: 00:08:20 
 
Date: Tuesday 29 February 1972 
Time: 08:26:40 
 
Date: Tuesday 31 December 1991 
Time: 23:59:59 
 
Date: Wednesday 01 January 1992 
Time: 00:00:00 
 
Date: Sunday 03 May 1992 
Time: 13:33:20 
 
Date: Monday 04 May 1992 
Time: 17:20:00 
 
Date: Friday 15 May 1992 
Time: 03:20:00 
 
 
Using the fr_FR.ISO8859-1 locale: 
 
Date: jeudi 01 janvier 1970 
Time: 00:08:20 
 
Date: mardi 29 f□rier 1972 
Time: 08:26:40 
 
Date: mardi 31 d□embre 1991 
Time: 23:59:59 
 
Date: mercredi 01 janvier 1992 
Time: 00:00:00 
 
Date: dimanche 03 mai 1992 
Time: 13:33:20 
 
Date: lundi 04 mai 1992 
Time: 17:20:00 
 
Date: vendredi 15 mai 1992 
Time: 03:20:00 


wcslen

ワイド文字列の中のワイド文字の数を返します。返される長さには,終端の null 文字は含まれません。

形式

#include <wchar.h>

size_t wcslen (const wchar_t *wstr);


引数

wstr

null で終了するワイド文字列へのポインタ。

戻り値

x 終端の null ワイド文字を除いたワイド文字列の長さ。


wcsncat

1 つの文字列の一定数のワイド文字を別の文字列に連結します。

形式

#include <wchar.h>

wchar_t *wcsncat (wchar_t *wstr_1, const wchar_t *wstr_2, size_t maxchar);

関数バリアント wcsncat関数は,それぞれ 32 ビットと 64 ビットのポインタ・サイズで使用するための _wcsncat32_wcsncat64という名前のバリアントを持っています。ポインタ・サイズ固有の関数の使用方法については,『HP C ランタイム・ライブラリ・リファレンス・マニュアル(上巻)』第 1.10 節を参照してください。

引数

wstr_1, wstr_2

null で終了するワイド文字列へのポインタ。

maxchar

wstr_2 から wstr_1 にコピーされるワイド文字の数の最大値。maxchar が 0 の場合, wstr_2 から文字はコピーされません。

説明

wcsncat関数は,ワイド文字列 wstr_2 から wstr_1 の末尾に,最高 maxchar 個のワイド文字を追加します。 wcsncat関数の結果には,終端の null ワイド文字がつねに追加されます。このため,wstr_1 に格納されるワイド文字の数の最大値は wcslen(wstr_1) + maxchar + 1) です。

wcscatも参照してください。


戻り値

x 連結された結果を保持できるだけの大きさを持つと仮定される第 1 引数 wstr_1



#include <stdlib.h> 
#include <stdio.h> 
#include <wchar.h> 
#include <string.h> 
 
/* This program concatenates two wide-character strings using   */ 
/* the wcsncat function, and then manually compares the result  */ 
/* to the expected result                                       */ 
 
#define S1LENGTH 10 
#define S2LENGTH 8 
#define SIZE     3 
 
main() 
{ 
    int i; 
    wchar_t s1buf[S1LENGTH + S2LENGTH]; 
    wchar_t s2buf[S2LENGTH]; 
    wchar_t test1[S1LENGTH + S2LENGTH]; 
 
    /* Initialize the three wide-character strings */ 
 
 
    if (mbstowcs(s1buf, "abcmnexyz", S1LENGTH) == (size_t)-1) { 
 
        perror("mbstowcs"); 
        exit(EXIT_FAILURE); 
    } 
 
 
    if (mbstowcs(s2buf, " orthis", S2LENGTH) == (size_t)-1) { 
 
        perror("mbstowcs"); 
        exit(EXIT_FAILURE); 
    } 
 
    if (mbstowcs(test1, "abcmnexyz orthis", S1LENGTH + SIZE) 
 
        == (size_t)-1) { 
 
        perror("mbstowcs"); 
        exit(EXIT_FAILURE); 
    } 
 
/* Concatenate s1buf with SIZE characters from s2buf, placing the */ 
/* result into s1buf. Then compare s1buf with the expected result */ 
/* in test1.                                                      */ 
 
    wcsncat(s1buf, s2buf, SIZE); 
 
    for (i = 0; i <= S1LENGTH + SIZE - 2; i++) { 
        /* Check that each character is correct */ 
        if (test1[i] != s1buf[i]) { 
            printf("Error in wcsncat\n"); 
            exit(EXIT_FAILURE); 
        } 
 
    } 
 
    printf("Concatenated string: <%S>\n", s1buf); 
} 

この例のプログラムを実行すると,次の結果が生成されます。


Concatenated string: <abcmnexyz or> 


wcsncmp

2 つのワイド文字列の中の文字を,最高 maxchar 個比較します。文字列が異なるかどうか,またどのように異なるかを示す整数を返します。

形式

#include <wchar.h>

int wcsncmp (const wchar_t *wstr_1, const wchar_t *wstr_2, size_t maxchar);


引数

wstr_1, wstr_2

null で終了するワイド文字列へのポインタ。

maxchar

wstr_1wstr_2 の両方で探す文字の数の最大値。 maxchar が 0 の場合,比較は実行されず, 0 が返されます ( 文字列は等しいと見なされます )。

説明

文字列は null 文字が検出されるか,文字列に違いが検出されるか,または maxchar に達するまで比較されます。文字が異なる場合, wcsncmpは次の値を返します。

maxchar 個の文字を比較しても違いが発見されなかった場合,関数は 0 を返します。

wcscmpも参照してください。


戻り値

< 0 wstr_1wstr_2 よりも小さいことを示します。
0 wstr_1wstr_2 と等しいことを示します。
> 0 wstr_1wstr_2 よりも大きいことを示します。


wcsncpy

source から dest にワイド文字をコピーします。関数は最高 maxchar 個の文字をコピーします。

形式

#include <wchar.h>

wchar_t *wcsncpy (wchar_t *dest, const wchar_t *source, size_t maxchar);

関数バリアント wcsncpy関数は,それぞれ 32 ビットと 64 ビットのポインタ・サイズで使用するための _wcsncpy32_wcsncpy64という名前のバリアントを持っています。ポインタ・サイズ固有の関数の使用方法については,『HP C ランタイム・ライブラリ・リファレンス・マニュアル(上巻)』第 1.10 節を参照してください。

引数

dest

null で終了するワイド文字デスティネーション文字列へのポインタ。

source

null で終了するワイド文字ソース文字列へのポインタ。

maxchar

source から dest にコピーするワイド文字の数の最大値。

説明

wcsncpy関数は, source から dest に最高 maxchar 個の文字をコピーします。 source に含まれている文字が maxchar 個未満である場合には, maxchar 個の文字が書き込まれるまで, dest には null 文字が追加されます。

source に含まれている文字が maxchar 個以上である場合には,可能な限り多くの文字が dest にコピーされます。 source の終端の null 文字は dest にはコピーされません。

wcscpyも参照してください。


戻り値

x dest のアドレス。


wcspbrk

ワイド文字列の中で,指定されたワイド文字のセットの 1 つの最初のオカレンスを検索します。

形式

#include <wchar.h>

wchar_t *wcspbrk (const wchar_t *wstr, const wchar_t *charset);

関数バリアント wcspbrk関数は,それぞれ 32 ビットと 64 ビットのポインタ・サイズで使用するための _wcspbrk32_wcspbrk64という名前のバリアントを持っています。ポインタ・サイズ固有の関数の使用方法については,『HP C ランタイム・ライブラリ・リファレンス・マニュアル(上巻)』第 1.10 節を参照してください。

引数

wstr

ワイド文字列へのポインタ。これが null 文字列だった場合には, NULL が返されます。

charset

関数が探すワイド文字のセットを含んでいるワイド文字列へのポインタ。

説明

wcspbrk関数は文字列の中のワイド文字をスキャンし, charset に含まれているワイド文字を検出した時点で停止し,文字列の中の,文字セットに含まれる最初の文字のアドレスを返します。

戻り値

x 文字列の中の,セットに含まれる最初のワイド文字のアドレス。
NULL どの文字も charset に含まれていないことを示します。


wcsrchr

指定された文字列の中のワイド文字の最後のオカレンスをスキャンします。

形式

#include <wchar.h>

wchar_t *wcsrchr (const wchar_t *wstr, wchar_t wc);

関数バリアント wcsrchr関数は,それぞれ 32 ビットと 64 ビットのポインタ・サイズで使用するための _wcsrchr32_wcsrchr64という名前のバリアントを持っています。ポインタ・サイズ固有の関数の使用方法については,『HP C ランタイム・ライブラリ・リファレンス・マニュアル(上巻)』第 1.10 節を参照してください。

引数

wstr

null で終了するワイド文字列へのポインタ。

wc

wchar_t型の文字。

説明

wcsrchr関数は,null で終了するワイド文字列の中の,指定されたワイド文字の最後のオカレンスのアドレスを返します。終端の null 文字は文字列の一部と見なされます。

wcschrも参照してください。


戻り値

x 指定されたワイド文字の最後のオカレンスのアドレス。
NULL ワイド文字が文字列に含まれていないことを示します。



#include <stdlib.h> 
#include <stdio.h> 
#include <wchar.h> 
#include <string.h> 
 
#define BUFF_SIZE 50 
#define STRING_SIZE 6 
 
main() 
{ 
    int i; 
    wchar_t s1buf[BUFF_SIZE], 
            w_string[STRING_SIZE]; 
    wchar_t *status; 
    wchar_t *pbuf = s1buf; 
 
    /* Initialize the buffer */ 
 
    if (mbstowcs(s1buf, "hijklabcdefg ytuhijklfedcba", BUFF_SIZE) 
 
        == (size_t)-1) { 
 
        perror("mbstowcs"); 
        exit(EXIT_FAILURE); 
    } 
 
    /* Initialize the string to be searched for */ 
 
 
    if (mbstowcs(w_string, "hijkl", STRING_SIZE) == (size_t)-1) { 
 
        perror("mbstowcs"); 
        exit(EXIT_FAILURE); 
   } 
 
/* This program checks the wcsrchr function by searching for */ 
/* the last occurrence of a string in the buffer s1buf and   */ 
/* prints out the contents of s1buff from the location of 
/* the string found.                                         */ 
 
    status = wcsrchr(s1buf, w_string[0]); 
/* Check for pointer to start of rightmost character string. */ 
    if (status == pbuf) { 
        printf("Error in wcsrchr\n"); 
        exit(EXIT_FAILURE); 
    } 
 
    printf("Program completed successfully\n"); 
    printf("String found : [%S]\n", status); 
 
} 

この例のプログラムを実行すると,次の結果が生成されます。


Program completed successfully 
String found : [hijklfedcba] 


wcsrtombs

ワイド文字のシーケンスを,対応するマルチバイト文字のシーケンスに変換します。

形式

#include <wchar.h>

size_t wcsrtombs (char *dst, const wchar_t **src, size_t len, mbstate_t *ps);

関数バリアント wcsrtombs関数は,それぞれ 32 ビットと 64 ビットのポインタ・サイズで使用するための _wcsrtombs32_wcsrtombs64という名前のバリアントを持っています。ポインタ・サイズ固有の関数の使用方法については,『HP C ランタイム・ライブラリ・リファレンス・マニュアル(上巻)』第 1.10 節を参照してください。

引数

dst

変換されたマルチバイト文字シーケンスのためのデスティネーション配列へのポインタ。

src

変換するワイド文字のシーケンスを含んだ配列へのポインタのアドレス。

len

dst がポイントする配列に格納できるバイト数の最大値。

ps

mbstate_tオブジェクトへのポインタ。NULL ポインタが指定された場合,関数は内部の mbstate_tオブジェクトを使用します。 mbstate_tは,状態依存のコードセットの変換状態を保持することを目的とする不透明のデータ型です。

説明

wcsrtombs関数は,src が間接的にポイントする配列の中のワイド文字のシーケンスを,ps がポイントするオブジェクトによって記述される変換状態を出発点として,対応するマルチバイト文字のシーケンスに変換します。

dst が NULL ポインタでない場合,変換された文字は dst がポイントする配列に格納されます。変換は終端の null ワイド文字まで続けられ,その null ワイド文字も格納されます。

変換は次の 2 つの場合に停止します。

個々の変換は, wcrtomb関数を呼び出したかのように実行されます。

dst が NULL ポインタでない場合,src がポイントするポインタ・オブジェクトには,NULL ポインタ (終端の null ワイド文字に達したために変換が停止した場合) か,変換された最後のワイド文字の直後のアドレス (存在する場合) が代入されます。終端の null ワイド文字に達したために変換が終了した場合,記述される結果の状態は初期変換状態です。

dst に NULL ポインタを指定して, wcsrtombs関数をカウント関数として呼び出した場合,内部の mbstate_tオブジェクトの値は変更されません。

wcrtombも参照してください。


戻り値

x 結果として得られる配列に格納されたバイト数。終端の null は (存在していても) 含みません。
- 1 エンコーディング・エラーを示します。有効なマルチバイト文字に対応しない文字が検出されました。 errno EILSEQ に設定されます。変換状態は未定義です。


wcsspn

ワイド文字列の中の文字を,ワイド文字のセットと比較します。関数は,ワイド文字のセットに含まれる文字のみから構成される最初の部分文字列の長さを返します。

形式

#include <wchar.h>

size_t wcsspn (const wchar_t *wstr1, const wchar_t *wstr2);


引数

wstr1

null で終了するワイド文字列へのポインタ。この文字列が null 文字列だった場合には,0 が返されます。

wstr2

関数が探すワイド文字のセットを含んでいる, null で終了するワイド文字列へのポインタ。

説明

wcsspn関数は,wstr2 に含まれない文字を検出するまで,wstr1 がポイントするワイド文字列の中でワイド文字をスキャンします。関数は,wstr2 に含まれる文字から構成される, wstr1 の最初のセグメントの長さを返します。

戻り値

x セグメントの長さ。



#include <stdlib.h> 
#include <stdio.h> 
#include <wchar.h> 
#include <string.h> 
 
/* This test sets up 2 strings, buffer and w_string. It */ 
/* then uses wcsspn() to calculate the maximum segment  */ 
/* of w_string that consists entirely of characters     */ 
/* from buffer.                                         */ 
 
#define BUFF_SIZE 20 
#define STRING_SIZE 50 
 
main() 
{ 
    wchar_t buffer[BUFF_SIZE]; 
    wchar_t w_string[STRING_SIZE]; 
    size_t result; 
 
    /* Initialize the buffer */ 
 
 
    if (mbstowcs(buffer, "abcdefg", BUFF_SIZE) == (size_t)-1) { 
 
        perror("mbstowcs"); 
        exit(EXIT_FAILURE); 
    } 
 
    /* Initialize the string */ 
 
    if (mbstowcs(w_string, "abcedjklmabcjklabcdehjkl", STRING_SIZE) 
 
        == (size_t)-1) { 
 
        perror("mbstowcs"); 
        exit(EXIT_FAILURE); 
    } 
 
/* Using wcsspn - work out the largest string in w_string */ 
/* that consists entirely of characters from buffer       */ 
 
    result = wcsspn(w_string, buffer); 
    printf("Longest segment found in w_string is: %d", result); 
 
} 

この例のプログラムを実行すると,次の結果が生成されます。


Longest segment found in w_string is: 5 


前へ 次へ 目次 索引