CoolFace
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes838downloads
StrB.txt189 linesDownload Raw Back to Kernel
1 2U8 *Tabs2Spaces(U8 *src)3{//MAlloc str with tabs to spaces.4  I64 ch,i,j,l=StrLen(src)<<1+2,col=0;5  U8 *dst=MAlloc(l),*tmp;6  while (ch=*src++) {7    if (ch=='\t') {8      j=(col+8) & ~7;9      for (i=col;i<j;i++) {10        dst[i]=CH_SPACE;11        if (i>=l-2) {12          tmp=MAlloc(l<<1);13          MemCpy(tmp,dst,i+1);14          Free(dst);15          l<<=1;16          dst=tmp;17        }18      }19      col=j;20    } else {21      dst[col]=ch;22      if (col>=l-2) {23        tmp=MAlloc(l<<1);24        MemCpy(tmp,dst,col+1);25        Free(dst);26        l<<=1;27        dst=tmp;28      }29      col++;30    }31  }32  dst[col]=0;33  return dst;34}35 36U8 *ScaleIndent(U8 *src,F64 indent_scale_factor)37{//MAlloced str.  8*0.25-->2 or 8*2.0-->1638  I64 ch,i,col=0;39  U8 *dst,*dst2;40  while (ch=*src++) {41    if (ch=='\t')42      col=(col+8) & -0x8;43    else if (ch==CH_SPACE)44      col++;45    else46      break;47  }48  src--;49  col=Round(indent_scale_factor*col);50  dst=dst2=MAlloc(StrLen(src)+col/8+col&7+1);51  for (i=col/8;i>0;i--)52    *dst2++='\t';53  for (i=col&7;i>0;i--)54    *dst2++=CH_SPACE;55  StrCpy(dst2,src);56  return dst;57}58 59U8 *MStrUtil(U8 *src,I64 flags,F64 indent_scale_factor=0)60{//MAlloc StrUtil().61  U8 *dst=StrNew(src),*dst2,*tmp;62  StrUtil(dst,flags);63  if (flags & SUF_T2S) {64    tmp=Tabs2Spaces(dst);65    Free(dst);66    dst=tmp;67  }68  if (flags & SUF_SCALE_INDENT)69    dst2=ScaleIndent(dst,indent_scale_factor);70  else71    dst2=StrNew(dst); //Shorten to just right size.72  Free(dst);73  return dst2;74}75 76U0 GetOutOfDollar()77{//If a $ has been printed, print another $ to exit mode.78  CDoc *doc;79  if (IsRaw) {80    if (text.raw_flags&RWF_IN_DOLLAR)81      '$';82  } else {83    if (fp_doc_put && (doc=(*fp_doc_put)(Fs)) && doc->flags&DOCF_IN_DOLLAR)84      '$';85  }86}87 88Bool YorN()89{//Wait for user to answer Y or N.90  I64 ch;91  "(y or n)? ";92  while (TRUE) {93    ch=ToUpper(GetChar(,FALSE));94    if (ch=='Y') {95      "$PT$YES$FG$\n";96      return TRUE;97    } else if (ch=='N') {98      "$PT$NO$FG$\n";99      return FALSE;100    }101  }102}103 104I64 PressAKey()105{//Print "Press a key" and wait for non-zero ASCII key.106  "$BK,1$PRESS A KEY$BK,0$\n";107  return GetChar(,FALSE);108}109 110Bool AreYouSure()111{//Print "Are you sure" and waits for Y or N.112  "ARE YOU SURE ";113  return YorN;114}115 116U0 Help()117{//Dbg help or master help index file.118  if (IsDbgMode)119    DbgHelp;120  else121    PopUp("Type(\"::/Doc/HelpIndex.DD\");DocTop;View;");122}123 124U0 ScanFlags(U8 *_dst_flags,U8 *lst,U8 *src)125{/*More than 64 flags. Flags passed by ref.126 127Examples:128ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),fu_flags);129 130I64 flags=0;131ScanFlags(&flags,"R\0L\0Dump\0Scan\0","+Dump-R"); //Sets Bit#2, Clears Bit#0.132*/133  I64 i;134  U8 *buf,*ptr;135  if (src) {136    buf=MAlloc(StrLen(src)+1);137    while (*src) {138      while (*src && *src!='+' && *src!='-')139        src++;140      if (*src=='+') {141        src++;142        if (*src) {143          ptr=buf;144          while (*src && *src!='+' && *src!='-' &&145                *src!=CH_SPACE && *src!=CH_SHIFT_SPACE)146            *ptr++=*src++;147          *ptr=0;148          i=LstMatch(buf,lst);149          if (i>=0)150            LBts(_dst_flags,i);151          else {152            Free(buf);153            throw('ScanFlag');154          }155        }156      } else if (*src=='-') {157        src++;158        if (*src) {159          ptr=buf;160          while (*src && *src!='+' && *src!='-' &&161                *src!=CH_SPACE && *src!=CH_SHIFT_SPACE)162            *ptr++=*src++;163          *ptr=0;164          i=LstMatch(buf,lst);165          if (i>=0)166            LBtr(_dst_flags,i);167          else {168            Free(buf);169            throw('ScanFlag');170          }171        }172      }173    }174    Free(buf);175  }176}177 178U8 *StrPrintFlags(U8 *dst,U8 *lst,I64 flags)179{//Only 64 flags. Flags passed by value.180  I64 i;181  *dst=0;182  while (flags) {183    i=Bsf(flags);184    Btr(&flags,i);185    CatPrint(dst,"+%z",i,lst);186  }187  return dst;188}189