Skip to content

Commit b27bc98

Browse files
committed
add data type support, add binary logic for encoding arg
Signed-off-by: Trae Yelovich <[email protected]>
1 parent 758ff16 commit b27bc98

File tree

8 files changed

+46
-27
lines changed

8 files changed

+46
-27
lines changed

native/c/zds.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ int zds_read_from_dd(ZDS *zds, string ddname, string &response)
7474
if (strlen(zds->encoding) > 0 /* && (*encoding != "IBM-1047" && *encoding != "01047") */)
7575
{
7676
char *bufEnd;
77-
char *outBuf = zut_encode_alloc(raw_data, size, string(zds->encoding), zds->diag, &bufEnd);
77+
// Convert from stored encoding (IBM-1047) to desired encoding
78+
char *outBuf = zut_encode_alloc(raw_data, size, "IBM-1047", string(zds->encoding), zds->diag, &bufEnd);
7879
if (outBuf)
7980
{
8081
response.clear();
@@ -90,7 +91,7 @@ int zds_read_from_dsn(ZDS *zds, string dsn, string &response)
9091
{
9192
dsn = "//'" + dsn + "'";
9293

93-
ifstream in(dsn.c_str());
94+
ifstream in(dsn.c_str(), zds->data_type == DataType::Binary ? ios::in | ios::binary : ios::in);
9495
if (!in.is_open())
9596
{
9697
zds->diag.e_msg_len = sprintf(zds->diag.e_msg, "Could not open file '%s'", dsn.c_str());
@@ -107,10 +108,13 @@ int zds_read_from_dsn(ZDS *zds, string dsn, string &response)
107108
response.assign(rawData);
108109
in.close();
109110

111+
const bool encodingProvided = zds->data_type == DataType::Text && strlen(zds->encoding) > 0;
112+
110113
char *bufEnd;
111-
if (strlen(zds->encoding) > 0 /* && (*encoding != "IBM-1047" && *encoding != "01047") */)
114+
if (encodingProvided /* && (*encoding != "IBM-1047" && *encoding != "01047") */)
112115
{
113-
char *outBuf = zut_encode_alloc(rawData, size, string(zds->encoding), zds->diag, &bufEnd);
116+
// Convert from stored encoding (IBM-1047) to desired encoding
117+
char *outBuf = zut_encode_alloc(rawData, size, "IBM-1047", string(zds->encoding), zds->diag, &bufEnd);
114118
if (outBuf)
115119
{
116120
response.clear();
@@ -145,7 +149,7 @@ int zds_write_to_dsn(ZDS *zds, string dsn, string &data)
145149
{
146150
const bool hasEncoding = strlen(zds->encoding) > 0;
147151
dsn = "//'" + dsn + "'";
148-
ofstream out(dsn.c_str(), hasEncoding ? ios::out | ios::binary : ios::out);
152+
ofstream out(dsn.c_str(), zds->data_type == DataType::Binary ? ios::out | ios::binary : ios::out);
149153

150154
if (!out.is_open())
151155
{
@@ -156,7 +160,8 @@ int zds_write_to_dsn(ZDS *zds, string dsn, string &data)
156160
if (hasEncoding)
157161
{
158162
char *bufEnd;
159-
char *outBuf = zut_encode_alloc((char *)data.c_str(), data.length(), string(zusf->encoding), zusf->diag, &bufEnd);
163+
// Convert from given encoding to IBM-1047 to store in dataset
164+
char *outBuf = zut_encode_alloc((char *)data.c_str(), data.length(), zds->encoding, "IBM-1047", zds->diag, &bufEnd);
160165
if (outBuf)
161166
{
162167
data.clear();

native/c/zdstype.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ typedef struct
5252
unsigned char version[1]; // future use
5353
int32_t len; // future use
5454

55-
char encoding[16]; // User-specified, desired encoding for dataset contents
55+
char encoding[16]; // User-specified, desired encoding for dataset contents
56+
DataType data_type; // Desired data type for dataset contents (text or binary)
5657

5758
int32_t max_entries;
5859
int32_t buffer_size;

native/c/zowex.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ int handle_data_set_view_dsn(ZCLIResult result)
747747
string dsn = result.get_positional("dsn").get_value();
748748
ZCLIOption &encoding = result.get_option("--encoding");
749749
ZDS zds = {0};
750+
zds.data_type = result.get_option("--encoding").get_value() == "binary" ? DataType::Binary : DataType::Text;
750751
string response;
751752
string encodingValue = encoding.get_value();
752753
const bool hasEncoding = !encodingValue.empty();
@@ -887,7 +888,7 @@ int handle_data_set_write_to_dsn(ZCLIResult result)
887888
data += line;
888889
data.push_back('\n');
889890
}
890-
891+
zds.data_type = result.get_option("--encoding").get_value() == "binary" ? DataType::Binary : DataType::Text;
891892
rc = zds_write_to_dsn(&zds, dsn, data);
892893

893894
if (0 != rc)
@@ -1037,6 +1038,8 @@ int handle_uss_view(ZCLIResult result)
10371038
string uss_file = result.get_positional("file-path").get_value();
10381039

10391040
ZUSF zusf = {0};
1041+
zusf.data_type = result.get_option("--encoding").get_value() == "binary" ? DataType::Binary : DataType::Text;
1042+
10401043
string response;
10411044
rc = zusf_read_from_uss_file(&zusf, uss_file, response);
10421045
if (0 != rc)
@@ -1069,7 +1072,7 @@ int handle_uss_write(ZCLIResult result)
10691072
data.push_back('\n');
10701073
}
10711074

1072-
rc = zds_write_to_uss_file(&zusf, file, data);
1075+
rc = zusf_write_to_uss_file(&zusf, file, data);
10731076
if (0 != rc)
10741077
{
10751078
cout << "Error: could not write to USS file: '" << file << "' rc: '" << rc << "'" << endl;
@@ -1102,7 +1105,7 @@ int handle_uss_chmod(ZCLIResult result)
11021105
mode = "755";
11031106

11041107
ZUSF zusf = {0};
1105-
rc = zds_chmod_uss_file_or_dir(&zusf, file_path, mode);
1108+
rc = zusf_chmod_uss_file_or_dir(&zusf, file_path, mode);
11061109
if (0 != rc)
11071110
{
11081111
cout << "Error: could not create USS path: '" << file_path << "' rc: '" << rc << "'" << endl;

native/c/ztype.h

+12-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
#include <stdint.h>
1414

15-
#define RTNCD_WARNING 1
16-
#define RTNCD_SUCCESS 0
15+
#define RTNCD_WARNING 1
16+
#define RTNCD_SUCCESS 0
1717
#define RTNCD_FAILURE -1
1818

1919
#if defined(__IBM_METAL__)
@@ -46,10 +46,11 @@
4646
#endif
4747

4848
// NOTE(Kelosky): struct is padded to nearest double word boundary; ensure proper alignment for fields
49-
typedef struct {
50-
char eye[3]; // future use
49+
typedef struct
50+
{
51+
char eye[3]; // future use
5152
unsigned char version[1]; // future use
52-
int32_t len; // future use
53+
int32_t len; // future use
5354

5455
char service_name[24];
5556

@@ -75,4 +76,10 @@ typedef struct {
7576
#pragma pack(reset)
7677
#endif
7778

79+
enum DataType
80+
{
81+
Text = 0,
82+
Binary = 1
83+
};
84+
7885
#endif

native/c/zusf.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ int zusf_list_uss_file_path(ZUSF *zusf, string file, string &response)
154154
*/
155155
int zusf_read_from_uss_file(ZUSF *zusf, string file, string &response)
156156
{
157-
ifstream in(file.c_str());
157+
ifstream in(file.c_str(), zusf->data_type == DataType::Binary ? ios::in | ios::binary : ios::in);
158158
if (!in.is_open())
159159
{
160160
zusf->diag.e_msg_len = sprintf(zusf->diag.e_msg, "Could not open file '%s'", file.c_str());
@@ -174,13 +174,13 @@ int zusf_read_from_uss_file(ZUSF *zusf, string file, string &response)
174174
char tagged_encoding[16] = {0};
175175
// ssize_t xattr_result = getxattr(file.c_str(), "system.filetag", &zusf->encoding);
176176

177-
const bool encodingProvided = strlen(zusf->encoding) > 0;
177+
const bool encodingProvided = zusf->data_type == DataType::Text && strlen(zusf->encoding) > 0;
178178

179179
char *bufEnd;
180180
if (encodingProvided /* && (*encoding != "IBM-1047" && *encoding != "01047") */)
181181
{
182182
// const encoding = encodingProvided ? string(zusf->encoding) : string(tagged_encoding);
183-
char *outBuf = zut_encode_alloc(rawData, size, string(zusf->encoding), zusf->diag, &bufEnd);
183+
char *outBuf = zut_encode_alloc(rawData, size, "IBM-1047", string(zusf->encoding), zusf->diag, &bufEnd);
184184
if (outBuf)
185185
{
186186
response.clear();
@@ -203,21 +203,21 @@ int zusf_read_from_uss_file(ZUSF *zusf, string file, string &response)
203203
*
204204
* @return RTNCD_SUCCESS on success, RTNCD_FAILURE on failure
205205
*/
206-
int zds_write_to_uss_file(ZUSF *zusf, string file, string &data)
206+
int zusf_write_to_uss_file(ZUSF *zusf, string file, string &data)
207207
{
208208
// TODO(zFernand0): Avoid overriding existing files
209-
const bool withEncoding = strlen(zusf->encoding) > 0;
210-
ofstream out(file.c_str(), withEncoding ? ios::out | ios::binary : ios::out);
209+
const bool hasEncoding = strlen(zusf->encoding) > 0;
210+
ofstream out(file.c_str(), zusf->data_type == DataType::Binary ? ios::out | ios::binary : ios::out);
211211
if (!out.is_open())
212212
{
213213
zusf->diag.e_msg_len = sprintf(zusf->diag.e_msg, "Could not open '%s'", file.c_str());
214214
return RTNCD_FAILURE;
215215
}
216216

217-
if (strlen(zusf->encoding) > 0)
217+
if (hasEncoding)
218218
{
219219
char *bufEnd;
220-
char *outBuf = zut_encode_alloc((char *)data.c_str(), data.length(), string(zusf->encoding), zusf->diag, &bufEnd);
220+
char *outBuf = zut_encode_alloc((char *)data.c_str(), data.length(), string(zusf->encoding), "IBM-1047", zusf->diag, &bufEnd);
221221
if (outBuf)
222222
{
223223
data.clear();
@@ -241,7 +241,7 @@ int zds_write_to_uss_file(ZUSF *zusf, string file, string &data)
241241
*
242242
* @return RTNCD_SUCCESS on success, RTNCD_FAILURE on failure
243243
*/
244-
int zds_chmod_uss_file_or_dir(ZUSF *zusf, string file, string mode)
244+
int zusf_chmod_uss_file_or_dir(ZUSF *zusf, string file, string mode)
245245
{
246246
// TODO(zFernand0): Add recursive option for directories
247247
struct stat file_stats;

native/c/zusf.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
int zusf_create_uss_file_or_dir(ZUSF *zusf, std::string file, std::string mode, bool createDir);
2020
int zusf_list_uss_file_path(ZUSF *zusf, std::string file, std::string &response);
2121
int zusf_read_from_uss_file(ZUSF *zusf, std::string file, std::string &response);
22-
int zds_write_to_uss_file(ZUSF *zusf, std::string file, std::string &response);
23-
int zds_chmod_uss_file_or_dir(ZUSF *zusf, std::string file, std::string mode);
22+
int zusf_write_to_uss_file(ZUSF *zusf, std::string file, std::string &response);
23+
int zusf_chmod_uss_file_or_dir(ZUSF *zusf, std::string file, std::string mode);
2424

2525
#endif

native/c/zusftype.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
// NOTE(zFernand0): Figure out how to visualize the struct in memory
3030
// NOTE(Kelosky): struct is padded to nearest double word boundary; ensure proper alignment for fields
31+
3132
typedef struct
3233
{
3334
char eye[3]; // future use
@@ -37,7 +38,8 @@ typedef struct
3738
int16_t mode; // permissions
3839
char file_name[1024]; // filename
3940

40-
char encoding[16]; // User-specified, desired encoding for USS contents
41+
char encoding[16]; // User-specified, desired encoding for USS contents
42+
DataType data_type; // Desired data type for USS contents (text or binary)
4143

4244
ZDIAG diag;
4345

native/golang/messages.go

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type WriteDatasetRequest struct {
7575
// command: "writeFile"
7676
type WriteFileRequest struct {
7777
Encoding string `json:"encoding,omitempty"`
78+
DataType string `json:"dataType,omitempty"`
7879
Path string `json:"path"`
7980
Contents string `json:"contents"`
8081
}

0 commit comments

Comments
 (0)