void StringToNLMPath( char *name, char *res ) { BYTE isdos; LONG handle; BYTE filename[14]; unsigned i; /* right trim the name */ i = strlen( name ); for( ;; ) { if( i == 0 ) break; if( name[i-1] != ' ' ) break; --i; } name[i] = '\0'; ccode = OpenFileUsingSearchPath( (BYTE *)name, &handle, &isdos, (BYTE *)res, filename, FALSE, 4, ".NLM", ".DSK", ".LAN", ".NAM" ); if( ccode == 0 ) { AppendStr( res, (char *)filename ); if( isdos ) { ccode = INWDOSClose( handle ); } else { ccode = CloseFile(0, 1, handle ); } } else { res[0] = '\0'; } }
// Helper functions to combine paths static MyString MakePath(const MyString &root, const MyString &file) { MyString res = root; if (!EndsWithChar(res, W('\\'))) AppendChar(res, W('\\')); AppendStr(res, file); return res; }
/* ExitComposer - clean up after the compose window. If DirectComp flag is set * set QuitFlag = TRUE; * * arguments: * none. * * return value: * none. */ VOID PASCAL INTERNAL ExitComposer (VOID) { CloseWindow ( hCompose ); BringToTop ( hCommand, FALSE ); hCompose = NULL; hFocus = hHeaders; _unlink ( pCompFile ); /* in case the user's editor created a *.bak file */ pCompFile = AppendStr ( pCompFile, "bak", NULL, TRUE ); _unlink ( pCompFile ); ZMfree ( pCompFile ); if ( fDirectComp ) fQuit = TRUE; SetScrnSt ( BHDRNOCOMP ); }
int IOOpen( char *openname, int openmode ) { BYTE isdos; LONG handle; BYTE filename[14]; BYTE loadpath[256]; my_file *p; LONG filesize; struct find_t dta; // if( !MayRelinquishControl ) return( -1 ); if( openmode == O_RDONLY ) { ccode = OpenFileUsingSearchPath( (BYTE *)openname, &handle, &isdos, loadpath, filename, FALSE, 0 ); AppendStr( (char *)loadpath, (char *)filename ); _DBG_IO(( ( ccode==0 ? "Opened %s." : "" ), loadpath )); } else { ccode = OpenServer( OpenFile, openname, &handle, FILE_ATTRIB_MASK, FILE_OPEN_PRIVS ); _DBG_IO(( "Opened %s.", openname )); isdos = FALSE; } _DBG_IO(( " RC(%d). Handle=%8x\r\n", ccode, handle )); if( ccode == 0 ) { p = FindFile(); p->handle = handle; p->seekpos = 0; p->file_type = isdos ? FILE_DOS : FILE_SERVER; if( isdos ) { p->routine = ReadDOS; ccode = INWDOSFindFirstFile( loadpath, 0, &dta ); p->filesize = dta.size; } else { ccode = GetFileSize( 0, p->handle, &filesize ); p->routine = ReadServer; p->filesize = filesize; } if( openmode != O_RDONLY ) { p->routine = NULL; } _DBG_IO(( ( ccode != 0 ? " Size failed - ccode %d\r\n" : "" ), ccode )); _DBG_IO(( ( ccode == 0 ? " Size is %d\r\n" : "" ), p->filesize )); } return( ccode ? ErrorCode() : ( p->handlenum + FIRST_HANDLE ) ); }
/* EnterComposer - start up the compose window, set up hooks, etc. then spawn * MAILEDIT editor via DoEditComp, draw compose window. * * arguments: * pFileName name of file to invoke compose on. * pStuffApnd pointer to string to pass to DoAppend. * fEdit nonzero => invoke editor * * return value: * none. * */ VOID PASCAL INTERNAL EnterComposer ( PSTR pFileName, PSTR pStuffApnd, FLAG fEdit ) { *commandLine = '\0'; hCompose = NULL; pCompFile = ZMMakeStr ( pFileName ); /* Make sure file name ends with . because some editors will tack on * their own default extensions if file name doesn't contain a . */ if ( !*strbscan ( pCompFile, strPERIOD ) ) pCompFile = AppendStr ( pCompFile, strPERIOD, NULL, TRUE ); DoAppend ( hCommand, (pStuffApnd ? pStuffApnd : strEMPTY), FALSE ); if ( fEdit ) DoEditComp ( hCommand, NULL, 0 ); hFocus = hCompose = ZmReadFile ( pCompFile, "The Composer", TRUE, 0, 0, -xSize, -HdrHeight, readProc, StdSKey ); BringToTop ( hCommand, FALSE ); SetScrnSt ( BIGCOMPOSE ); }
static long OpenServer( LONG (*routine)(), char *name, LONG *handle, LONG attr, LONG privs ) { LONG volumeNumber, pathBase, pathCount, entryNumber; BYTE pathString[128]; BYTE fileName[256]; void *Entry; fileName[1] = '\0'; fileName[0] = AppendStr( (char *)fileName + 1, name ); if (ConvertPathString(0, 0, fileName, &volumeNumber, &pathBase, pathString, &pathCount) != 0) /* The path is invalid */ return (-1); /* Attempt to open the command file */ return( routine(0, 1, volumeNumber, pathBase, pathString, pathCount, 0, attr, privs, PrimaryDataStream, handle, &entryNumber, &Entry ) ); }
static bool Expand(const char* filename, int* line, FILE* f, char** pBuf, int end, Dict* env, Array* arr, int arrIndex) { char* buf = *pBuf; while(true) { int c = getc(f); if (c == end) { break; } if(c == '\n') ++*line; if(c == '[') { c = getc(f); if(c == '[') { sb_push(buf, '['); continue; } char var[VAR_SIZE]; const Tiny_Value* val = ReadVar(filename, line, f, env, arr, arrIndex, c, var); c = getc(f); if(c != ']') { ERROR("Expected ']' after %s.\n", var); } c = getc(f); if(c != '{') { ERROR("Expected '{' after ']'.\n"); } c = getc(f); long epos = ftell(f); int nest = 1; while(c != EOF && nest != 0) { if(c == '\n') ++*line; if (c == '{') ++nest; if (c == '}') --nest; c = getc(f); } if (c == EOF) { ERROR("Unexpected EOF.\n"); } long pos = ftell(f); if(!val || Tiny_GetProp(*val) != &ArrayProp) { ERROR("Attempted to expand '[%s]' but %s is not an array.\n", var, var); } else { Array* a = Tiny_ToAddr(*val); *pBuf = buf; for(int i = 0; i < a->length; ++i) { fseek(f, epos, SEEK_SET); Expand(filename, line, f, pBuf, '}', env, a, i); } buf = *pBuf; fseek(f, pos, SEEK_SET); } } else if(c == '?') { c = getc(f); if(c == '?') { sb_push(buf, '?'); continue; } char var[VAR_SIZE]; const Tiny_Value* val = ReadVar(filename, line, f, env, arr, arrIndex, c, var); c = getc(f); if(c != '{') { ERROR("Expected '{' after %s.\n", var); } c = getc(f); long tp = ftell(f); int nest = 1; while(c != EOF && nest != 0) { if(c == '\n') ++*line; if (c == '{') ++nest; if (c == '}') --nest; c = getc(f); } if (c == EOF) { ERROR("Unexpected EOF.\n"); } if(c != '{') { ERROR("Expected '{' after '}' in '?%s' clause.\n", var); } c = getc(f); long fp = ftell(f); nest = 1; while(c != EOF && nest != 0) { if(c == '\n') ++*line; if (c == '{') ++nest; if (c == '}') --nest; c = getc(f); } if (c == EOF) { ERROR("Unexpected EOF.\n"); } long pos = ftell(f); if(!val || !Tiny_ToBool(*val)) { fseek(f, fp, SEEK_SET); } else { fseek(f, tp, SEEK_SET); } *pBuf = buf; Expand(filename, line, f, pBuf, '}', env, NULL, -1); buf = *pBuf; fseek(f, pos, SEEK_SET); } else if(c == '$') { c = getc(f); if(c == '$') { sb_push(buf, c); continue; } char var[VAR_SIZE]; const Tiny_Value* val = ReadVar(filename, line, f, env, arr, arrIndex, c, var); if(!val) { ERROR("Var '%s' doesn't exist in env.\n", var); } switch(val->type) { case TINY_VAL_BOOL: { AppendStr(&buf, val->boolean ? "true" : "false"); } break; case TINY_VAL_INT: { char s[32]; sprintf(s, "%i", val->i); AppendStr(&buf, s); } break; case TINY_VAL_FLOAT: { char s[32]; sprintf(s, "%g", val->f); AppendStr(&buf, s); } break; case TINY_VAL_STRING: case TINY_VAL_CONST_STRING: { AppendStr(&buf, Tiny_ToString(*val)); } break; case TINY_VAL_NATIVE: { if(Tiny_GetProp(*val) != &BufProp) { goto defaultCase; } char* b = *(char**)Tiny_ToAddr(*val); for(int i = 0; i < sb_count(b); ++i) { sb_push(buf, b[i]); } } break; defaultCase: default: { ERROR("Attempted to expand unsupported value at '$%s'.\n", var); } break; } } else { sb_push(buf, c); } } *pBuf = buf; return true; error: *pBuf = buf; return false; }
/* Convert a number using a scientific format (e.g. 0.00e+00). The num argument is the number to convert. The argument numFrac is the number of digits in the fraction, expLen is the number of digits in the exponent, plusExp defines whether to prefix a positive exponent always with a '+', and optFrac speficies the number of fraction digits that are only generated if they are not zeroes. */ static void NumberToScientific(FormatOutput *output, AValue num, int numFrac, int expLen, int expChar, ABool plusExp, int optFrac) { /* FIX: Use sprintf for the conversion for better accuracy. */ double f = AGetFloat(output->t, num); int exp = 0; ABool sign = f < 0.0; int i; char s[NUM_BUF_SIZE]; int si; int s0; /* Handle infinities and NaN's as special cases. */ if (AIsNaN(f)) { AppendStr(output, "nan"); return; } else if (AIsInf(f)) { if (f > 0.0) AppendStr(output, "inf"); else AppendStr(output, "-inf"); return; } if (sign) f = -f; if (f != 0.0) { double max = 10.0 - 0.5 * pow(10.0, -numFrac); double min = 1.0 - 0.5 * pow(10.0, -numFrac); while (f >= max) { f /= 10.0; exp++; } while (f < min) { f *= 10.0; exp--; } } f = floor(f * pow(10.0, numFrac) + 0.5); if (numFrac >= NUM_BUF_SIZE - 2) ARaiseValueError(output->t, "Formatted Float is too long"); si = 0; for (i = 0; i <= numFrac; i++) { int d = (int)fmod(f, 10.0); s[si++] = '0' + d; f /= 10.0; } s0 = 0; while (optFrac > 0 && s0 < si && s[s0] == '0') { s0++; optFrac--; numFrac--; } if (sign) AppendCh(output, '-'); AppendCh(output, s[si - 1]); if (numFrac > 0) AppendCh(output, '.'); for (i = si - 2; i >= s0; i--) AppendCh(output, s[i]); AppendCh(output, expChar); if (plusExp && exp >= 0) AppendCh(output, '+'); NumberToStr(output, AMakeInt(output->t, exp), expLen, 0, 0); }
/* Convert a number to a string using a non-scientific format. The num argument is the number to convert, intLen is the minimum number of digits in the integral part of the number, fractionLen is the minimum length of the fraction and optFrac is the number of additional fraction digits to include if they are non-zero. */ static void NumberToStr(FormatOutput *output, AValue num, int intLen, int fractionLen, int optFrac) { int i; if (AIsInt(num)) { /* Int */ int sign; output->str[1] = num; output->str[1] = AStdStr(output->t, output->str + 1); if (output->str[1] == AError) ADispatchException(output->t); if (AStrItem(output->str[1], 0) == '-') { Append(output, '-'); sign = 1; } else sign = 0; for (i = 0; i < intLen - AStrLen(output->str[1]) + sign; i++) Append(output, '0'); for (i = sign; i < AStrLen(output->str[1]); i++) Append(output, AStrItem(output->str[1], i)); if (optFrac < fractionLen) { Append(output, '.'); for (i = 0; i < fractionLen - optFrac; i++) Append(output, '0'); } } else { /* Float */ double f; char s[NUM_BUF_SIZE]; int si; int sign; f = AGetFloat(output->t, num); /* Display very small and very large values in scientific format to avoid producing very long results. */ if (f > 1e50 || f < -1e50) { NumberToScientific(output, num, fractionLen, 1, 'e', TRUE, optFrac); return; } if (AIsNaN(f)) { AppendStr(output, "nan"); return; } if (fractionLen + 50 + 5 > NUM_BUF_SIZE) ARaiseValueError(output->t, "Formatted Float is too long"); si = sprintf(s, "%.*f", fractionLen, AGetFloat(output->t, num)); if (s[0] == '-') { AppendCh(output, '-'); sign = 1; } else sign = 0; if (intLen > 1) { int isFract = strchr(s, '.') != NULL; while (intLen + isFract + fractionLen > si - sign) { AppendCh(output, '0'); intLen--; } } while (optFrac > 0 && si > 0 && s[si - 1] == '0') { si--; optFrac--; fractionLen--; } for (i = sign; i < si; i++) AppendCh(output, s[i]); } }
void TDFWriter::AppendFloat(const std::string& name, float value) { AppendStr(name, Util::ToFloatString(value)); }
void TDFWriter::AppendInt(const std::string& name, int value) { AppendStr(name, Util::ToIntString(value)); }