Annotation of nono/lib/memorystream.h, revision 1.1.1.6

1.1       root        1: //
                      2: // nono
1.1.1.2   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
1.1.1.4   root        7: //
                      8: // メモリストリーム
                      9: //
                     10: 
1.1       root       11: #pragma once
                     12: 
                     13: #include "header.h"
                     14: 
                     15: // ホストのメモリ領域に対するストリームクラスみたいなもの。
1.1.1.6 ! root       16: class MemoryStream
1.1       root       17: {
                     18:  public:
1.1.1.6 ! root       19:        MemoryStream(void *start_) {
1.1.1.4   root       20:                Init(start_);
1.1       root       21:        }
                     22: 
1.1.1.4   root       23:        // 開始ポインタを設定する
                     24:        // 現在のポインタ位置は開始ポインタ位置になる
                     25:        void Init(void *start_) {
                     26:                start = (uint8 *)start_;
                     27:                ptr = start;
                     28:        }
                     29: 
                     30:        // 開始ポインタを取得する
                     31:        void *GetStartPtr() const
                     32:        {
                     33:                return (void *)start;
                     34:        }
                     35: 
                     36:        // バイトオフセットを設定する
                     37:        void SetOffset(uint32 ofs)
                     38:        {
                     39:                ptr = start + ofs;
1.1       root       40:        }
                     41: 
1.1.1.4   root       42:        // 現在のバイトオフセットを取得する
                     43:        uint32 GetOffset() const
                     44:        {
                     45:                return (uint32)(ptr - start);
1.1       root       46:        }
                     47: 
                     48:        // 1バイト読み込んでポインタを進める
                     49:        uint32 Read8() {
1.1.1.3   root       50:                uint32 data = *ptr++;
1.1       root       51:                return data;
                     52:        }
1.1.1.6 ! root       53: 
        !            54:        // 1バイト書き込んでポインタを進める
        !            55:        void Write8(uint32 data) {
        !            56:                *ptr++ = data;
        !            57:        }
        !            58: 
        !            59:        // 文字列を書き込んでポインタを進める。
        !            60:        // 書き込む文字列も終端の '\0' まで書く。
        !            61:        void WriteString(const char *p) {
        !            62:                while (*p) {
        !            63:                        Write8(*p++);
        !            64:                }
        !            65:                Write8('\0');
        !            66:        }
        !            67: 
        !            68:  protected:
        !            69:        uint8 *ptr {};
        !            70:        uint8 *start {};
        !            71: };
        !            72: 
        !            73: // データの受け渡しはホストエンディアンで、
        !            74: // アクセスはビッグエンディアンになる。境界への整列は不要。
        !            75: class MemoryStreamBE : public MemoryStream
        !            76: {
        !            77:        using inherited = MemoryStream;
        !            78:  public:
        !            79:        MemoryStreamBE(void *start_)
        !            80:                : inherited(start_)
        !            81:        {
        !            82:        }
        !            83: 
1.1       root       84:        // BE で2バイト読み込んでポインタを進める
                     85:        uint32 Read16() {
1.1.1.3   root       86:                uint32 data;
                     87:                data  = Read8() << 8;
                     88:                data |= Read8();
                     89:                return data;
1.1       root       90:        }
                     91:        // BE で4バイト読み込んでポインタを進める
                     92:        uint32 Read32() {
1.1.1.3   root       93:                uint32 data;
                     94:                data  = Read16() << 16;
                     95:                data |= Read16();
                     96:                return data;
1.1       root       97:        }
                     98: 
                     99:        // BE で2バイト書き込んでポインタを進める
                    100:        void Write16(uint32 data) {
1.1.1.3   root      101:                Write8(data >> 8);
                    102:                Write8(data & 0xff);
1.1       root      103:        }
                    104:        // BE で4バイト書き込んでポインタを進める
                    105:        void Write32(uint32 data) {
1.1.1.3   root      106:                Write16(data >> 16);
                    107:                Write16(data & 0xffff);
1.1       root      108:        }
1.1.1.6 ! root      109: };
1.1       root      110: 
1.1.1.6 ! root      111: // データの受け渡しはホストエンディアンで、
        !           112: // アクセスはリトルエンディアンになる。境界への整列は不要。
        !           113: class MemoryStreamLE : public MemoryStream
        !           114: {
        !           115:        using inherited = MemoryStream;
        !           116:  public:
        !           117:        MemoryStreamLE(void *start_)
        !           118:                : inherited(start_)
        !           119:        {
1.1.1.5   root      120:        }
                    121: 
1.1.1.6 ! root      122:        // LE で2バイト読み込んでポインタを進める
        !           123:        uint32 Read16() {
        !           124:                uint32 data;
        !           125:                data  = Read8();
        !           126:                data |= Read8() << 8;
        !           127:                return data;
        !           128:        }
        !           129:        // LE で4バイト読み込んでポインタを進める
        !           130:        uint32 Read32() {
        !           131:                uint32 data;
        !           132:                data  = Read16();
        !           133:                data |= Read16() << 16;
        !           134:                return data;
1.1.1.5   root      135:        }
                    136: 
1.1.1.6 ! root      137:        // LE で2バイト書き込んでポインタを進める。
        !           138:        void Write16(uint32 data) {
        !           139:                Write8(data & 0xff);
        !           140:                Write8(data >> 8);
        !           141:        }
        !           142:        // LE で4バイト書き込んでポインタを進める。
        !           143:        void Write32(uint32 data) {
        !           144:                Write16(data & 0xffff);
        !           145:                Write16(data >> 16);
        !           146:        }
        !           147:        // LE で8バイト書き込んでポインタを進める。
        !           148:        void Write64(uint64 data) {
        !           149:                Write32((uint32)data);
        !           150:                Write32(data >> 32);
        !           151:        }
1.1       root      152: };

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.