@@ -908,6 +908,139 @@ PY_EXPORT PyObject* gdsnRead2(PyObject *self, PyObject *args)
908
908
}
909
909
910
910
911
+ // / Write data to a node
912
+ // PY_EXPORT PyObject* gdsnWrite(PyObject *self, PyObject *args)
913
+ // {
914
+ /* * \param Node [in] a GDS node
915
+ * \param Val [in] the values
916
+ * \param Start [in] the starting positions
917
+ * \param Count [in] the count
918
+ * \param Check [in] if TRUE, check any(is.na(val)) if val is character
919
+ **/
920
+
921
+ /*
922
+ if (!Rf_isNumeric(Val) && !Rf_isString(Val) && !Rf_isLogical(Val) &&
923
+ !Rf_isFactor(Val) && (TYPEOF(Val)!=RAWSXP))
924
+ error("'val' should be integer, numeric, character, logical or raw.");
925
+ if (!Rf_isNull(Start) && !Rf_isNumeric(Start))
926
+ error("'start' should be numeric.");
927
+ if (!Rf_isNull(Count) && !Rf_isNumeric(Count))
928
+ error("'count' should be numeric.");
929
+ if ((Rf_isNull(Start) && !Rf_isNull(Count)) ||
930
+ (!Rf_isNull(Start) && Rf_isNull(Count)))
931
+ error("'start' and 'count' should be both NULL.");
932
+ if (!Rf_isLogical(Check) || (XLENGTH(Check) <= 0))
933
+ error("'check' should be a logical variable.");
934
+
935
+ // GDS object
936
+ CdAbstractArray *Obj;
937
+ {
938
+ bool has_error = false;
939
+ CORE_TRY
940
+ Obj = dynamic_cast<CdAbstractArray*>(GDS_R_SEXP2Obj(Node, FALSE));
941
+ if (Obj == NULL)
942
+ throw ErrGDSFmt(ERR_NO_DATA);
943
+ CORE_CATCH(has_error = true);
944
+ if (has_error) error(GDS_GetError());
945
+ }
946
+
947
+ CdAbstractArray::TArrayDim DStart, DLen;
948
+ if (!Rf_isNull(Start) && !Rf_isNull(Count))
949
+ {
950
+ int Len = Obj->DimCnt();
951
+ CdAbstractArray::TArrayDim DCnt;
952
+ Obj->GetDim(DCnt);
953
+
954
+ PROTECT(Start = Rf_coerceVector(Start, INTSXP));
955
+ if (XLENGTH(Start) != Len)
956
+ error("The length of 'start' is invalid.");
957
+ for (int i=0; i < Len; i++)
958
+ {
959
+ int v = INTEGER(Start)[i];
960
+ if ((v < 1) || (v > DCnt[Len-i-1]))
961
+ error("'start' is invalid.");
962
+ DStart[Len-i-1] = v-1;
963
+ }
964
+
965
+ PROTECT(Count = Rf_coerceVector(Count, INTSXP));
966
+ if (XLENGTH(Count) != Len)
967
+ error("The length of 'count' is invalid.");
968
+ for (int i=0; i < Len; i++)
969
+ {
970
+ int v = INTEGER(Count)[i];
971
+ if (v == -1) v = DCnt[Len-i-1];
972
+ if ((v < 0) || ((DStart[Len-i-1]+v) > DCnt[Len-i-1]))
973
+ error("'count' is invalid.");
974
+ DLen[Len-i-1] = v;
975
+ }
976
+
977
+ UNPROTECT(2);
978
+
979
+ C_Int64 Cnt = 1;
980
+ for (int i=0; i < Len; i++)
981
+ Cnt *= DLen[i];
982
+ if (Cnt != (C_Int64)Rf_length(Val))
983
+ error("Invalid length of dimension of 'val'.");
984
+ }
985
+
986
+ COREARRAY_TRY
987
+
988
+ int nProtected = 0;
989
+ C_SVType ObjSV = Obj->SVType();
990
+
991
+ if (COREARRAY_SV_INTEGER(ObjSV))
992
+ {
993
+ if (TYPEOF(Val) != RAWSXP)
994
+ {
995
+ PROTECT(Val = Rf_coerceVector(Val, INTSXP));
996
+ nProtected ++;
997
+ Obj->WriteData(DStart, DLen, INTEGER(Val), svInt32);
998
+ } else {
999
+ Obj->WriteData(DStart, DLen, RAW(Val), svInt8);
1000
+ }
1001
+ } else if (COREARRAY_SV_FLOAT(ObjSV))
1002
+ {
1003
+ PROTECT(Val = Rf_coerceVector(Val, REALSXP));
1004
+ nProtected ++;
1005
+ Obj->WriteData(DStart, DLen, REAL(Val), svFloat64);
1006
+ } else if (COREARRAY_SV_STRING(ObjSV))
1007
+ {
1008
+ PROTECT(Val = Rf_coerceVector(Val, STRSXP));
1009
+ nProtected ++;
1010
+ R_xlen_t Len = XLENGTH(Val);
1011
+ if (Rf_asLogical(Check) == TRUE)
1012
+ {
1013
+ for (R_xlen_t i=0; i < Len; i++)
1014
+ {
1015
+ if (STRING_ELT(Val, i) == NA_STRING)
1016
+ {
1017
+ warning("Missing characters are converted to \"\".");
1018
+ break;
1019
+ }
1020
+ }
1021
+ }
1022
+ vector<UTF8String> buf(Len);
1023
+ for (R_xlen_t i=0; i < Len; i++)
1024
+ {
1025
+ SEXP s = STRING_ELT(Val, i);
1026
+ if (s != NA_STRING)
1027
+ buf[i] = UTF8Text(translateCharUTF8(s));
1028
+ }
1029
+ Obj->WriteData(DStart, DLen, &(buf[0]), svStrUTF8);
1030
+ } else
1031
+ throw ErrGDSFmt("No support!");
1032
+
1033
+ UNPROTECT(nProtected);
1034
+ }
1035
+ catch (ErrAllocWrite &E) {
1036
+ GDS_SetError(ERR_READ_ONLY);
1037
+ has_error = true;
1038
+
1039
+ COREARRAY_CATCH_NONE
1040
+ }
1041
+ */
1042
+
1043
+
911
1044
912
1045
// ----------------------------------------------------------------------------
913
1046
// Attribute Operations
@@ -976,28 +1109,29 @@ extern COREARRAY_DLL_LOCAL bool pygds_init();
976
1109
977
1110
static PyMethodDef module_methods[] = {
978
1111
// file operations
979
- { " create_gds" , (PyCFunction)gdsCreateGDS, METH_VARARGS, NULL },
980
- { " open_gds" , (PyCFunction)gdsOpenGDS, METH_VARARGS, NULL },
981
- { " close_gds" , (PyCFunction)gdsCloseGDS, METH_VARARGS, NULL },
982
- { " sync_gds" , (PyCFunction)gdsSyncGDS, METH_VARARGS, NULL },
983
- { " filesize" , (PyCFunction)gdsFileSize, METH_VARARGS, NULL },
984
- { " tidy_up" , (PyCFunction)gdsTidyUp, METH_VARARGS, NULL },
985
- { " root_gds" , (PyCFunction)gdsRoot, METH_VARARGS, NULL },
986
- { " index_gds" , (PyCFunction)gdsIndex, METH_VARARGS, NULL },
1112
+ { " create_gds" , (PyCFunction)gdsCreateGDS, METH_VARARGS, NULL },
1113
+ { " open_gds" , (PyCFunction)gdsOpenGDS, METH_VARARGS, NULL },
1114
+ { " close_gds" , (PyCFunction)gdsCloseGDS, METH_VARARGS, NULL },
1115
+ { " sync_gds" , (PyCFunction)gdsSyncGDS, METH_VARARGS, NULL },
1116
+ { " filesize" , (PyCFunction)gdsFileSize, METH_VARARGS, NULL },
1117
+ { " tidy_up" , (PyCFunction)gdsTidyUp, METH_VARARGS, NULL },
1118
+ { " root_gds" , (PyCFunction)gdsRoot, METH_VARARGS, NULL },
1119
+ { " index_gds" , (PyCFunction)gdsIndex, METH_VARARGS, NULL },
987
1120
988
1121
// file structure operations
989
- { " ls_gdsn" , (PyCFunction)gdsnListName, METH_VARARGS, NULL },
990
- { " index_gdsn" , (PyCFunction)gdsnIndex, METH_VARARGS, NULL },
991
- { " name_gdsn" , (PyCFunction)gdsnName, METH_VARARGS, NULL },
992
- { " rename_gdsn" , (PyCFunction)gdsnRename, METH_VARARGS, NULL },
993
- { " desp_gdsn" , (PyCFunction)gdsnDesp, METH_VARARGS, NULL },
1122
+ { " ls_gdsn" , (PyCFunction)gdsnListName, METH_VARARGS, NULL },
1123
+ { " index_gdsn" , (PyCFunction)gdsnIndex, METH_VARARGS, NULL },
1124
+ { " name_gdsn" , (PyCFunction)gdsnName, METH_VARARGS, NULL },
1125
+ { " rename_gdsn" , (PyCFunction)gdsnRename, METH_VARARGS, NULL },
1126
+ { " desp_gdsn" , (PyCFunction)gdsnDesp, METH_VARARGS, NULL },
994
1127
995
1128
// data operations
996
- { " read_gdsn" , (PyCFunction)gdsnRead, METH_VARARGS, NULL },
997
- { " read2_gdsn" , (PyCFunction)gdsnRead2, METH_VARARGS, NULL },
1129
+ { " read_gdsn" , (PyCFunction)gdsnRead, METH_VARARGS, NULL },
1130
+ { " read2_gdsn" , (PyCFunction)gdsnRead2, METH_VARARGS, NULL },
1131
+ { " write_gdsn" , (PyCFunction)gdsnRead, METH_VARARGS, NULL },
998
1132
999
1133
// attribute operations
1000
- { " getattr_gdsn" , (PyCFunction)gdsnGetAttr, METH_VARARGS, NULL },
1134
+ { " getattr_gdsn" , (PyCFunction)gdsnGetAttr, METH_VARARGS, NULL },
1001
1135
1002
1136
// end
1003
1137
{ NULL , NULL , 0 , NULL }
0 commit comments