root/lang/ruby/first_ci/Shape.cpp @ 1980

Revision 1980, 2.4 kB (checked in by gyuque, 6 years ago)

imported

Line 
1#include <stdio.h>
2#include <malloc.h>
3#include "Shape.h"
4
5Shape::Shape(void)
6{
7        mBaseName = NULL;
8        memset(&mSHPFHeader, 0, sizeof(mSHPFHeader));
9}
10
11Shape::~Shape(void)
12{
13        clearBaseName();
14}
15
16es_error_t Shape::readFromFile(const TCHAR* filename)
17{
18        es_error_t rv = ESHP_OK;
19
20        storeBaseName(filename);
21
22        TCHAR* actualName = (TCHAR*)malloc(sizeof(TCHAR) * (_tcslen(filename) + 5));
23        _tcscpy(actualName, filename);
24
25        size_t orgLen = _tcslen(actualName);
26        _tcscpy(actualName + orgLen, ".shp");
27        if ((rv = readMainFile(actualName)) == ESHP_OK)
28        {
29                _tcscpy(actualName + orgLen, ".shx");
30                _tcscpy(actualName + orgLen, ".dbf");
31        }
32
33        free(actualName);
34
35        return rv;
36}
37
38es_error_t Shape::readMainFile(const TCHAR* filename)
39{
40        FILE* fp = fopen(filename, "rb");
41        if (NULL == fp)
42                return ESHP_CANNOTOPEN;
43
44        _tprintf(_T("%s\n"), filename);
45
46        mSHPFHeader.filecode   = freadBig32(fp);
47        mSHPFHeader.rsv1       = freadBig32(fp);
48        mSHPFHeader.rsv2       = freadBig32(fp);
49        mSHPFHeader.rsv3       = freadBig32(fp);
50        mSHPFHeader.rsv4       = freadBig32(fp);
51        mSHPFHeader.rsv5       = freadBig32(fp);
52        mSHPFHeader.filelength = freadBig32(fp);
53
54        mSHPFHeader.version    = freadLittle32(fp);
55        mSHPFHeader.shapetype  = freadLittle32(fp);
56
57        _tprintf(_T("%d\n"), mSHPFHeader.filecode);
58        _tprintf(_T("%d\n"), mSHPFHeader.rsv1);
59        _tprintf(_T("%d\n"), mSHPFHeader.rsv2);
60        _tprintf(_T("%d\n"), mSHPFHeader.rsv3);
61        _tprintf(_T("%d\n"), mSHPFHeader.rsv4);
62        _tprintf(_T("%d\n"), mSHPFHeader.rsv5);
63        _tprintf(_T("%d\n"), mSHPFHeader.filelength);
64        _tprintf(_T("%d\n"), mSHPFHeader.version);
65        _tprintf(_T("%d\n"), mSHPFHeader.shapetype);
66
67        fclose(fp);
68
69        return ESHP_OK;
70}
71
72void Shape::clearBaseName()
73{
74        if (mBaseName)
75        {
76                free(mBaseName);
77                mBaseName = NULL;
78        }
79}
80
81void Shape::storeBaseName(const TCHAR* baseName)
82{
83        clearBaseName();
84        mBaseName = _tcsdup(baseName);
85}
86
87ESInt32 Shape::freadBig32(FILE* fp)
88{
89        ESInt32 i;
90        fread(&i, sizeof(ESInt32), 1, fp);
91        return (i>>24) | ((i>>8)&0xff00) | ((i<<8)&0xff0000) | (i<<24);
92}
93
94ESInt32 Shape::freadLittle32(FILE* fp)
95{
96        ESInt32 i;
97        fread(&i, sizeof(ESInt32), 1, fp);
98        return i;
99}
100
101void* c_Shape_new()
102{return new Shape();}
103
104void c_Shape_delete(void* pThis)
105{delete (Shape*)pThis;}
106
107es_error_t c_Shape_readFromFile(void* pThis, const TCHAR* filename)
108{return ((Shape*)pThis)->readFromFile(filename);}
Note: See TracBrowser for help on using the browser.