⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.c

📁 功能丰富的串口通讯程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/*    utils.c -- Utility routines    Copyright (C) 1996  Nadav Cohen    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program; if not, write to the Free Software    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "screen.h"#include "utils.h"#include "config.h"#include "window.h"#include "menu.h"#include "phone.h"#include "error.h"#include "log.h"#include <string.h>#include <stdlib.h>#include <sys/time.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <ctype.h>#include <time.h>#include <fcntl.h>int Year, Month, Day;void OpenCaptureFile(WINDOW *Window, char *CaptureName){ char Temp[40], TempStr[255]; tWindow *Win; int Center, Y, X; int mode, i; time_t t; tm *Time; TempStr[0] = 0; strcpy(TempStr, Strings[CapturePath]); strcat(TempStr, "/"); strcat(TempStr, CaptureName); if (strlen(CaptureName)>0){  mode = O_RDWR | O_CREAT;  if (Integers[AppendCapture]) mode |= O_APPEND;  else mode |= O_TRUNC;  if ((CaptureHandle = open(TempStr, mode, S_IFREG | S_IRUSR | S_IWUSR)) > 0){   CaptureOn = 1;   if (Integers[AppendCapture]){    time(&t);    Time = localtime(&t);    for (i = 0; i < 80; i++)     TempStr[i] = '*';    write(CaptureHandle, "\n", 1);    write(CaptureHandle, &TempStr, 80);    strcpy(Temp, ctime(&t));    write(CaptureHandle, &Temp, strlen(Temp));    write(CaptureHandle, &TempStr, 80);   }   strcpy(TempStr, "Capture open:");   strcat(TempStr, CaptureName);   Printlogln(TempStr);  }  else WindowError(Window, "Unable to open file"); }}void DisplayMessage(WINDOW *Window, char *Text, int sleeptime){ tWindow *Win; int Center, Y, X, Attr; char Temp[10];  Attr = Window->_attrs; getyx(Window, Y, X);  Center = findCenter(0, COLS, strlen(Text)); Win = new tWindow(Window, Center-3, LINES/2-2, Center+strlen(Text)+1, LINES/2+2, Integers[cInfo] >> 4,            Integers[cInfo] % 16); Win->Frame(2, Integers[cInfo]); textAttr(Window, Integers[cInfoBold]); mvwaddstr(Window, LINES/2, Center, Text); wrefresh(Window); sleep(sleeptime); delete Win; wmove(Window, Y, X); Window->_attrs = Attr;}char *nocasestrstr(char *Haystack, char *Needle){ int i, j, len1 = strlen(Haystack), len2 = strlen(Needle);  for (i = 0; i < len1 && j != len2; i++) {  j = 0;  while (tolower(*(Haystack+j+i)) == tolower(*(Needle+j)) && *(Haystack+j+i) != '\0' && *(Needle+j) != '\0') j++; } if (j == len2) return(Haystack+i); else return NULL;}int ValidName(char *Str, char *Putin){ int i,j; char NumOfDots, Space, DotPos; char *Ptr;  NumOfDots = 0; Space = 0; DotPos = 0;  Ptr = Str; i = 0; j = 0; while (i<12 && NumOfDots < 2 && !Space && *Ptr != 0){ if (*Ptr == '.'){   NumOfDots++;   DotPos = Ptr - Str;   i = 8;  *(Putin+j) = '.';  j++; } if (isspace(*Ptr)){   Space++;  i--; } if (isalnum(*Ptr)) {  *(Putin+j) = *Ptr;  j++; } i++; Ptr++; } *(Putin+j) = 0; if (i == 0 || NumOfDots > 1 || (i > 8 && NumOfDots == 0) || DotPos > 8 || !isspace(*(Str+j)))  return 0; return 1;}/*void FromJulian(int Date){ long Temp; int Days[2][12] = {0,31,59,90,120,151,181,212,243,273,304,334,0,31,60,91,121,152,182,213,244,274,305,335}; int counter, Leap;  Year = 100*Date/36525; Temp = Year*36525l; Date -= Temp/100; if (Temp % 100 == 0){  Date++; Leap = 1; } else Leap = 0; for (Month=counter=0;counter<12;counter++)  if (Days[Leap][counter]<Date)    Month = counter+1; Day = Date - Days[Leap][Month];}*/void ReplaceM(char *s){ char *ptr; char Replaced;  Replaced = 1; while (Replaced){  Replaced = 0;  if ((ptr=strstr(s, "^M")) != NULL){   *ptr = '\r';   strcpy(ptr+1, ptr+2);   Replaced = 1;  }  if ((ptr=strstr(s, "^J")) != NULL){   *ptr = '\n';   strcpy(ptr+1, ptr+2);   Replaced = 1;  } }}void MacroParser(char *s, char *Username, char *Pwd){ char *ptr; char Replaced, Temp[256], Pass[40];  Replaced = 1; CharToPas(Pwd, Pass); while (Replaced){  Replaced = 0;  if ((ptr=strstr(s, "^")) != NULL){   *ptr = *(ptr+1)-64;   strcpy(ptr+1, ptr+3);   Replaced = 1;  }  if ((ptr=strstr(s, "!S")) != NULL){   strncpy(Temp, s, ptr-s);   Temp[ptr-s]=0;   strcat(Temp, Username);   strcat(Temp, ptr+2);   strcpy(s, Temp);   Replaced = 1;  }  if ((ptr=strstr(s, "!P")) != NULL){   strncpy(Temp, s, ptr-s);   Temp[ptr-s]=0;   strcat(Temp, Pass);   strcat(Temp, ptr+2);   strcpy(s, Temp);   Replaced = 1;  } }}void ProtocolParser(char *s, char *Dl, char *Device, int Baud, char *Filename){ char *ptr; char Replaced, Temp[256], BaudStr[10];  Replaced = 1; sprintf(BaudStr,"%d",Baud); while (Replaced){  Replaced = 0;  if ((ptr=strstr(s, "!P")) != NULL){   strncpy(Temp, s, ptr-s);   Temp[ptr-s]=0;   strcat(Temp, Dl);   strcat(Temp, ptr+2);   strcpy(s, Temp);   Replaced = 1;  }  if ((ptr=strstr(s, "!D")) != NULL){   strncpy(Temp, s, ptr-s);   Temp[ptr-s]=0;   strcat(Temp, Device);   strcat(Temp, ptr+2);   strcpy(s, Temp);   Replaced = 1;  }  if ((ptr=strstr(s, "!B")) != NULL){   strncpy(Temp, s, ptr-s);   Temp[ptr-s]=0;   strcat(Temp, BaudStr);   strcat(Temp, ptr+2);   strcpy(s, Temp);   Replaced = 1;  }  if ((ptr=strstr(s, "!F")) != NULL){   strncpy(Temp, s, ptr-s);   Temp[ptr-s]=0;   strcat(Temp, Filename);   strcat(Temp, ptr+2);   strcpy(s, Temp);   Replaced = 1;  } }}void Julian(int Jul){ long Temp, XYear; short int YYear, YMonth, YDay;  Temp = (((Jul-1721119) << 2) - 1); XYear = (Temp % 146097) | 3; Jul = Temp / 146097; YYear = XYear / 1461; Temp = ((((XYear % 1461) + 4) >> 2) * 5) - 3; YMonth =  Temp / 153; if (YMonth >= 10) { YYear++; YMonth -= 12; } YMonth = YMonth + 3; YDay = Temp % 153; YDay = (YDay + 5) / 5; Year = YYear + (Jul * 100); Month = YMonth; Day = YDay;}void PasToChar(char *Pas, char *Char){ char s[256];  if (*Pas+1 == 0) *Char = 0; else{  strncpy(s, Pas, *Pas+1);  s[s[0]+1]=0;  strcpy(Char, &s[1]); }}void CharToPas(char *Pas, char *Char){ strncpy(Pas+1, Char, strlen(Char)); *Pas = strlen(Char);}int ColorChoose(WINDOW *Window, int Color){ int Key, Refresh, LastColor, Blink; tWindow *Win; int i, j, x, y, savecolor; Win = new tWindow(Window, 50, 5, 72, 21, BLACK, WHITE);  Win->Frame(1, Integers[cMenu]); Win->Headline("Color selection", Integers[cMenuTitle]); textColor(Window, WHITE); textBkgd(Window, BLACK); mvwaddstr(Window, 19, findCenter(50,72,7), "B-Blink"); savecolor = Color; Blink = 0; if (Color > 128) { Color-=128; Blink = ~Blink; }  for (y=9,i=0;y<17;y++,i++)  for (x=53,j=0;x<69;x++,j++)  {   textAttr(Window, (i << 4) + j + (Blink & 128));   mvwaddch(Window, y, x, 254);  }   wrefresh(Window); Key = 0; Refresh = 1; LastColor = Color; while (Key != '\e' && Key !='\n'){   textAttr(Window, LastColor + (Blink & 128));   mvwaddch(Window, 9+(LastColor >> 4), 53+(LastColor % 16), 254);   textColor(Window, WHITE);   textBkgd(Window, BLACK);   mvwaddch(Window, 9+(LastColor >> 4), 52, ' ');   mvwaddch(Window, 9+(LastColor >> 4), 69, ' ');   mvwaddch(Window, 8, 53+(LastColor % 16), ' ');   mvwaddch(Window, 17, 53+(LastColor % 16), ' ');     wrefresh(Window);    textAttr(Window, Color + (Blink & 128));   mvwaddstr(Window, 7, findCenter(50,72,12), "Sample color");   wrefresh(Window);   mvwaddch(Window, 9+(Color >> 4), 53+(Color % 16), '0');   textColor(Window, WHITE);   textBkgd(Window, BLACK);   mvwaddch(Window, 9+(Color >> 4), 52, SHLINE);   mvwaddch(Window, 9+(Color >> 4), 69, SHLINE);   wrefresh(Window);   mvwaddch(Window, 8, 53+(Color % 16), '|');   mvwaddch(Window, 17, 53+(Color % 16), '|');   LastColor = Color;   wrefresh(Window);   Key = getEscChar();   switch(Key){   case KEY_DOWN:     if (Color >> 4 < 7){        Color+=16;       Refresh = 1;     }     break;   case KEY_UP:     if (Color >> 4 > 0){       Color-=16;       Refresh = 1;     }     break;   case KEY_LEFT:     if (Color % 16 > 0){      Color--;      Refresh = 1;     }     break;   case KEY_RIGHT:     if (Color % 16 < 15){      Color++;      Refresh = 1;     }     break;   case 'b':   case 'B':     Blink = ~Blink;     for (y=9,i=0;y<17;y++,i++)      for (x=53,j=0;x<69;x++,j++)     {      textAttr(Window, (i << 4) + j + (Blink & 128));      mvwaddch(Window, y, x, 254);      wrefresh(Window);     }     break;   default: Refresh = 0; break; } }   delete Win; clearok(Window, TRUE); if (Key == '\e') return (savecolor); else  return (Color+(Blink & 128));}long int ChooseBaud(WINDOW *Window, long int Baud){ int Key, Attr, x, y; tWindow *Win; tMenu *Menu;  Attr = Window->_attrs; getyx(Window, y, x);  Win = new tWindow(Window, 56,2,70,14, Integers[cMenu] >> 4, Integers[cMenu] % 16); Win->Frame(1, Integers[cMenu]); Win->Headline("Choose baud", Integers[cMenuTitle]);  Menu = new tMenu(Window, Integers[cMenuText], Integers[cMenuHot], Integers[cMenuBar], Integers[cMenuText],             ALIGNLEFT); Menu->Add("      300",57,70,3); Menu->Add("      600",57,70,4); Menu->Add("     1200",57,70,5); Menu->Add("     2400",57,70,6); Menu->Add("     4800",57,70,7); Menu->Add("     9600",57,70,8); Menu->Add("    19200",57,70,9); Menu->Add("    38400",57,70,10); Menu->Add("    57600",57,70,11); Menu->Add("   115200",57,70,12); Menu->Add("   230400",57,70,13);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -